Skip to content
Myr

index.js

This file

Path

test/index.js

168 lines · 4.1 KiB · JavaScript

Last changed by

Update all dependenciesJason Millerbefore adoptiondd64eea

History of this file

Take it

Initial commit! 🚀Jason Millerbefore adoptione77a42c

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

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

lines 7–9why, one step back
7it('should default export be a function', () => {
8 expect(mitt).to.be.a('function');9});

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 10
10

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

lines 11–14why, one step back
11describe('mitt#', () => {
12 let events, inst;1314 beforeEach( () => {

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 17–18
17 });
18

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

lines 19–24why, one step back
19 describe('on()', () => {
20 it('should be a function', () => {21 expect(inst)22 .to.have.property('on')23 .that.is.a('function');24 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 25
25

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

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

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

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

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

lines 28–29why, one step back
28 inst.on('foo', foo);
29

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 31–32
31 });
32

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

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

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 36
36

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 38why, one step back
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 append handler for existing type', () => {

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 45
45

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

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

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

line 47why, one step back
47 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 48
48

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

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

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

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

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

lines 51–53why, one step back
51 inst.on('FOO', foo);
52 inst.on('Bar', foo);53 inst.on('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 54
54

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

lines 55–59why, one step back
55 expect(events.get('FOO')).to.deep.equal([foo]);
56 expect(events.has('foo')).to.equal(false);57 expect(events.get('Bar')).to.deep.equal([foo]);58 expect(events.has('bar')).to.equal(false);59 expect(events.get('baz:baT!')).to.deep.equal([foo]);

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

line 60why, one step back
60 });

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

lines 61–67why, one step back
61
62 it('can take symbols for event types', () => {63 const foo = () => {};64 const eventType = Symbol('eventType');65 inst.on(eventType, foo);66 expect(events.get(eventType)).to.deep.equal([foo]);67 });

Add tests for duplicate listenersJason Millerbefore adoption7313d3a

lines 68–76why, one step back
68
69 // Adding the same listener multiple times should register it multiple times.70 // See https://nodejs.org/api/events.html#events_emitter_on_eventname_listener71 it('should add duplicate listeners', () => {72 const foo = () => {};73 inst.on('foo', foo);74 inst.on('foo', foo);75 expect(events.get('foo')).to.deep.equal([foo, foo]);76 });

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

line 77why, one step back
77 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 78
78

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

lines 79–83why, one step back
79 describe('off()', () => {
80 it('should be a function', () => {81 expect(inst)82 .to.have.property('off')83 .that.is.a('function');

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 84–85
84 });
85

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

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

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 90
90

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

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

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

line 92why, one step back
92 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 93
93

fix failing test from before removal of case normalizationJason Millerbefore adoption9953570

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

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

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

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 99
99

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

lines 100–102why, one step back
100 inst.off('FOO', foo);
101 inst.off('Bar', foo);102 inst.off('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 103
103

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

lines 104–108why, one step back
104 expect(events.get('FOO')).to.be.empty;
105 expect(events.has('foo')).to.equal(false);106 expect(events.get('Bar')).to.be.empty;107 expect(events.has('bar')).to.equal(false);108 expect(events.get('baz:bat!')).to.have.lengthOf(1);

Add tests for duplicate listenersJason Millerbefore adoption7313d3a

lines 109–118why, one step back
109 });
110111 it('should remove only the first matching listener', () => {112 const foo = () => {};113 inst.on('foo', foo);114 inst.on('foo', foo);115 inst.off('foo', foo);116 expect(events.get('foo')).to.deep.equal([foo]);117 inst.off('foo', foo);118 expect(events.get('foo')).to.deep.equal([]);

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

lines 119–120why, one step back
119 });
120 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 121
121

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

lines 122–127why, one step back
122 describe('emit()', () => {
123 it('should be a function', () => {124 expect(inst)125 .to.have.property('emit')126 .that.is.a('function');127 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 128
128

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 131
131

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 133–135why, one step back
133 expect(one).to.deep.equal(event);
134 expect(two).to.be.an('undefined');135 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 136
136

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 138–139
138 });
139

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

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

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

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

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 145
145

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 148
148

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

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

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

line 151why, one step back
151 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 152
152

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

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

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

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

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

lines 155–157why, one step back
155 ea = { a: 'a' },
156 eb = { b: 'b' };157

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

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

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', ea);
161 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);

Update all dependenciesJason Millerbefore adoptiondd64eea

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 163
163

Use sinon-chai methods for assertionsJason Millerbefore adoption4540116

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 166–168
166 });
167 });168});