1 | assert = require 'assert'
|
2 |
|
3 | Keychain = require '../src/keychain'
|
4 | keychain = new Keychain()
|
5 |
|
6 | describe 'Event Handler', ->
|
7 |
|
8 | it 'should listen for events', (done) ->
|
9 | keychain.on 'simple', ->
|
10 | done()
|
11 | keychain._trigger('simple')
|
12 |
|
13 | it 'should pass data to events', (done) ->
|
14 | keychain.on 'withData', (data) ->
|
15 | assert.equal(data, true)
|
16 | done()
|
17 | keychain._trigger('withData', true)
|
18 |
|
19 | it 'should remove events', ->
|
20 | keychain.on 'remove', 'id', ->
|
21 | throw new Error 'Should not be fired'
|
22 | keychain.off('remove', 'id')
|
23 | keychain._trigger('remove')
|
24 |
|
25 | it 'should remove all events when no ID is passed to off', ->
|
26 | keychain.on 'removeAll', 'id_1', ->
|
27 | throw new Error 'Should not be fired'
|
28 | keychain.on 'removeAll', 'id_2', ->
|
29 | throw new Error 'Should not be fired'
|
30 | keychain.off('removeAll')
|
31 | keychain._trigger('removeAll')
|
32 |
|
33 | it 'should use .one() to listen for an event only once', ->
|
34 | keychain.one 'runOnce', (data) ->
|
35 | assert.equal(data, 1)
|
36 | keychain._trigger('runOnce', 1)
|
37 | keychain._trigger('runOnce', 2)
|
38 |
|
39 | it 'should return the ID of the event', ->
|
40 | id = keychain.on 'returnId', 'id_1', ->
|
41 | assert.equal(id, 'id_1')
|
42 | id = keychain.one 'returnId', 'id_2', ->
|
43 | assert.equal(id, 'id_2')
|