@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
105 lines (75 loc) • 3.23 kB
text/coffeescript
suite "DOMValue", ()->
suiteSetup ()-> if not isBrowser then @skip()
test "Dispatching an event isn't necessary when updating the value for a binding's .set()/.update() method", ()->
binding = SimplyBind('value').of(inputA).to('prop2').of(objectA)
binding.set 'changed from binding instance'
expect(objectA.prop2).to.equal 'changed from binding instance'
test "When passing an element collection to .of() in the form of a jQuery object/NodeList/HTMLCollection, only the first element will be used", ()->
SimplyBind('prop1').of(objectA)
.to('value').of $inputA
.and.to('value').of document.querySelectorAll('#input2')
objectA.prop1 = 'updated'
expect($inputA.val()).to.equal 'updated'
expect($inputB.val()).to.equal 'updated'
restartSandbox()
test "A DOM select field with multiple options shouldn't be interpreted as an iterable interface", ()->
origLog = console.warn
console.warn = chai.spy()
SimplyBind('value').of(select)
expect(console.warn).not.to.have.been.called()
console.warn = origLog
restartSandbox()
test "A change event will be dispatched upon value update if SimplyBind.settings.dispatchEvents is on", ()->
invokeCountInput = 0
invokeCountTextarea = 0
invokeCountSelect = 0
invokeCountRadio = 0
SimplyBind.settings.dispatchEvents = true
SimplyBind.defaultOptions.updateEvenIfSame = true
SimplyBind('prop4').of(objectA)
.to('value').of $inputA
.and.to('value').of $textarea
.and.to('value').of $select
.and.to('checked').of $radioFields
$inputA[0].on 'change', ()-> invokeCountInput++
$textarea[0].on 'change', ()-> invokeCountTextarea++
$select[0].on 'change', ()-> invokeCountSelect++
$radioFields.each ()-> @on 'change', ()-> invokeCountRadio++
objectA.prop4 = 'radioA'
objectA.prop4 = 'radioB'
objectA.prop4 = 'radioB'
expect(invokeCountInput).to.equal 3
expect(invokeCountTextarea).to.equal 3
expect(invokeCountSelect).to.equal 3
expect(invokeCountRadio).to.equal 2
SimplyBind.settings.dispatchEvents = false
SimplyBind.defaultOptions.updateEvenIfSame = false
restartSandbox()
test "Changes from user input shouldn't move the cursor position to the end of the input's text", ()->
objectA.prop = 'abc123'
SimplyBind('prop').of(objectA)
.to('value').of(inputA)
.and.to('value').of(inputB)
SimplyBind('value').of(inputB).transformSelf (value)-> value.toUpperCase()
expect(inputA.value).to.equal 'abc123'
expect(inputB.value).to.equal 'ABC123'
inputA.focus()
return @skip() if inputA.selectionStart is 0
expect(inputA.selectionStart).to.equal objectA.prop.length
inputA.setSelectionRange(3,3)
expect(inputA.selectionStart).to.equal 3
inputA.value = 'abc1234'
inputA.setSelectionRange(3,3)
inputA.emit('input')
expect(inputA.selectionStart).to.equal 3
expect(inputA.value).to.equal 'abc1234'
inputB.focus()
expect(inputB.selectionStart).to.equal objectA.prop.length
inputB.setSelectionRange(4,4)
expect(inputB.selectionStart).to.equal 4
inputB.value = 'abc1234'
inputB.setSelectionRange(4,4)
inputB.emit('input')
expect(inputB.selectionStart).to.equal 4
expect(inputB.value).to.equal 'ABC1234'
restartSandbox()