@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
153 lines (93 loc) • 5.47 kB
text/coffeescript
suite "Cached Bindings", ()->
test "A cached version of the binding should be returned when re-creating an existing one", ()->
# ==== ObjectProp =================================================================================
expect(SimplyBind('prop1').of(objectA)._.ID)
.to.equal(SimplyBind('prop1').of(objectA)._.ID)
expect(SimplyBind('prop1').of(objectA).to('prop1').of(objectB)._.subs[0].ID)
.to.equal(SimplyBind('prop1').of(objectB)._.ID)
if isBrowser
# ==== DOMAttr =================================================================================
expect(SimplyBind('attr:someattr').of(regA)._.ID)
.to.equal(SimplyBind('attr:someattr').of(regA)._.ID)
expect(SimplyBind('attr:someattr').of(regA).to('attr:someattr').of(regB)._.subs[0].ID)
.to.equal(SimplyBind('attr:someattr').of(regB)._.ID)
# ==== DOMValue =================================================================================
expect(SimplyBind('value').of(inputA)._.ID)
.to.equal(SimplyBind('value').of(inputA)._.ID)
expect(SimplyBind('value').of(inputA).to('value').of(inputB)._.subs[0].ID)
.to.equal(SimplyBind('value').of(inputB)._.ID)
# ==== DOMCheckbox Single =================================================================================
expect(SimplyBind('checked').of(checkboxA)._.ID)
.to.equal(SimplyBind('checked').of(checkboxA)._.ID)
expect(SimplyBind('checked').of(checkboxA).to('checked').of(checkboxB)._.subs[0].ID)
.to.equal(SimplyBind('checked').of(checkboxB)._.ID)
# ==== DOMCheckbox =================================================================================
expect(SimplyBind('checked').of($checkboxFields)._.ID)
.to.equal(SimplyBind('checked').of($checkboxFields)._.ID)
# ==== DOMRadio =================================================================================
expect(SimplyBind('checked').of($radioFields)._.ID)
.to.equal(SimplyBind('checked').of($radioFields)._.ID)
# ==== DOMText =================================================================================
expect(SimplyBind('textContent').of(regA)._.ID)
.to.equal(SimplyBind('textContent').of(regA)._.ID)
expect(SimplyBind('textContent').of(regA).to('textContent').of(regB)._.subs[0].ID)
.to.equal(SimplyBind('textContent').of(regB)._.ID)
# ==== Event =================================================================================
expect(SimplyBind('event:someEvent').of(eventEmitterA)._.ID)
.to.equal(SimplyBind('event:someEvent').of(eventEmitterA)._.ID)
expect(SimplyBind('event:someEvent').of(eventEmitterA).to('event:someEvent').of(eventEmitterB)._.subs[0].ID)
.to.equal(SimplyBind('event:someEvent').of(eventEmitterB)._.ID)
# ==== Array =================================================================================
expect(SimplyBind('array:list').of(objectA)._.ID)
.to.equal(SimplyBind('array:list').of(objectA)._.ID)
expect(SimplyBind('array:list').of(objectA).to('array:list').of(objectB)._.subs[0].ID)
.to.equal(SimplyBind('array:list').of(objectB)._.ID)
# ==== Function =================================================================================
expect(SimplyBind(fnA)._.ID)
.to.equal(SimplyBind(fnA)._.ID)
expect(SimplyBind(fnA).to(fnB)._.subs[0].ID)
.to.equal(SimplyBind(fnB)._.ID)
restartSandbox()
test "When binding an object prop with a placeholder indicator in the selector for an existing binding that didn't have indicators, placeholders will be rescanned", ()->
dispatcher = 'prop':'This is an {{size}} size shirt'
receiver = 'prop':''
SimplyBind('prop').of(dispatcher)
.to('prop').of(receiver)
expect(receiver.prop).to.equal(dispatcher.prop)
expect(dispatcher.prop).to.equal 'This is an {{size}} size shirt'
SimplyBind('prop').of(dispatcher)
.to('prop.size').of(receiver)
dispatcher.prop = 'XXL'
expect(dispatcher.prop).to.equal 'XXL'
expect(receiver.prop).to.equal 'This is an XXL size shirt'
test "When re-binding a binding with placeholder where the global placeholders regEx has changed to a diff regEx from present in the original binding, the binding's regEx shouldn't change", ()->
dispatcher = 'noun':'', 'adjective':''
receiver = 'prop':'This {{noun}} is **adjective**'
SimplyBind('noun').of(dispatcher)
.to('prop.noun').of(receiver)
dispatcher.noun = 'test'
expect(receiver.prop).to.equal 'This test is **adjective**'
SimplyBind.settings.placeholder = ['**', '**']
SimplyBind('adjective').of(dispatcher)
.to('prop.adjective').of(receiver)
dispatcher.adjective = 'good'
expect(receiver.prop).to.equal 'This test is **adjective**'
dispatcher.noun = 'suite'
expect(receiver.prop).to.equal 'This suite is **adjective**'
SimplyBind.settings.placeholder = ['{{', '}}']
test "When re-binding an object prop that was deleted manually make sure to re-make the property a live one", ()->
objectA.prop = 1
SimplyBind('prop').of(objectA)
.to('prop').of(objectB)
expect(objectB.prop).to.equal(objectA.prop)
objectA.prop = 2
expect(objectB.prop).to.equal(objectA.prop)
delete objectB.prop
objectA.prop = 3
expect(objectB.prop).not.to.equal(objectA.prop)
SimplyBind('prop').of(objectA)
.to('prop').of(objectB)
expect(objectB.prop).to.equal(objectA.prop)
objectA.prop = 4
expect(objectB.prop).to.equal(objectA.prop)
restartSandbox()