@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
249 lines (153 loc) • 6.25 kB
text/coffeescript
suite ".transformSelf()", ()->
suiteSetup(restartSandbox)
suite "Will transform any received value", ()->
test "ObjectProp", ()->
object = 'prop':'value'
SimplyBind('prop').of(object)
.transformSelf (value)-> value.toUpperCase()
expect(object.prop).to.equal 'VALUE'
object.prop = 'anotherValue'
expect(object.prop).to.equal 'ANOTHERVALUE'
test "DOMText", ()-> if not isBrowser then else
regA.textContent = 'value'
SimplyBind('textContent').of(regA)
.transformSelf (value)-> value.toUpperCase()
expect(regA.textContent).to.equal 'VALUE'
SimplyBind('textContent').of(regA).update('anotherValue')
expect(regA.textContent).to.equal 'ANOTHERVALUE'
restartSandbox()
test "DOMAttr", ()-> if not isBrowser then else
regA.setAttribute 'someattr', 'value'
SimplyBind('attr:someattr').of(regA)
.transformSelf (value)-> value.toUpperCase()
expect(regA.getAttribute 'someattr').to.equal 'VALUE'
SimplyBind('attr:someattr').of(regA).update('anotherValue')
expect(regA.getAttribute 'someattr').to.equal 'ANOTHERVALUE'
restartSandbox()
test "DOMValue", ()-> if not isBrowser then else
inputA.value = 'value'
SimplyBind('value').of(inputA)
.transformSelf (value)-> value.toUpperCase()
expect(inputA.value).to.equal 'VALUE'
inputA.value = 'anotherValue'
inputA.emit 'change'
expect(inputA.value).to.equal 'ANOTHERVALUE'
restartSandbox()
test "Array", ()->
originalArray = objectA.list
originalReceivedArray = null
receivedArray = null
SimplyBind('array:list').of(objectA).transformSelf ()-> ['another array']
.to (array)-> receivedArray = array
expect(Object.values receivedArray).not.to.eql originalArray
expect(Object.values receivedArray).to.eql ['another array']
originalReceivedArray = receivedArray
originalArray.sort()
expect(Object.values receivedArray).not.to.eql Object.values originalArray
expect(receivedArray).to.equal originalReceivedArray
restartSandbox()
test "Function", ()->
dispatcher = 'prop':'value'
receiver = 'prop':'value'
fn = (value)-> value
SimplyBind(fn).transformSelf (value)-> value?.toUpperCase()
SimplyBind('prop').of(dispatcher)
.to(fn)
.chainTo('prop').of(receiver)
expect(dispatcher.prop).to.equal 'value'
expect(receiver.prop).to.equal 'VALUE'
dispatcher.prop = 'anotherValue'
expect(dispatcher.prop).to.equal 'anotherValue'
expect(receiver.prop).to.equal 'ANOTHERVALUE'
test "Event", ()->
dispatcher = 'prop':eventEmitterA
receiver = 'prop':'N/A'
eventEmitterA.type = 'someEvent'
eventEmitterB.type = 'someEvent'
SimplyBind('event:someEvent').of(eventEmitterA)
.transformSelf (event)-> event?.type or event
SimplyBind('prop').of(dispatcher)
.to('event:someEvent').of(eventEmitterA)
.chainTo('prop').of(receiver)
expect(dispatcher.prop).to.equal eventEmitterA
expect(receiver.prop).to.equal 'N/A'
receiver.prop = 'reset'
dispatcher.prop = eventEmitterB
expect(dispatcher.prop).to.equal eventEmitterB
expect(receiver.prop).to.equal 'someEvent'
test "Proxy", ()->
invokeCount = 0
obj = base:10, test:(a,b)-> (a+b)*
SimplyBind('func:test').of(obj)
.transformSelf (args)-> [args[0]/3, args[1]/3]
.to (value)->
expect(value.result).to.equal 50
expect(value.args).to.eql [2,3]
invokeCount++
obj.test(6,9)
expect(invokeCount).to.equal(1)
suite "Will not transform value on bind if SimplyBind.options.updateOnBind is off", ()->
suiteSetup ()-> SimplyBind.defaultOptions.updateOnBind = false
suiteTeardown ()-> SimplyBind.defaultOptions.updateOnBind = true
test "ObjectProp", ()->
object = 'prop':'value'
SimplyBind('prop').of(object)
.transformSelf (value)-> value.toUpperCase()
expect(object.prop).to.equal 'value'
object.prop = 'anotherValue'
expect(object.prop).to.equal 'ANOTHERVALUE'
test "DOMText", ()-> if not isBrowser then else
regA.textContent = 'value'
SimplyBind('textContent').of(regA)
.transformSelf (value)-> value.toUpperCase()
expect(regA.textContent).to.equal 'value'
SimplyBind('textContent').of(regA).update('anotherValue')
expect(regA.textContent).to.equal 'ANOTHERVALUE'
restartSandbox()
test "DOMAttr", ()-> if not isBrowser then else
regA.setAttribute 'someattr', 'value'
SimplyBind('attr:someattr').of(regA)
.transformSelf (value)-> value.toUpperCase()
expect(regA.getAttribute 'someattr').to.equal 'value'
SimplyBind('attr:someattr').of(regA).update('anotherValue')
expect(regA.getAttribute 'someattr').to.equal 'ANOTHERVALUE'
restartSandbox()
test "DOMValue", ()-> if not isBrowser then else
inputA.value = 'value'
SimplyBind('value').of(inputA)
.transformSelf (value)-> value.toUpperCase()
expect(inputA.value).to.equal 'value'
inputA.value = 'anotherValue'
inputA.emit 'change'
expect(inputA.value).to.equal 'ANOTHERVALUE'
restartSandbox()
test "Function", ()->
dispatcher = 'prop':'value'
receiver = 'prop':'value'
fn = (value)-> value
SimplyBind(fn).transformSelf (value)-> value?.toUpperCase()
SimplyBind('prop').of(dispatcher)
.to(fn)
.chainTo('prop').of(receiver)
expect(dispatcher.prop).to.equal 'value'
expect(receiver.prop).to.be.undefined
dispatcher.prop = 'anotherValue'
expect(dispatcher.prop).to.equal 'anotherValue'
expect(receiver.prop).to.equal 'ANOTHERVALUE'
restartSandbox()
test "Event", ()->
dispatcher = 'prop':eventEmitterA
receiver = 'prop':'N/A'
eventEmitterA.type = 'someEvent'
eventEmitterB.type = 'someEvent'
SimplyBind('event:someEvent').of(eventEmitterA)
.transformSelf (event)-> event?.type or event
SimplyBind('prop').of(dispatcher)
.to('event:someEvent').of(eventEmitterA)
.chainTo('prop').of(receiver)
expect(dispatcher.prop).to.equal eventEmitterA
expect(receiver.prop).to.equal 'N/A'
receiver.prop = 'reset'
dispatcher.prop = eventEmitterB
expect(dispatcher.prop).to.equal eventEmitterB
expect(receiver.prop).to.equal 'someEvent'