Sha256: 284938b1567c9912825305fc622f67ac419dd70a14835d2d10fe88453f6647f2

Contents?: true

Size: 1.39 KB

Versions: 20

Compression:

Stored size: 1.39 KB

Contents

##
# Knife: Get, set or mock inaccessible variables in a Javascript closure
# ======================================================================
#
# Requires [Jasmine](http://jasmine.github.io/) 2+.
#
# Usage:
#
#     klass = (->
#
#       privateVariable = 0
#
#       privateMethod = ->
#         privateVariable += 1
#
#       publicMethod = ->
#         privateMethod()
#
#       add: add
#       knife: eval(Knife.point)
#
#     )()
#
#     klass.knife.get('privateVariable') => 0
#     klass.knife.set('privateCounter', 5)
#     klass.knife.get('privateCounter') => 5
#     spy = klass.knife.mock('privateMethod').and.returnValue("mocked!")
#     klass.publicMethod() # => 'mocked!'
#     expect(spy).toHaveBeenCalled()
#
@Knife = (->

  contextBleeder = ->

    get = (symbol) ->
      eval(symbol)

    set = (symbol, value) ->
      eval("#{symbol} = value")

    mock = (symbol) ->
      oldImpl = get(symbol)
      spy = jasmine.createSpy(symbol)
      set(symbol, spy)
      cleaner = -> set(symbol, oldImpl)
      Knife.cleaners.push(cleaner)
      spy

    get: get
    set: set
    mock: mock

  reset = ->
    for cleaner in Knife.cleaners
      cleaner()
    Knife.cleaners = []

  me = {}
  me.reset = reset
  me.cleaners = []

  if jasmine?
    me.point = "(#{contextBleeder.toString()})()"
    # Jasmine defines afterEach on window
    afterEach reset
  else
    me.point = "undefined"

  me

)()

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
upjs-rails-0.16.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.15.1 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.15.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.14.1 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.14.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.13.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.5 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.4 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.3 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.2 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.1 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.12.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.11.1 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.11.0 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.5 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.4 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.3 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.2 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.1 spec_app/spec/javascripts/helpers/knife.js.coffee
upjs-rails-0.10.0 spec_app/spec/javascripts/helpers/knife.js.coffee