Skip to content
Myr

index.js

This file

Path

test/index.js

182 lines · 4.4 KiB · JavaScript

Last changed by

Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c

History of this file

Take it

Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c

line 1why, one step back
1import mitt from '..';

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 2–6
2import chai, { expect } from 'chai';
3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);6

Export Mitt types for TS consumers (#101)Jack Franklinbefore adoptioneb2be7c

lines 7–22why, one step back
7describe('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]);18 const events = mitt(map);19 events.emit('foo');20 expect(a).to.have.been.calledOnce;21 expect(b).to.have.been.calledOnce;22 });

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 23why, one step back
23});

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 24
24

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 25–28why, one step back
25describe('mitt#', () => {
26 let events, inst;2728 beforeEach( () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 29why, one step back
29 events = new Map();

Merge branch 'agudulin-master' into tunnckoCore-refactorJason Millerbefore adoptione428f3a

line 30why, one step back
30 inst = mitt(events);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 31–32
31 });
32

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 33–38why, one step back
33 describe('on()', () => {
34 it('should be a function', () => {35 expect(inst)36 .to.have.property('on')37 .that.is.a('function');38 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 39
39

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 40why, one step back
40 it('should register handler for new type', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 41why, one step back
41 const foo = () => {};

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 42–43why, one step back
42 inst.on('foo', foo);
43

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 44why, one step back
44 expect(events.get('foo')).to.deep.equal([foo]);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 45–46
45 });
46

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 47why, one step back
47 it('should register handlers for any type strings', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 48why, one step back
48 const foo = () => {};

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 49why, one step back
49 inst.on('constructor', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 50
50

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 51why, one step back
51 expect(events.get('constructor')).to.deep.equal([foo]);

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 52why, one step back
52 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 53
53

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 54why, one step back
54 it('should append handler for existing type', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 55–56why, one step back
55 const foo = () => {};
56 const bar = () => {};

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 57–58why, one step back
57 inst.on('foo', foo);
58 inst.on('foo', bar);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 59
59

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 60why, one step back
60 expect(events.get('foo')).to.deep.equal([foo, bar]);

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 61why, one step back
61 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 62
62

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

line 63why, one step back
63 it('should NOT normalize case', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 64why, one step back
64 const foo = () => {};

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 65–67why, one step back
65 inst.on('FOO', foo);
66 inst.on('Bar', foo);67 inst.on('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 68
68

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 69–73why, one step back
69 expect(events.get('FOO')).to.deep.equal([foo]);
70 expect(events.has('foo')).to.equal(false);71 expect(events.get('Bar')).to.deep.equal([foo]);72 expect(events.has('bar')).to.equal(false);73 expect(events.get('baz:baT!')).to.deep.equal([foo]);

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 74why, one step back
74 });

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 75–81why, one step back
75
76 it('can take symbols for event types', () => {77 const foo = () => {};78 const eventType = Symbol('eventType');79 inst.on(eventType, foo);80 expect(events.get(eventType)).to.deep.equal([foo]);81 });

Add tests for duplicate listenersJason Millerbefore adoption7313d3a

lines 82–90why, one step back
82
83 // Adding the same listener multiple times should register it multiple times.84 // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener85 it('should add duplicate listeners', () => {86 const foo = () => {};87 inst.on('foo', foo);88 inst.on('foo', foo);89 expect(events.get('foo')).to.deep.equal([foo, foo]);90 });

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 91why, one step back
91 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 92
92

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 93–97why, one step back
93 describe('off()', () => {
94 it('should be a function', () => {95 expect(inst)96 .to.have.property('off')97 .that.is.a('function');

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 98–99
98 });
99

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 100why, one step back
100 it('should remove handler for type', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 101why, one step back
101 const foo = () => {};

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 102–103why, one step back
102 inst.on('foo', foo);
103 inst.off('foo', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 104
104

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 105why, one step back
105 expect(events.get('foo')).to.be.empty;

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 106why, one step back
106 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 107
107

fix failing test from before removal of case normalizationJason Millerbefore adoption9953570

line 108why, one step back
108 it('should NOT normalize case', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 109why, one step back
109 const foo = () => {};

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

lines 110–111why, one step back
110 inst.on('FOO', foo);
111 inst.on('Bar', foo);

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 112why, one step back
112 inst.on('baz:bat!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 113
113

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 114–116why, one step back
114 inst.off('FOO', foo);
115 inst.off('Bar', foo);116 inst.off('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 117
117

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 118–122why, one step back
118 expect(events.get('FOO')).to.be.empty;
119 expect(events.has('foo')).to.equal(false);120 expect(events.get('Bar')).to.be.empty;121 expect(events.has('bar')).to.equal(false);122 expect(events.get('baz:bat!')).to.have.lengthOf(1);

Add tests for duplicate listenersJason Millerbefore adoption7313d3a

lines 123–132why, one step back
123 });
124125 it('should remove only the first matching listener', () => {126 const foo = () => {};127 inst.on('foo', foo);128 inst.on('foo', foo);129 inst.off('foo', foo);130 expect(events.get('foo')).to.deep.equal([foo]);131 inst.off('foo', foo);132 expect(events.get('foo')).to.deep.equal([]);

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 133–134why, one step back
133 });
134 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 135
135

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 136–141why, one step back
136 describe('emit()', () => {
137 it('should be a function', () => {138 expect(inst)139 .to.have.property('emit')140 .that.is.a('function');141 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 142
142

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 143why, one step back
143 it('should invoke handler for type', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 144why, one step back
144 const event = { a: 'b' };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 145
145

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 146why, one step back
146 inst.on('foo', (one, two) => {

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 147–149why, one step back
147 expect(one).to.deep.equal(event);
148 expect(two).to.be.an('undefined');149 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 150
150

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 151why, one step back
151 inst.emit('foo', event);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 152–153
152 });
153

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

line 154why, one step back
154 it('should NOT ignore case', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 155why, one step back
155 const onFoo = spy(),

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

line 156why, one step back
156 onFOO = spy();

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

lines 157–158why, one step back
157 events.set('Foo', [onFoo]);
158 events.set('FOO', [onFOO]);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 159
159

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 160–161why, one step back
160 inst.emit('Foo', 'Foo arg');
161 inst.emit('FOO', 'FOO arg');

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 162
162

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 163–164why, one step back
163 expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');
164 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 165why, one step back
165 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 166
166

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 167why, one step back
167 it('should invoke * handlers', () => {

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 168why, one step back
168 const star = spy(),

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 169–171why, one step back
169 ea = { a: 'a' },
170 eb = { b: 'b' };171

Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f

line 172why, one step back
172 events.set('*', [star]);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 173
173

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 174–175why, one step back
174 inst.emit('foo', ea);
175 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);

Update all dependenciesJason Millerbefore adoptiondd64eea

line 176why, one step back
176 star.resetHistory();

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 177
177

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 178–179why, one step back
178 inst.emit('bar', eb);
179 expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 180–182
180 });
181 });182});