diff options
| author | Jason Miller <[email protected]> | 2017-01-14 21:48:51 -0500 |
|---|---|---|
| committer | Jason Miller <[email protected]> | 2017-01-14 21:48:51 -0500 |
| commit | e77a42cdeca3f4c484f82a335a8a2bb8382d4fcc (patch) | |
| tree | 612f883228b2db7343df71151427480f82b35206 /test | |
Initial commit! 🚀
Diffstat (limited to 'test')
| -rw-r--r-- | test/index.js | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..8055544 --- /dev/null +++ b/test/index.js @@ -0,0 +1,145 @@ +import mitt from '../src'; +import chai, { expect } from 'chai'; +import { spy } from 'sinon'; +import sinonChai from 'sinon-chai'; +chai.use(sinonChai); + + +describe('mitt', () => { + it('should be a function', () => { + expect(mitt).to.be.a('function'); + }); + + describe('mitt#', () => { + let events, inst; + + beforeEach( () => { + events = {}; + inst = mitt(events); + }); + + describe('on()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('on') + .that.is.a('function'); + }); + + it('should register handler for new type', () => { + let foo = () => {}; + inst.on('foo', foo); + + expect(events).to.have.property('foo').that.deep.equals([foo]); + }); + + it('should append handler for existing type', () => { + let foo = () => {}; + let bar = () => {}; + inst.on('foo', foo); + inst.on('foo', foo); + inst.on('foo', bar); + + expect(events).to.have.property('foo').that.deep.equals([foo, foo, bar]); + }); + + it('should normalize case', () => { + let foo = () => {}; + inst.on('FOO', foo); + inst.on('Bar', foo); + inst.on('baz:baT!', foo); + + expect(events).to.have.property('foo').that.deep.equals([foo]); + expect(events).to.have.property('bar').that.deep.equals([foo]); + expect(events).to.have.property('baz:bat!').that.deep.equals([foo]); + }); + }); + + describe('off()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('off') + .that.is.a('function'); + }); + + it('should remove handler for type', () => { + let foo = () => {}; + events.foo = [foo]; + inst.off('foo', foo); + + expect(events).to.have.property('foo').that.is.empty; + }); + + it('should remove only one handler for dupes', () => { + let foo = () => {}; + events.foo = [foo, foo]; + + inst.off('foo', foo); + expect(events).to.have.property('foo').that.deep.equals([foo]); + + inst.off('foo', foo); + expect(events).to.have.property('foo').that.is.empty; + }); + + it('should normalize case', () => { + let foo = () => {}; + events.foo = [foo]; + events.bar = [foo]; + events['baz:bat!'] = [foo]; + + inst.off('FOO', foo); + inst.off('Bar', foo); + inst.off('baz:baT!', foo); + + expect(events).to.have.property('foo').that.is.empty; + expect(events).to.have.property('bar').that.is.empty; + expect(events).to.have.property('baz:bat!').that.is.empty; + }); + }); + + describe('emit()', () => { + it('should be a function', () => { + expect(inst) + .to.have.property('emit') + .that.is.a('function'); + }); + + it('should invoke handler for type', () => { + let foo = spy(), + event = {}; + events.foo = [foo]; + + inst.emit('foo', event); + + expect(foo) + .to.have.been.calledOnce + .and.calledWithExactly(event); + }); + + it('should ignore case', () => { + let foo = spy(), + event = {}; + events.foo = [foo]; + + inst.emit('FOO', event); + inst.emit('Foo', event); + + expect(foo) + .to.have.been.calledTwice + .and.always.calledWithExactly(event); + }); + + it('should invoke * handlers', () => { + let star = spy(), + event = {}; + events['*'] = [star]; + + inst.emit('foo', event); + inst.emit('bar', event); + + expect(star) + .to.have.been.calledTwice + .and.always.calledWithExactly(event); + }); + }); + }); +}); |
