Sha256: 0099a70565ee8cfe556e08114ab6cc49ddc15c1a0fbf27ecdcba829d4f83810e

Contents?: true

Size: 1.37 KB

Versions: 28

Compression:

Stored size: 1.37 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 = ->
    cleaner() while cleaner = Knife.cleaners.pop()

  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

28 entries across 28 versions & 2 rubygems

Version Path
unpoly-rails-0.31.2 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.31.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.31.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.30.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.30.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.29.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.28.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.28.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.27.3 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.27.2 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.27.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.27.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.26.2 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.26.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.26.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.25.2 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.25.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.25.0 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.24.1 spec_app/spec/javascripts/helpers/knife.js.coffee
unpoly-rails-0.24.0 spec_app/spec/javascripts/helpers/knife.js.coffee