@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
49 lines (35 loc) • 1.44 kB
text/coffeescript
suite "Function", ()->
test "Will be invoked on bind and have its return value used as the value that the subscribers will receive", ()->
SimplyBind ()-> 123
.to('prop').of(objectA)
expect(objectA.prop).to.equal 123
test "Will be invoked on bind as a dependent and will receive 2 arguments - 1st is the new value passed while the 2nd is the prev value passed", ()->
invokeCount = 0
objectA.prop1 = 'some value'
SimplyBind('prop1').of(objectA)
.to (newValue, prevValue)->
switch invokeCount
when 0
expect(newValue).to.equal 'some value'
expect(prevValue).to.be.undefined
when 1
expect(newValue).to.equal 'new value'
expect(prevValue).to.equal 'some value'
when 2
expect(newValue).to.equal 'newer value'
expect(prevValue).to.equal 'new value'
invokeCount++
objectA.prop1 = 'new value'
objectA.prop1 = 'newer value'
restartSandbox()
test "Will be invoked when is the first part of an argument regardless of whether or not SimplyBind.options.updateOnBind is on", ()->
invokeCount = 0
expect(SimplyBind.defaultOptions.updateOnBind).to.be.true
SimplyBind ()-> invokeCount++; 123
.to('prop').of(objectA)
expect(invokeCount).to.equal 1
SimplyBind.defaultOptions.updateOnBind = false
SimplyBind ()-> invokeCount++; 456
.to('prop').of(objectA)
expect(invokeCount).to.equal 2
SimplyBind.defaultOptions.updateOnBind = true