Skip to content
Myr

index.js

This file

Path

test/index.js

152 lines · 3.5 KiB · JavaScript

Last changed by

Fixes #9: use Object.create(null) over {}Alexander Gudulinbefore adoption3fb2dc2

History of this file

Take it

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 1–16
1import mitt from '../src';
2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);678describe('mitt', () => {9 it('should be a function', () => {10 expect(mitt).to.be.a('function');11 });1213 describe('mitt#', () => {14 let events, inst;1516 beforeEach( () => {

Fixes #9: use Object.create(null) over {}Alexander Gudulinbefore adoption3fb2dc2

line 17why, one step back
17 events = Object.create(null);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 18–33
18 inst = mitt(events);
19 });2021 describe('on()', () => {22 it('should be a function', () => {23 expect(inst)24 .to.have.property('on')25 .that.is.a('function');26 });2728 it('should register handler for new type', () => {29 let foo = () => {};30 inst.on('foo', foo);3132 expect(events).to.have.property('foo').that.deep.equals([foo]);33 });

Fixes #9: use Object.create(null) over {}Alexander Gudulinbefore adoption3fb2dc2

lines 34–40why, one step back
34
35 it('should register handlers for any type strings', () => {36 let foo = () => {};37 inst.on('constructor', foo);3839 expect(events).to.have.property('constructor').that.deep.equals([foo]);40 });

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 41–152
41
42 it('should append handler for existing type', () => {43 let foo = () => {};44 let bar = () => {};45 inst.on('foo', foo);46 inst.on('foo', foo);47 inst.on('foo', bar);4849 expect(events).to.have.property('foo').that.deep.equals([foo, foo, bar]);50 });5152 it('should normalize case', () => {53 let foo = () => {};54 inst.on('FOO', foo);55 inst.on('Bar', foo);56 inst.on('baz:baT!', foo);5758 expect(events).to.have.property('foo').that.deep.equals([foo]);59 expect(events).to.have.property('bar').that.deep.equals([foo]);60 expect(events).to.have.property('baz:bat!').that.deep.equals([foo]);61 });62 });6364 describe('off()', () => {65 it('should be a function', () => {66 expect(inst)67 .to.have.property('off')68 .that.is.a('function');69 });7071 it('should remove handler for type', () => {72 let foo = () => {};73 events.foo = [foo];74 inst.off('foo', foo);7576 expect(events).to.have.property('foo').that.is.empty;77 });7879 it('should remove only one handler for dupes', () => {80 let foo = () => {};81 events.foo = [foo, foo];8283 inst.off('foo', foo);84 expect(events).to.have.property('foo').that.deep.equals([foo]);8586 inst.off('foo', foo);87 expect(events).to.have.property('foo').that.is.empty;88 });8990 it('should normalize case', () => {91 let foo = () => {};92 events.foo = [foo];93 events.bar = [foo];94 events['baz:bat!'] = [foo];9596 inst.off('FOO', foo);97 inst.off('Bar', foo);98 inst.off('baz:baT!', foo);99100 expect(events).to.have.property('foo').that.is.empty;101 expect(events).to.have.property('bar').that.is.empty;102 expect(events).to.have.property('baz:bat!').that.is.empty;103 });104 });105106 describe('emit()', () => {107 it('should be a function', () => {108 expect(inst)109 .to.have.property('emit')110 .that.is.a('function');111 });112113 it('should invoke handler for type', () => {114 let foo = spy(),115 event = {};116 events.foo = [foo];117118 inst.emit('foo', event);119120 expect(foo)121 .to.have.been.calledOnce122 .and.calledWithExactly(event);123 });124125 it('should ignore case', () => {126 let foo = spy(),127 event = {};128 events.foo = [foo];129130 inst.emit('FOO', event);131 inst.emit('Foo', event);132133 expect(foo)134 .to.have.been.calledTwice135 .and.always.calledWithExactly(event);136 });137138 it('should invoke * handlers', () => {139 let star = spy(),140 event = {};141 events['*'] = [star];142143 inst.emit('foo', event);144 inst.emit('bar', event);145146 expect(star)147 .to.have.been.calledTwice148 .and.always.calledWithExactly(event);149 });150 });151 });152});