nebulab-dropbox
Version:
Client library for the Dropbox API
145 lines (116 loc) • 5.02 kB
text/coffeescript
describe 'Dropbox.Util.EventSource', ->
beforeEach ->
= new Dropbox.Util.EventSource
= new Dropbox.Util.EventSource cancelable: true
# 3 listeners, 1 and 2 are already hooked up
= null
= (event) =>
= event
.returnValue
.returnValue = true
.addListener
.addListener
= null
= (event) =>
= event
.returnValue
.returnValue = false
.addListener
.addListener
= null
= (event) =>
= event
.returnValue
.returnValue = true
describe '#addListener', ->
it 'adds a new listener', ->
.addListener
expect(._listeners).to.deep.
equal [, , ]
it 'does not add an existing listener', ->
.addListener
expect(._listeners).to.deep.equal [, ]
it 'is idempotent', ->
.addListener
.addListener
expect(._listeners).to.deep.
equal [, , ]
it 'refuses to add non-functions', ->
expect(=> .addListener 42).to.throw(TypeError, /listener type/)
describe '#removeListener', ->
it 'does nothing for a non-existing listener', ->
.removeListener
expect(._listeners).to.deep.equal [, ]
it 'removes a listener at the end of the queue', ->
.removeListener
expect(._listeners).to.deep.equal []
it 'removes a listener at the beginning of the queue', ->
.removeListener
expect(._listeners).to.deep.equal []
it 'removes a listener at the middle of the queue', ->
.addListener
.removeListener
expect(._listeners).to.deep.equal [, ]
it 'removes all the listeners', ->
.removeListener
.removeListener
expect(._listeners).to.deep.equal []
describe 'without ES5 Array#indexOf', ->
beforeEach ->
._listeners.indexOf = null
afterEach ->
delete ._listeners.indexOf
assertArraysEqual = (array1, array2) ->
expect(array1.length).to.equal(array2.length)
for i in [0...array1.length]
expect(array1[i]).to.equal(array2[i])
it 'does nothing for a non-existing listener', ->
.removeListener
assertArraysEqual ._listeners, [, ]
it 'removes a listener at the end of the queue', ->
.removeListener
assertArraysEqual ._listeners, []
it 'removes a listener at the beginning of the queue', ->
.removeListener
assertArraysEqual ._listeners, []
it 'removes a listener at the middle of the queue', ->
.addListener
.removeListener
assertArraysEqual ._listeners, [, ]
it 'removes all the listeners', ->
.removeListener
.removeListener
assertArraysEqual ._listeners, []
describe '#dispatch', ->
beforeEach ->
= { answer: 42 }
it 'passes event to all listeners', ->
.dispatch
expect().to.equal
expect().to.equal
expect().to.equal null
describe 'on non-cancelable events', ->
beforeEach ->
.addListener
= .dispatch
it 'calls all the listeners', ->
expect().to.equal
expect().to.equal
expect().to.equal
it 'ignores the listener return values', ->
expect().to.equal true
describe 'on cancelable events', ->
beforeEach ->
.addListener
= .dispatch
it 'stops calling listeners after cancelation', ->
expect().to.equal
expect().to.equal
expect().to.equal null
it 'reports cancelation', ->
expect().to.equal false
it 'calls all listeners if no cancelation occurs', ->
.returnValue = true
= .dispatch
expect().to.equal true
expect().to.equal