diff options
| author | tunnckoCore <[email protected]> | 2017-01-17 18:21:35 +0200 |
|---|---|---|
| committer | tunnckoCore <[email protected]> | 2017-01-17 18:21:35 +0200 |
| commit | a6190e91a68d35adffbeda2e92f92ec565557f89 (patch) | |
| tree | 3d92550834ee10c117ca195db96568f9762c95b1 /test | |
| parent | 42fb45ef29482c136e7ba602bc27bfe2c50c2f00 (diff) | |
refactor: fixes #2, #6, #12 and #13
Diffstat (limited to 'test')
| -rw-r--r-- | test/index.js | 256 |
1 files changed, 130 insertions, 126 deletions
diff --git a/test/index.js b/test/index.js index 8055544..4d818b5 100644 --- a/test/index.js +++ b/test/index.js @@ -4,142 +4,146 @@ import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; chai.use(sinonChai); +it('should default export be a function', () => { + expect(mitt).to.be.a('function'); +}); + +describe('mitt#', () => { + let events, inst; -describe('mitt', () => { - it('should be a function', () => { - expect(mitt).to.be.a('function'); + beforeEach( () => { + inst = mitt(); + events = inst.all; }); - describe('mitt#', () => { - let events, inst; + 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(new Set([foo])); + }); + + it('should register handlers for any type strings', () => { + let foo = () => {}; + inst.on('constructor', foo); + + expect(events).to.have.property('constructor').that.deep.equals(new Set([foo])); + }); + + it('should append handler for existing type', () => { + let foo = () => {}; + let bar = () => {}; + inst.on('foo', foo); + inst.on('foo', bar); + + expect(events).to.have.property('foo').that.deep.equals(new Set([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(new Set([foo])); + expect(events).to.have.property('bar').that.deep.equals(new Set([foo])); + expect(events).to.have.property('baz:bat!').that.deep.equals(new Set([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 = () => {}; + inst.on('foo', foo); + inst.off('foo', foo); - beforeEach( () => { - events = {}; - inst = mitt(events); + expect(events).to.have.property('foo').that.is.empty; }); - 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]); - }); + it('should normalize case', () => { + let foo = () => {}; + inst.on('foo', foo); + inst.on('bar', foo); + inst.on('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('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'); }); - 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); - }); + it('should invoke handler for type', () => { + let event = { a: 'b' }; + + inst.on('foo', (one, two) => { + expect(one).to.deep.equal(event); + expect(two).to.be.an('undefined'); + }); + + inst.emit('foo', event); + }); + + it('should invoke handler with multiple arguments', () => { + inst.on('foo', (aaa, bbb, ccc) => { + expect(aaa).to.be.equal(111); + expect(bbb).to.be.equal(222); + expect(ccc).to.be.equal(333); + }); + inst.emit('foo', 111, 222, 333); + }); + + it('should ignore case', () => { + let foo = spy(), + num = 123; + events.foo = [foo]; + + inst.emit('FOO', num); + inst.emit('Foo', num); + + let args = foo.args[0]; + + expect(foo).to.have.been.calledTwice; + expect(args).to.be.deep.equal([num, undefined, undefined]); + }); + + it('should invoke * handlers', () => { + let star = spy(), + aa = 'bb'; + events['*'] = [star]; + + inst.emit('foo', aa); + inst.emit('bar', aa); + + let args1 = star.args[0], + args2 = star.args[1]; + + expect(star).to.have.been.calledTwice; + expect(args1).to.deep.equal(['foo', aa, undefined, undefined]); + expect(args2).to.deep.equal(['bar', aa, undefined, undefined]); }); }); }); |
