|
| 1 | +import mitt from '../src'; |
| 2 | +import chai, { expect } from 'chai'; |
| 3 | +import { spy } from 'sinon'; |
| 4 | +import sinonChai from 'sinon-chai'; |
| 5 | +chai.use(sinonChai); |
| 6 | + |
| 7 | + |
| 8 | +describe('mitt', () => { |
| 9 | + it('should be a function', () => { |
| 10 | + expect(mitt).to.be.a('function'); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('mitt#', () => { |
| 14 | + let events, inst; |
| 15 | + |
| 16 | + beforeEach( () => { |
| 17 | + events = {}; |
| 18 | + inst = mitt(events); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('on()', () => { |
| 22 | + it('should be a function', () => { |
| 23 | + expect(inst) |
| 24 | + .to.have.property('on') |
| 25 | + .that.is.a('function'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should register handler for new type', () => { |
| 29 | + let foo = () => {}; |
| 30 | + inst.on('foo', foo); |
| 31 | + |
| 32 | + expect(events).to.have.property('foo').that.deep.equals([foo]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should append handler for existing type', () => { |
| 36 | + let foo = () => {}; |
| 37 | + let bar = () => {}; |
| 38 | + inst.on('foo', foo); |
| 39 | + inst.on('foo', foo); |
| 40 | + inst.on('foo', bar); |
| 41 | + |
| 42 | + expect(events).to.have.property('foo').that.deep.equals([foo, foo, bar]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should normalize case', () => { |
| 46 | + let foo = () => {}; |
| 47 | + inst.on('FOO', foo); |
| 48 | + inst.on('Bar', foo); |
| 49 | + inst.on('baz:baT!', foo); |
| 50 | + |
| 51 | + expect(events).to.have.property('foo').that.deep.equals([foo]); |
| 52 | + expect(events).to.have.property('bar').that.deep.equals([foo]); |
| 53 | + expect(events).to.have.property('baz:bat!').that.deep.equals([foo]); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('off()', () => { |
| 58 | + it('should be a function', () => { |
| 59 | + expect(inst) |
| 60 | + .to.have.property('off') |
| 61 | + .that.is.a('function'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should remove handler for type', () => { |
| 65 | + let foo = () => {}; |
| 66 | + events.foo = [foo]; |
| 67 | + inst.off('foo', foo); |
| 68 | + |
| 69 | + expect(events).to.have.property('foo').that.is.empty; |
| 70 | + }); |
| 71 | + |
| 72 | + it('should remove only one handler for dupes', () => { |
| 73 | + let foo = () => {}; |
| 74 | + events.foo = [foo, foo]; |
| 75 | + |
| 76 | + inst.off('foo', foo); |
| 77 | + expect(events).to.have.property('foo').that.deep.equals([foo]); |
| 78 | + |
| 79 | + inst.off('foo', foo); |
| 80 | + expect(events).to.have.property('foo').that.is.empty; |
| 81 | + }); |
| 82 | + |
| 83 | + it('should normalize case', () => { |
| 84 | + let foo = () => {}; |
| 85 | + events.foo = [foo]; |
| 86 | + events.bar = [foo]; |
| 87 | + events['baz:bat!'] = [foo]; |
| 88 | + |
| 89 | + inst.off('FOO', foo); |
| 90 | + inst.off('Bar', foo); |
| 91 | + inst.off('baz:baT!', foo); |
| 92 | + |
| 93 | + expect(events).to.have.property('foo').that.is.empty; |
| 94 | + expect(events).to.have.property('bar').that.is.empty; |
| 95 | + expect(events).to.have.property('baz:bat!').that.is.empty; |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('emit()', () => { |
| 100 | + it('should be a function', () => { |
| 101 | + expect(inst) |
| 102 | + .to.have.property('emit') |
| 103 | + .that.is.a('function'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should invoke handler for type', () => { |
| 107 | + let foo = spy(), |
| 108 | + event = {}; |
| 109 | + events.foo = [foo]; |
| 110 | + |
| 111 | + inst.emit('foo', event); |
| 112 | + |
| 113 | + expect(foo) |
| 114 | + .to.have.been.calledOnce |
| 115 | + .and.calledWithExactly(event); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should ignore case', () => { |
| 119 | + let foo = spy(), |
| 120 | + event = {}; |
| 121 | + events.foo = [foo]; |
| 122 | + |
| 123 | + inst.emit('FOO', event); |
| 124 | + inst.emit('Foo', event); |
| 125 | + |
| 126 | + expect(foo) |
| 127 | + .to.have.been.calledTwice |
| 128 | + .and.always.calledWithExactly(event); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should invoke * handlers', () => { |
| 132 | + let star = spy(), |
| 133 | + event = {}; |
| 134 | + events['*'] = [star]; |
| 135 | + |
| 136 | + inst.emit('foo', event); |
| 137 | + inst.emit('bar', event); |
| 138 | + |
| 139 | + expect(star) |
| 140 | + .to.have.been.calledTwice |
| 141 | + .and.always.calledWithExactly(event); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments