@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
108 lines (77 loc) • 2.73 kB
text/coffeescript
suite ".pollEvery() + .stopPolling()", ()->
suiteSetup(restartSandbox)
test 'Will cause the value to be manually polled from the subject at a given interval', (done)->
shouldFinish = false
lastValue = undefined
binding = undefined
arr = []
fn = (length)->
if shouldFinish
expect(length).to.equal 2
binding.stopPolling()
done()
else
lastValue = length
binding = SimplyBind('length').of(arr).to(fn)
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(fn).pollEvery(5)
setTimeout ()->
shouldFinish = true
expect(lastValue).to.equal 0
arr.push 'some value'
arr.push 'and another'
expect(lastValue).to.equal 0
, 8
test 'Will invoke a function upon poll', (done)->
invokeCountA = 0
invokeCountB = 0
binding = SimplyBind(()-> ++invokeCountA).to(()-> ++invokeCountB).pollEvery(5)
setTimeout ()->
expect(invokeCountA).to.be.gt(1)
expect(invokeCountB).to.be.gt(1)
binding.stopPolling()
done()
, 15
test "Will re-create the interval poll if called .pollEvery() twice with different values", (done)->
invokeCount = 0
objectA.prop = 200
expect ()-> SimplyBind('prop', {'updateEvenIfSame':true}).of(objectA).to(()->invokeCount++).pollEvery(50).pollEvery(5)
.not.to.throw()
setTimeout ()->
expect(invokeCount).to.be.gte(3)
done()
restartSandbox()
, 75
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", (done)->
invokeCount = 0
binding = SimplyBind('prop', {updateEvenIfSame:true, updateOnBind:false}).of(objectA)
.transformSelf ()-> invokeCount++
.to(noop)
binding.pollEvery(5)
setTimeout ()->
expect(invokeCount).to.be.gte(1)
lastCount = invokeCount
SimplyBind.unBindAll(objectA)
setTimeout ()->
expect(invokeCount).to.equal lastCount
done()
, 15
, 15
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()