@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
163 lines (104 loc) • 5.43 kB
text/coffeescript
suite "Errors & Warnings", ()->
suiteSetup(restartSandbox)
setup ()-> sinon.stub(console, 'warn') unless console.warn.restore
teardown ()-> console.warn.restore()
test "Warning when binding a property of a jQuery object containing multiple els", ()-> if not isBrowser then @skip() else
SimplyBind('value').of $('input')
expect(console.warn.calledOnce).to.be.true
test "Warning when binding a property of a jQuery/NodeList/HTMLCollection object containing zero els", ()-> if not isBrowser then @skip() else
expect ()-> SimplyBind('value').of $('nonexistent')
.to.throw()
expect ()-> SimplyBind('value').of document.querySelectorAll('nonexistent')
.to.throw()
expect ()-> SimplyBind('value').of document.getElementsByTagName('nonexistent')
.to.throw()
test "Warning when binding a property of a jQuery object containing zero els", ()-> if not isBrowser then @skip() else
expect ()-> SimplyBind('value').of $('input[type="nonexistent"]')
.to.throw()
test "Warning when binding a property of a NodeList/HTMLCollection containing multiple els", ()-> if not isBrowser then @skip() else
SimplyBind('value').of document.querySelectorAll('input')
expect(console.warn.calledOnce).to.be.true
test "No Warning when binding 'checked' of a jQuery object containing multiple radio/checkbox inputs", ()-> if not isBrowser then @skip() else
SimplyBind('checked').of $radioFields
expect(console.warn.called).to.be.false
SimplyBind('value').of $radioFields
expect(console.warn.callCount).to.equal(1)
test "No Warning when binding 'checked' of a NodeList/HTMLCollection/Array containing multiple radio/checkbox inputs", ()-> if not isBrowser then @skip() else
SimplyBind('checked').of $radioFields.toArray()
expect(console.warn.called).to.be.false
SimplyBind('checked').of radioFields
expect(console.warn.called).to.be.false
SimplyBind('value').of $radioFields.toArray()
SimplyBind('value').of radioFields
expect(console.warn.callCount).to.equal(2)
test "Warning when binding a property of a NodeList/HTMLCollection/Array/jQuery containing elements with mixed types", ()-> if not isBrowser then @skip() else
SimplyBind('checked').of $radioFields.add($checkboxFields).toArray()
expect(console.warn.callCount).to.equal(1)
SimplyBind('checked').of $radioFields.add($checkboxFields)
expect(console.warn.callCount).to.equal(2)
SimplyBind('checked').of document.querySelectorAll 'input'
expect(console.warn.callCount).to.equal(3)
test "Warning when creating a binding and passing a non-function object to .transform/.condition/All", ()->
binding = SimplyBind('prop1').of(objectA).to('prop1').of(objectB)
timesCalled = 0
binding.transform []
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transformAll []
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform {}
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform new Date()
expect(console.warn.callCount).to.equal(timesCalled += 1)
if isBrowser
binding.transform $('<div />')[0]
expect(console.warn.callCount).to.equal(timesCalled += 1)
if window.Map and window.Set and window.Symbol
binding.transform new Map()
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform new WeakMap()
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform new Set()
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform new WeakSet()
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform Symbol(0)
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform 's'
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform 0
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform true
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform null
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.transform undefined
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.condition []
expect(console.warn.callCount).to.equal(timesCalled += 1)
binding.conditionAll []
expect(console.warn.callCount).to.equal(timesCalled += 1)
restartSandbox()
test "Warning when passed a non-number argument selector to an event binding", ()->
SimplyBind('event:someEvent#value')
SimplyBind('event:someEvent#15')
SimplyBind('event:someEvent#1aos95')
expect(console.warn.callCount).to.equal(1)
test "No warnings should be thrown when SimplyBind.options.silent is on", ()->
SimplyBind.settings.silent = true
SimplyBind('event:someEvent#value')
SimplyBind('prop3').of(objectA).to('prop3').of(objectB).transform []
expect(console.warn.called).to.be.false
SimplyBind.settings.silent = false
test "Errors should still be thrown when SimplyBind.options.silent is on", ()->
SimplyBind.settings.silent = true
expect ()-> SimplyBind ''
.to.throw()
SimplyBind.settings.silent = false
test "No errors should be thrown when scanning for placeholders on a non-string object", ()->
dispatcher = 'prop':'value'
receiver = 'prop':null
expect(()->
SimplyBind('prop').of(dispatcher)
.to('prop.placeholder').of(receiver)
).not.to.throw()
expect(receiver.prop).not.to.equal('value')