Skip to content
Myr

index_test.ts

This file

Path

test/index_test.ts

204 lines · 5.2 KiB · TypeScript

Last changed by

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

History of this file

Take it

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 1why, one step back
1import mitt, { Emitter, EventHandlerMap } from '..';

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 2–17why, one step back
2import 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]);

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 18why, one step back
18 const events = mitt<{ foo: undefined }>(map);

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 19–25why, one step back
19 events.emit('foo');
20 expect(a).to.have.been.calledOnce;21 expect(b).to.have.been.calledOnce;22 });23});2425describe('mitt#', () => {

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

lines 26–38why, one step back
26 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 back
39

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 40why, one step back
40 beforeEach(() => {

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 41–44why, one step back
41 events = new Map();
42 inst = mitt(events);43 });44

Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead

lines 45–46why, one step back
45 describe('properties', () => {
46 it('should expose the event handler map', () => {

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 47why, one step back
47 expect(inst).to.have.property('all').that.is.a('map');

Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead

lines 48–50why, one step back
48 });
49 });50

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 51–52why, one step back
51 describe('on()', () => {
52 it('should be a function', () => {

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 53why, one step back
53 expect(inst).to.have.property('on').that.is.a('function');

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 54–109why, one step back
54 });
5556 it('should register handler for new type', () => {57 const foo = () => {};58 inst.on('foo', foo);5960 expect(events.get('foo')).to.deep.equal([foo]);61 });6263 it('should register handlers for any type strings', () => {64 const foo = () => {};65 inst.on('constructor', foo);6667 expect(events.get('constructor')).to.deep.equal([foo]);68 });6970 it('should append handler for existing type', () => {71 const foo = () => {};72 const bar = () => {};73 inst.on('foo', foo);74 inst.on('foo', bar);7576 expect(events.get('foo')).to.deep.equal([foo, bar]);77 });7879 it('should NOT normalize case', () => {80 const foo = () => {};81 inst.on('FOO', foo);82 inst.on('Bar', foo);83 inst.on('baz:baT!', foo);8485 expect(events.get('FOO')).to.deep.equal([foo]);86 expect(events.has('foo')).to.equal(false);87 expect(events.get('Bar')).to.deep.equal([foo]);88 expect(events.has('bar')).to.equal(false);89 expect(events.get('baz:baT!')).to.deep.equal([foo]);90 });9192 it('can take symbols for event types', () => {93 const foo = () => {};94 inst.on(eventType, foo);95 expect(events.get(eventType)).to.deep.equal([foo]);96 });9798 // Adding the same listener multiple times should register it multiple times.99 // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener100 it('should add duplicate listeners', () => {101 const foo = () => {};102 inst.on('foo', foo);103 inst.on('foo', foo);104 expect(events.get('foo')).to.deep.equal([foo, foo]);105 });106 });107108 describe('off()', () => {109 it('should be a function', () => {

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 110why, one step back
110 expect(inst).to.have.property('off').that.is.a('function');

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 111–146why, one step back
111 });
112113 it('should remove handler for type', () => {114 const foo = () => {};115 inst.on('foo', foo);116 inst.off('foo', foo);117118 expect(events.get('foo')).to.be.empty;119 });120121 it('should NOT normalize case', () => {122 const foo = () => {};123 inst.on('FOO', foo);124 inst.on('Bar', foo);125 inst.on('baz:bat!', foo);126127 inst.off('FOO', foo);128 inst.off('Bar', foo);129 inst.off('baz:baT!', foo);130131 expect(events.get('FOO')).to.be.empty;132 expect(events.has('foo')).to.equal(false);133 expect(events.get('Bar')).to.be.empty;134 expect(events.has('bar')).to.equal(false);135 expect(events.get('baz:bat!')).to.have.lengthOf(1);136 });137138 it('should remove only the first matching listener', () => {139 const foo = () => {};140 inst.on('foo', foo);141 inst.on('foo', foo);142 inst.off('foo', foo);143 expect(events.get('foo')).to.deep.equal([foo]);144 inst.off('foo', foo);145 expect(events.get('foo')).to.deep.equal([]);146 });

Add test for #124Jason Millerbefore adoption0cce8fa

lines 147–157why, one step back
147
148 it('off("type") should remove all handlers of the given type', () => {149 inst.on('foo', () => {});150 inst.on('foo', () => {});151 inst.on('bar', () => {});152 inst.off('foo');153 expect(events.get('foo')).to.deep.equal([]);154 expect(events.get('bar')).to.have.length(1);155 inst.off('bar');156 expect(events.get('bar')).to.deep.equal([]);157 });

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 158–161why, one step back
158 });
159160 describe('emit()', () => {161 it('should be a function', () => {

apply prettier formatting to all filesJason Millerbefore adoption05f4ce6

line 162why, one step back
162 expect(inst).to.have.property('emit').that.is.a('function');

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 163–167why, one step back
163 });
164165 it('should invoke handler for type', () => {166 const event = { a: 'b' };167

Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689

line 168why, one step back
168 inst.on('foo', (one, two?: unknown) => {

Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4

lines 169–204why, one step back
169 expect(one).to.deep.equal(event);
170 expect(two).to.be.an('undefined');171 });172173 inst.emit('foo', event);174 });175176 it('should NOT ignore case', () => {177 const onFoo = spy(),178 onFOO = spy();179 events.set('Foo', [onFoo]);180 events.set('FOO', [onFOO]);181182 inst.emit('Foo', 'Foo arg');183 inst.emit('FOO', 'FOO arg');184185 expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');186 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');187 });188189 it('should invoke * handlers', () => {190 const star = spy(),191 ea = { a: 'a' },192 eb = { b: 'b' };193194 events.set('*', [star]);195196 inst.emit('foo', ea);197 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);198 star.resetHistory();199200 inst.emit('bar', eb);201 expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);202 });203 });204});