Skip to content
Myr

index.js

This file

Path

test/index.js

149 lines · 3.5 KiB · JavaScript

Last changed by

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

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–16why, one step back
11describe('mitt#', () => {
12 let events, inst;1314 beforeEach( () => {15 inst = mitt();16 events = inst.all;

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

lines 26–30why, one step back
26 it('should register handler for new type', () => {
27 let foo = () => {};28 inst.on('foo', foo);2930 expect(events).to.have.property('foo').that.deep.equals(new Set([foo]));

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 31–32
31 });
32

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

lines 33–35why, one step back
33 it('should register handlers for any type strings', () => {
34 let foo = () => {};35 inst.on('constructor', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 36
36

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

lines 37–38why, one step back
37 expect(events).to.have.property('constructor').that.deep.equals(new Set([foo]));
38 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 39
39

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

lines 40–44why, one step back
40 it('should append handler for existing type', () => {
41 let foo = () => {};42 let bar = () => {};43 inst.on('foo', foo);44 inst.on('foo', bar);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 45
45

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

lines 46–47why, one step back
46 expect(events).to.have.property('foo').that.deep.equals(new Set([foo, foo, bar]));
47 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 48
48

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

lines 49–53why, one step back
49 it('should normalize case', () => {
50 let foo = () => {};51 inst.on('FOO', foo);52 inst.on('Bar', foo);53 inst.on('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 54
54

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

lines 55–59why, one step back
55 expect(events).to.have.property('foo').that.deep.equals(new Set([foo]));
56 expect(events).to.have.property('bar').that.deep.equals(new Set([foo]));57 expect(events).to.have.property('baz:bat!').that.deep.equals(new Set([foo]));58 });59 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 60
60

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

lines 61–65why, one step back
61 describe('off()', () => {
62 it('should be a function', () => {63 expect(inst)64 .to.have.property('off')65 .that.is.a('function');

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 66–67
66 });
67

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

lines 68–71why, one step back
68 it('should remove handler for type', () => {
69 let foo = () => {};70 inst.on('foo', foo);71 inst.off('foo', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 72
72

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

lines 73–74why, one step back
73 expect(events).to.have.property('foo').that.is.empty;
74 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 75
75

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

lines 76–80why, one step back
76 it('should normalize case', () => {
77 let foo = () => {};78 inst.on('foo', foo);79 inst.on('bar', foo);80 inst.on('baz:bat!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 81
81

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

lines 82–84why, one step back
82 inst.off('FOO', foo);
83 inst.off('Bar', foo);84 inst.off('baz:baT!', foo);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 85
85

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

lines 86–90why, one step back
86 expect(events).to.have.property('foo').that.is.empty;
87 expect(events).to.have.property('bar').that.is.empty;88 expect(events).to.have.property('baz:bat!').that.is.empty;89 });90 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 91
91

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 98
98

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

lines 99–100why, one step back
99 it('should invoke handler for type', () => {
100 let event = { a: 'b' };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 101
101

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

lines 102–105why, one step back
102 inst.on('foo', (one, two) => {
103 expect(one).to.deep.equal(event);104 expect(two).to.be.an('undefined');105 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 106
106

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 108–109
108 });
109

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

lines 110–117why, one step back
110 it('should invoke handler with multiple arguments', () => {
111 inst.on('foo', (aaa, bbb, ccc) => {112 expect(aaa).to.be.equal(111);113 expect(bbb).to.be.equal(222);114 expect(ccc).to.be.equal(333);115 });116 inst.emit('foo', 111, 222, 333);117 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 118
118

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

lines 119–122why, one step back
119 it('should ignore case', () => {
120 let foo = spy(),121 num = 123;122 events.foo = [foo];

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 123
123

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

lines 124–125why, one step back
124 inst.emit('FOO', num);
125 inst.emit('Foo', num);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 126
126

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

line 127why, one step back
127 let args = foo.args[0];

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 128
128

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

lines 129–131why, one step back
129 expect(foo).to.have.been.calledTwice;
130 expect(args).to.be.deep.equal([num, undefined, undefined]);131 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 132
132

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

lines 133–136why, one step back
133 it('should invoke * handlers', () => {
134 let star = spy(),135 aa = 'bb';136 events['*'] = [star];

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 137
137

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

lines 138–139why, one step back
138 inst.emit('foo', aa);
139 inst.emit('bar', aa);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 140
140

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

lines 141–142why, one step back
141 let args1 = star.args[0],
142 args2 = star.args[1];

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 143
143

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

lines 144–146why, one step back
144 expect(star).to.have.been.calledTwice;
145 expect(args1).to.deep.equal(['foo', aa, undefined, undefined]);146 expect(args2).to.deep.equal(['bar', aa, undefined, undefined]);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 147–149
147 });
148 });149});