UNPKG

@danielkalen/simplybind

Version:

Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.

141 lines (94 loc) 3.74 kB
suite ".once", ()-> suiteSetup(restartSandbox) suite "Allows to bind a publisher to a subscriber to be updated only once", ()-> test "ObjectProp", ()-> SimplyBind('prop').of(objectA) .once.to('prop').of(objectB) .and.to('prop').of(objectC) SimplyBind('interrupter', updateOnBind:false).of(objectA) .to('prop').of(objectB) objectA.interrupter = 'interrupterValue' objectA.prop = 'anotherValue' expect(objectB.prop).to.equal 'anotherValue' expect(objectC.prop).to.equal 'anotherValue' objectA.prop = 'diffValue' expect(objectB.prop).to.equal 'anotherValue' expect(objectC.prop).to.equal 'diffValue' restartSandbox() test "DOMText", ()-> if not isBrowser then @skip() else regA.textContent = regB.textContent = 'This line has "{{word}}" in it' SimplyBind('prop').of(objectA) .once.to('textContent.word').of(regA) .and.to('textContent.word').of(regB) objectA.prop = 'coin' expect(regA.textContent).to.equal 'This line has "coin" in it' expect(regB.textContent).to.equal 'This line has "coin" in it' objectA.prop = 'quotes' expect(regA.textContent).to.equal 'This line has "coin" in it' expect(regB.textContent).to.equal 'This line has "quotes" in it' restartSandbox() test "DOMValue", ()-> if not isBrowser then @skip() else SimplyBind('prop').of(objectA) .once.to('value').of(inputA) .and.to('value').of(inputB) objectA.prop = 'anotherValue' expect(inputA.value).to.equal 'anotherValue' expect(inputB.value).to.equal 'anotherValue' objectA.prop = 'diffValue' expect(inputA.value).to.equal 'anotherValue' expect(inputB.value).to.equal 'diffValue' restartSandbox() test "DOMAttr", ()-> if not isBrowser then @skip() else SimplyBind('prop').of(objectA) .once.to('attr:someattr').of(regA) .and.to('attr:someattr').of(regB) objectA.prop = 'anotherValue' expect(regA.getAttribute 'someattr').to.equal 'anotherValue' expect(regB.getAttribute 'someattr').to.equal 'anotherValue' objectA.prop = 'diffValue' expect(regA.getAttribute 'someattr').to.equal 'anotherValue' expect(regB.getAttribute 'someattr').to.equal 'diffValue' restartSandbox() test "Func", ()-> invokeCountOnce = 0 invokeCountTotal = 0 SimplyBind('prop1').of(objectA) .once.to ()-> invokeCountOnce++; return .and.to ()-> invokeCountTotal++; return expect(invokeCountOnce).to.equal(0) expect(invokeCountTotal).to.equal(1) objectA.prop1 += 1 expect(invokeCountOnce).to.equal(1) expect(invokeCountTotal).to.equal(2) objectA.prop1 += 1 expect(invokeCountOnce).to.equal(1) expect(invokeCountTotal).to.equal(3) restartSandbox() test "Event", ()-> if not isBrowser then @skip() else dispatchCountOnce = 0 dispatchCountTotal = 0 regA.on 'once', ()-> dispatchCountOnce++ regA.on 'total', ()-> dispatchCountTotal++ SimplyBind('prop1').of(objectA) .once.to('event:once').of(regA) .and.to('event:total').of(regA) expect(dispatchCountOnce).to.equal(0) expect(dispatchCountTotal).to.equal(1) objectA.prop1 += 1 expect(dispatchCountOnce).to.equal(1) expect(dispatchCountTotal).to.equal(2) objectA.prop1 += 1 expect(dispatchCountOnce).to.equal(1) expect(dispatchCountTotal).to.equal(3) restartSandbox() test "Adding a transform shouldn't force an update to a one-time-binding", ()-> objectA.prop = 1 objectB.prop = 2 SimplyBind('prop').of(objectA) .once.to('prop').of(objectB) .transform (value)-> value objectA.prop = 'anotherValue' expect(objectB.prop).to.equal 'anotherValue' objectA.prop = 'diffValue' expect(objectB.prop).to.equal 'anotherValue' restartSandbox()