index_test.ts
This file
Path
test/index_test.ts
201 lines · 4.9 KiB · TypeScript
Last changed by
Stronger typing (#114)iyegoroffbefore adoption8f439b8
History of this fileTake it
Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 1why, one step back1import mitt, { Emitter, EventHandlerMap } from '..';Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 2–17why, one step back2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);67describe('mitt', () => {8 it('should default export be a function', () => {9 expect(mitt).to.be.a('function');10 });1112 it('should accept an optional event handler map', () => {13 expect(() => mitt(new Map())).not.to.throw;14 const map = new Map();15 const a = spy();16 const b = spy();17 map.set('foo', [a, b]);Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 18why, one step back18 const events = mitt<{ foo: undefined }>(map);Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 19–25why, one step back19 events.emit('foo');20 expect(a).to.have.been.calledOnce;21 expect(b).to.have.been.calledOnce;22 });23});2425describe('mitt#', () => {Stronger typing (#114)iyegoroffbefore adoption8f439b8
lines 26–38why, one step back26 const eventType = Symbol('eventType');27 type Events = {28 foo: unknown;29 constructor: unknown;30 FOO: unknown;31 bar: unknown;32 Bar: unknown;33 'baz:bat!': unknown;34 'baz:baT!': unknown;35 Foo: unknown;36 [eventType]: unknown;37 };38 let events: EventHandlerMap<Events>, inst: Emitter<Events>;Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
line 39why, one step back39Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 40why, one step back40 beforeEach(() => {Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 41–44why, one step back41 events = new Map();42 inst = mitt(events);43 });44Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 45–52why, one step back45 describe('properties', () => {46 it('should expose the event handler map', () => {47 expect(inst)48 .to.have.property('all')49 .that.is.a('map');50 });51 });52Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 53–164why, one step back53 describe('on()', () => {54 it('should be a function', () => {55 expect(inst)56 .to.have.property('on')57 .that.is.a('function');58 });5960 it('should register handler for new type', () => {61 const foo = () => {};62 inst.on('foo', foo);6364 expect(events.get('foo')).to.deep.equal([foo]);65 });6667 it('should register handlers for any type strings', () => {68 const foo = () => {};69 inst.on('constructor', foo);7071 expect(events.get('constructor')).to.deep.equal([foo]);72 });7374 it('should append handler for existing type', () => {75 const foo = () => {};76 const bar = () => {};77 inst.on('foo', foo);78 inst.on('foo', bar);7980 expect(events.get('foo')).to.deep.equal([foo, bar]);81 });8283 it('should NOT normalize case', () => {84 const foo = () => {};85 inst.on('FOO', foo);86 inst.on('Bar', foo);87 inst.on('baz:baT!', foo);8889 expect(events.get('FOO')).to.deep.equal([foo]);90 expect(events.has('foo')).to.equal(false);91 expect(events.get('Bar')).to.deep.equal([foo]);92 expect(events.has('bar')).to.equal(false);93 expect(events.get('baz:baT!')).to.deep.equal([foo]);94 });9596 it('can take symbols for event types', () => {97 const foo = () => {};98 inst.on(eventType, foo);99 expect(events.get(eventType)).to.deep.equal([foo]);100 });101102 // Adding the same listener multiple times should register it multiple times.103 // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener104 it('should add duplicate listeners', () => {105 const foo = () => {};106 inst.on('foo', foo);107 inst.on('foo', foo);108 expect(events.get('foo')).to.deep.equal([foo, foo]);109 });110 });111112 describe('off()', () => {113 it('should be a function', () => {114 expect(inst)115 .to.have.property('off')116 .that.is.a('function');117 });118119 it('should remove handler for type', () => {120 const foo = () => {};121 inst.on('foo', foo);122 inst.off('foo', foo);123124 expect(events.get('foo')).to.be.empty;125 });126127 it('should NOT normalize case', () => {128 const foo = () => {};129 inst.on('FOO', foo);130 inst.on('Bar', foo);131 inst.on('baz:bat!', foo);132133 inst.off('FOO', foo);134 inst.off('Bar', foo);135 inst.off('baz:baT!', foo);136137 expect(events.get('FOO')).to.be.empty;138 expect(events.has('foo')).to.equal(false);139 expect(events.get('Bar')).to.be.empty;140 expect(events.has('bar')).to.equal(false);141 expect(events.get('baz:bat!')).to.have.lengthOf(1);142 });143144 it('should remove only the first matching listener', () => {145 const foo = () => {};146 inst.on('foo', foo);147 inst.on('foo', foo);148 inst.off('foo', foo);149 expect(events.get('foo')).to.deep.equal([foo]);150 inst.off('foo', foo);151 expect(events.get('foo')).to.deep.equal([]);152 });153 });154155 describe('emit()', () => {156 it('should be a function', () => {157 expect(inst)158 .to.have.property('emit')159 .that.is.a('function');160 });161162 it('should invoke handler for type', () => {163 const event = { a: 'b' };164Stronger typing (#114)iyegoroffbefore adoption8f439b8
line 165why, one step back165 inst.on('foo', (one, two?: unknown) => {Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
lines 166–201why, one step back166 expect(one).to.deep.equal(event);167 expect(two).to.be.an('undefined');168 });169170 inst.emit('foo', event);171 });172173 it('should NOT ignore case', () => {174 const onFoo = spy(),175 onFOO = spy();176 events.set('Foo', [onFoo]);177 events.set('FOO', [onFOO]);178179 inst.emit('Foo', 'Foo arg');180 inst.emit('FOO', 'FOO arg');181182 expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');183 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');184 });185186 it('should invoke * handlers', () => {187 const star = spy(),188 ea = { a: 'a' },189 eb = { b: 'b' };190191 events.set('*', [star]);192193 inst.emit('foo', ea);194 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);195 star.resetHistory();196197 inst.emit('bar', eb);198 expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);199 });200 });201});