@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
107 lines (75 loc) • 3.34 kB
text/coffeescript
suite ".updateOn() + .removeUpdater()", ()->
suiteSetup(restartSandbox)
test "Will listen for changes on the desired target upon change will poll (and update if changed) the binding's value", ()->
Object.defineProperty eventEmitterA, 'nonLiveProp', {value:'someValue', writable:true, configurable:false}
binding = SimplyBind('nonLiveProp').of(eventEmitterA).to('prop1').of(objectA).updateOn('event:click').of(eventEmitterA)
binding.set 'one'
expect(objectA.prop1).to.equal 'one'
eventEmitterA.nonLiveProp = 'two'
expect(objectA.prop1).to.equal 'one'
eventEmitterA.emit 'click'
expect(objectA.prop1).to.equal 'two'
binding.removeUpdater('event:click').of(eventEmitterA)
eventEmitterA.nonLiveProp = 'three'
expect(objectA.prop1).to.equal 'two'
eventEmitterA.emit 'click'
expect(objectA.prop1).to.equal 'two'
restartSandbox()
test "Will do nothing when trying to remove an updater that wasn't registered or when trying to add an updater that was already registered", ()->
Object.defineProperty eventEmitterB, 'nonLiveProp', {value:'someValue', writable:true, configurable:false}
binding = SimplyBind('nonLiveProp').of(eventEmitterB).to('prop1').of(objectA).updateOn('event:click').of(eventEmitterB)
pubsMap = binding._.pubsMap or binding._.pM;
Object.defineProperty(pubsMap, 'length', get:()->Object.keys(@).length)
expect(pubsMap.length).to.equal 1
binding = SimplyBind('nonLiveProp').of(eventEmitterB).to('prop1').of(objectA).updateOn('event:click').of(eventEmitterB)
binding = SimplyBind('nonLiveProp').of(eventEmitterB).to('prop1').of(objectA).updateOn('event:change').of(eventEmitterB)
expect(pubsMap.length).to.equal 2
binding.removeUpdater('event:clicker').of(eventEmitterB)
expect(pubsMap.length).to.equal 2
binding.removeUpdater('event:click').of(eventEmitterB)
expect(pubsMap.length).to.equal 1
binding.removeUpdater('event:change').of(eventEmitterB)
expect(pubsMap.length).to.equal 0
restartSandbox()
test "Multiple updaters can be registered", ()->
invokeCount = 0
SimplyBind('prop', updateEvenIfSame:true).of(objectA)
.updateOn('prop').of(objectB)
.updateOn('prop').of(objectC)
.to ()-> invokeCount++
expect(invokeCount).to.equal 1
objectA.prop = !objectA.prop
expect(invokeCount).to.equal 2
objectB.prop = !objectB.prop
expect(invokeCount).to.equal 3
objectC.prop = !objectC.prop
expect(invokeCount).to.equal 4
restartSandbox()
test "Can be thorttled", (done)->
invokeCount = 0
objectA.prop = true
binding = SimplyBind('prop', updateEvenIfSame:true).of(objectA)
.updateOn('prop', throttle:5).of(objectB)
.updateOn('prop').of(objectC)
.to ()-> invokeCount++
expect(invokeCount).to.equal 1
objectA.prop = !objectA.prop
objectA.prop = !objectA.prop
objectA.prop = !objectA.prop
expect(invokeCount).to.equal 4
objectB.prop = !objectB.prop
expect(invokeCount).to.equal 5
objectC.prop = !objectC.prop
expect(invokeCount).to.equal 6
objectC.prop = !objectC.prop
objectC.prop = !objectC.prop
objectC.prop = !objectC.prop
expect(invokeCount).to.equal 9
objectB.prop = !objectB.prop
objectB.prop = !objectB.prop
objectB.prop = !objectB.prop
expect(invokeCount).to.equal 9
setTimeout ()->
expect(invokeCount).to.equal 10
done()
, 10