UNPKG

@danielkalen/simplybind

Version:

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

213 lines (142 loc) 5.21 kB
suite ".condition()", ()-> suiteSetup(restartSandbox) test "Will pass the value to the condition before updating the dependent and will update the dep only if the return value is truthy", ()-> shouldAllowUpdate = false objectA.prop1 = objectB.prop1 = 0 SimplyBind('prop1').of(objectA) .to('prop1').of(objectB) .condition ()-> shouldAllowUpdate objectA.prop1 = 1 expect(objectB.prop1).to.equal 0 objectA.prop1 = 2 expect(objectB.prop1).to.equal 0 shouldAllowUpdate = true objectA.prop1 = 3 expect(objectB.prop1).to.equal 3 restartSandbox() test "Will update the dependent upon initial bind regardless of the condition unless SimplyBind.options.updateOnBind is on", ()-> shouldAllowUpdate = false objectA.prop1 = objectA.prop2 = 1 objectB.prop1 = objectB.prop2 = 0 SimplyBind('prop1').of(objectA) .to('prop1').of(objectB) .condition ()-> shouldAllowUpdate expect(objectA.prop1).to.equal 1 expect(objectB.prop1).to.equal 1 objectA.prop1 = 2 expect(objectB.prop1).to.equal 1 SimplyBind.defaultOptions.updateOnBind = false SimplyBind('prop2').of(objectA) .to('prop2').of(objectB) .condition ()-> shouldAllowUpdate expect(objectA.prop2).to.equal 1 expect(objectB.prop2).to.equal 0 SimplyBind.defaultOptions.updateOnBind = true restartSandbox() test "Will receive the subject object as the third argument", ()-> dispatcher = 'prop':1 receivers = [objectA, objectB, objectC] index = 0 SimplyBind('prop').of(dispatcher) .to('prop').of(objectA) .and.to('prop').of(objectB) .and.to('prop').of(objectC) .conditionAll (current, prev, target)-> expect(target).to.equal(receivers[index++]) return true dispatcher.prop++ expect(index).to.equal(3) restartSandbox() test "Condition will be added only to the last-proxied dependent", ()-> shouldAllowUpdate = false objectA.prop1 = objectB.prop1 = objectC.prop1 = 0 SimplyBind('prop1').of(objectA) .to('prop1').of(objectB).condition ()-> shouldAllowUpdate .and.to('prop1').of(objectC) objectA.prop1 = 1 expect(objectB.prop1).to.equal 0 expect(objectC.prop1).to.equal 1 objectA.prop1 = 2 expect(objectB.prop1).to.equal 0 expect(objectC.prop1).to.equal 2 shouldAllowUpdate = true objectA.prop1 = 3 expect(objectB.prop1).to.equal 3 expect(objectC.prop1).to.equal 3 restartSandbox() test "The condition will be added both ways if declared after the .bothWays() call", ()-> shouldAllowUpdate = false objectA.prop1 = objectB.prop1 = 0 SimplyBind('prop1').of(objectA) .to('prop1').of(objectB).bothWays() .condition ()-> shouldAllowUpdate objectA.prop1 = 1 expect(objectA.prop1).to.equal 1 expect(objectB.prop1).to.equal 0 objectB.prop1 = 2 expect(objectA.prop1).to.equal 1 expect(objectB.prop1).to.equal 2 shouldAllowUpdate = true objectA.prop1 = 3 expect(objectA.prop1).to.equal 3 expect(objectB.prop1).to.equal 3 objectB.prop1 = 4 expect(objectA.prop1).to.equal 4 expect(objectB.prop1).to.equal 4 restartSandbox() test "The condition will be added both ways if declared before the .bothWays() call", ()-> shouldAllowUpdate = false objectA.prop1 = objectB.prop1 = 0 SimplyBind('prop1').of(objectA) .to('prop1').of(objectB) .condition ()-> shouldAllowUpdate .bothWays() objectA.prop1 = 1 expect(objectA.prop1).to.equal 1 expect(objectB.prop1).to.equal 0 objectB.prop1 = 2 expect(objectA.prop1).to.equal 1 expect(objectB.prop1).to.equal 2 shouldAllowUpdate = true objectA.prop1 = 3 expect(objectA.prop1).to.equal 3 expect(objectB.prop1).to.equal 3 objectB.prop1 = 4 expect(objectA.prop1).to.equal 4 expect(objectB.prop1).to.equal 4 restartSandbox() test "If a the dependent has a transform and a condition, the new value will be run through the transform prior to testing the condition", ()-> invokeCount = 0 shouldAllowUpdate = true SimplyBind('prop1').of(objectA) .to('prop1').of(objectB) .transform (val)-> invokeCount++; val .condition ()-> shouldAllowUpdate expect(objectB.prop1).to.equal(objectA.prop1) expect(invokeCount).to.equal 1 objectA.prop1 = 2 expect(objectB.prop1).to.equal(objectA.prop1) expect(invokeCount).to.equal 2 shouldAllowUpdate = false objectA.prop1 = 3 expect(objectB.prop1).not.to.equal(objectA.prop1) expect(invokeCount).to.equal 3 objectA.prop1 = 4 expect(objectB.prop1).not.to.equal(objectA.prop1) expect(invokeCount).to.equal 4 restartSandbox() test "If a binding has a placeholder, the condition will be used only for that placeholder", ()-> dispatcher = 'prop':'' receiver = 'prop':'This {{noun}} works' lastPassedNoun = null shouldAllowUpdate = true SimplyBind('prop').of(dispatcher) .to('prop.noun').of(receiver) .condition (noun)-> lastPassedNoun=noun; return shouldAllowUpdate dispatcher.prop = 'test1' expect(lastPassedNoun).to.equal 'test1' expect(receiver.prop).to.equal 'This test1 works' shouldAllowUpdate = false dispatcher.prop = 'test2' expect(lastPassedNoun).to.equal 'test2' expect(receiver.prop).to.equal 'This test1 works'