@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
122 lines (84 loc) • 3.23 kB
text/coffeescript
suite ".pollEvery() + .stopPolling()", ()->
suiteSetup(restartSandbox)
setup ()-> = sinon.useFakeTimers()
teardown ()-> .restore()
test 'Will cause the value to be manually polled from the subject at a given interval', ()->
lastValue = undefined
arr = []
binding = SimplyBind('length').of(arr).to((v)-> lastValue = v)
expect(lastValue).to.equal 0
arr.push 'some value'
arr.push 'and another'
expect(lastValue).to.equal 0
arr = []
binding = SimplyBind('length').of(arr).to((v)-> lastValue = v).pollEvery(5)
expect(lastValue).to.equal 0
.tick(5)
expect(lastValue).to.equal 0
arr.push 'some value','and another','last value'
expect(lastValue).to.equal 0
.tick(5)
expect(lastValue).to.equal 3
.tick(5)
expect(lastValue).to.equal 3
arr.push 'some value'
arr.push 'and another'
binding.stopPolling()
test 'Will invoke a function upon poll', ()->
invokeCountA = 0
invokeCountB = 0
binding = SimplyBind(()-> ++invokeCountA).to(()-> ++invokeCountB).pollEvery(5)
expect(invokeCountA).to.equal 1
expect(invokeCountB).to.equal 1
.tick(4)
expect(invokeCountA).to.equal 1
expect(invokeCountB).to.equal 1
.tick(1)
expect(invokeCountA).to.equal 2
expect(invokeCountB).to.equal 2
.tick(5)
expect(invokeCountA).to.equal 3
expect(invokeCountB).to.equal 3
.tick(5)
expect(invokeCountA).to.equal 4
expect(invokeCountB).to.equal 4
binding.stopPolling()
test "Will re-create the interval poll if called .pollEvery() twice with different values", ()->
invokeCount = 0
objectA.prop = 200
expect ()-> SimplyBind('prop', {'updateEvenIfSame':true}).of(objectA).to(()->invokeCount++).pollEvery(50).pollEvery(5)
.not.to.throw()
expect(invokeCount).to.equal 1
.tick(5)
expect(invokeCount).to.equal 2
.tick(5)
expect(invokeCount).to.equal 3
.tick(50)
expect(invokeCount).to.equal 13
restartSandbox()
test "Will not throw errors when calling .stopPolling() on a binding with no pre-set interval", ()->
expect ()-> SimplyBind('prop').of(objectA).to('prop').of(objectB).stopPolling()
.not.to.throw()
restartSandbox()
test "Polling will be removed upon binding destruction", ()->
invokeCount = 0
binding = SimplyBind('prop', {updateEvenIfSame:true, updateOnBind:false}).of(objectA)
.transformSelf ()-> invokeCount++
.to(noop)
binding.pollEvery(5)
expect(invokeCount).to.equal 0
.tick(5)
expect(invokeCount).to.equal 1
.tick(5)
expect(invokeCount).to.equal 2
SimplyBind.unBindAll(objectA)
.tick(15)
expect(invokeCount).to.equal 2
test "Invoking on an event binding will be ineffictive and will not create an interval timer", ()->
objInterface = SimplyBind('prop').of(objectA).pollEvery(50)
eventInterface = SimplyBind('event:someEvent').of(eventEmitterA).pollEvery(50)
targetProperty = if objInterface.property? then 'pollInterval' else 'PI'
expect(typeof objInterface._[targetProperty]).to.equal if isBrowser then 'number' else 'object'
expect(typeof eventInterface._[targetProperty]).to.equal 'undefined'
objInterface.stopPolling()
restartSandbox()