@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
166 lines (113 loc) • 4.74 kB
text/coffeescript
suite "Miscellenious", ()->
test "Binding single prop to multiple ObjectProps' placeholders", ()->
dispatcher = 'prop':'word'
objectA.prop1 = objectB.prop1 = objectC.prop1 = 'some {{noun}} here'
SimplyBind('prop').of(dispatcher)
.to('multi:prop1.noun').of([objectA, objectB, objectC])
expect(objectA.prop1).to.equal 'some word here'
expect(objectB.prop1).to.equal 'some word here'
expect(objectC.prop1).to.equal 'some word here'
dispatcher.prop = 'letter'
expect(objectA.prop1).to.equal 'some letter here'
expect(objectB.prop1).to.equal 'some letter here'
expect(objectC.prop1).to.equal 'some letter here'
restartSandbox()
test "Fetching the value of a multi binding should yield an array of all the values", ()->
objectA.prop1 = 'A'
objectB.prop1 = 'B'
objectC.prop1 = 'C'
binding = SimplyBind('multi:prop1').of([objectA, objectB, objectC])
values = binding.value
expect(objectA.prop1).to.equal 'A'
expect(objectB.prop1).to.equal 'B'
expect(objectC.prop1).to.equal 'C'
expect(values).to.be.instanceOf Array
expect(values[0]).to.equal 'A'
expect(values[1]).to.equal 'B'
expect(values[2]).to.equal 'C'
restartSandbox()
test "Fetching the value using the method of a multi binding w/ placeholders should yield an array containing the values of each binding", ()->
dispatcher = 'prop':'word'
objectA.prop1 = objectB.prop1 = objectC.prop1 = 'some {{noun}} here'
SimplyBind('prop').of(dispatcher)
.to('multi:prop1.noun').of([objectA, objectB, objectC])
binding = SimplyBind('multi:prop1.noun').of([objectA, objectB, objectC])
value = binding.value
expect(objectA.prop1).to.equal 'some word here'
expect(objectB.prop1).to.equal 'some word here'
expect(objectC.prop1).to.equal 'some word here'
expect(value.length).to.equal 3
expect(value[0]).to.equal 'word'
restartSandbox()
test "Binding single prop to multiple ObjectProps with a self transform should be applied to all bindings", ()->
dispatcher = 'prop':'value1'
objectA.prop1 = objectB.prop1 = objectC.prop1 = null
SimplyBind('prop').of(dispatcher)
.to('multi:prop1').of([objectA, objectB, objectC])
SimplyBind('multi:prop1').of([objectA, objectB, objectC])
.transformSelf (value)-> value.toUpperCase()
expect(objectA.prop1).to.equal 'VALUE1'
expect(objectB.prop1).to.equal 'VALUE1'
expect(objectC.prop1).to.equal 'VALUE1'
dispatcher.prop = 'value2'
expect(objectA.prop1).to.equal 'VALUE2'
expect(objectB.prop1).to.equal 'VALUE2'
expect(objectC.prop1).to.equal 'VALUE2'
restartSandbox()
test "throttling is supported", (done)->
invokeCount = 0
lastValue = null
objectA.prop1 = objectB.prop1 = objectC.prop1 = 'same'
SimplyBind('multi:prop1', {throttle:15, updateEvenIfSame:true}).of([objectA, objectB, objectC])
.to (v)-> lastValue=v; invokeCount++
expect(invokeCount).to.equal(3)
expect(lastValue).to.equal 'same'
objectA.prop1 = 'another1'
objectA.prop1 = 'another2'
objectA.prop1 = 'another3'
objectA.prop1 = 'another4'
expect(invokeCount).to.equal(3)
expect(lastValue).to.equal('same')
objectB.prop1 = 'another5'
expect(lastValue).to.equal('same')
expect(invokeCount).to.equal(3)
objectC.prop1 = 'another6'
expect(lastValue).to.equal('same')
expect(invokeCount).to.equal(3)
objectC.prop1 = 'another7'
expect(invokeCount).to.equal(3)
expect(lastValue).to.equal('same')
setTimeout ()->
expect(invokeCount).to.equal(6)
expect(lastValue is 'another5' or lastValue is 'another7').to.be.true
done()
restartSandbox()
, 50
test "Using the 'multi:' descriptor on a empty DOM list should throw an error", ()->
expect ()-> SimplyBind('multi:prop').of($('nonexistent'))
.to.throw()
expect ()-> SimplyBind('multi:prop').of(document.querySelectorAll('nonexistent'))
.to.throw()
expect ()-> SimplyBind('multi:prop').of([])
.to.throw()
restartSandbox()
test "The 'multi:' descriptor can be used along with other descriptors", ()->
emitCount = 0
SimplyBind('multi:event:someEvent#0').of([eventEmitterA, eventEmitterB, eventEmitterC])
.to ()-> emitCount++
expect(emitCount).to.equal 0
eventEmitterA.emit('someEvent')
expect(emitCount).to.equal 1
eventEmitterB.emit('someEvent')
expect(emitCount).to.equal 2
eventEmitterC.emit('someEvent')
expect(emitCount).to.equal 3
if isBrowser
SimplyBind('prop').of(objectA)
.to('multi:attr:someattr').of([regA, regB, regC])
objectA.prop = 'anotherValue'
expect(regA.getAttribute 'someattr').to.equal 'anotherValue'
expect(regB.getAttribute 'someattr').to.equal 'anotherValue'
expect(regC.getAttribute 'someattr').to.equal 'anotherValue'
restartSandbox()