Skip to content
Myr

index.js

This file

Path

test/index.js

145 lines · 3.2 KiB · JavaScript

Last changed by

Initial commit! 🚀Jason Millerbefore adoptione77a42c

History of this file

Take it

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 1–145
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( () => {17 events = {};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 });3435 it('should append handler for existing type', () => {36 let foo = () => {};37 let bar = () => {};38 inst.on('foo', foo);39 inst.on('foo', foo);40 inst.on('foo', bar);4142 expect(events).to.have.property('foo').that.deep.equals([foo, foo, bar]);43 });4445 it('should normalize case', () => {46 let foo = () => {};47 inst.on('FOO', foo);48 inst.on('Bar', foo);49 inst.on('baz:baT!', foo);5051 expect(events).to.have.property('foo').that.deep.equals([foo]);52 expect(events).to.have.property('bar').that.deep.equals([foo]);53 expect(events).to.have.property('baz:bat!').that.deep.equals([foo]);54 });55 });5657 describe('off()', () => {58 it('should be a function', () => {59 expect(inst)60 .to.have.property('off')61 .that.is.a('function');62 });6364 it('should remove handler for type', () => {65 let foo = () => {};66 events.foo = [foo];67 inst.off('foo', foo);6869 expect(events).to.have.property('foo').that.is.empty;70 });7172 it('should remove only one handler for dupes', () => {73 let foo = () => {};74 events.foo = [foo, foo];7576 inst.off('foo', foo);77 expect(events).to.have.property('foo').that.deep.equals([foo]);7879 inst.off('foo', foo);80 expect(events).to.have.property('foo').that.is.empty;81 });8283 it('should normalize case', () => {84 let foo = () => {};85 events.foo = [foo];86 events.bar = [foo];87 events['baz:bat!'] = [foo];8889 inst.off('FOO', foo);90 inst.off('Bar', foo);91 inst.off('baz:baT!', foo);9293 expect(events).to.have.property('foo').that.is.empty;94 expect(events).to.have.property('bar').that.is.empty;95 expect(events).to.have.property('baz:bat!').that.is.empty;96 });97 });9899 describe('emit()', () => {100 it('should be a function', () => {101 expect(inst)102 .to.have.property('emit')103 .that.is.a('function');104 });105106 it('should invoke handler for type', () => {107 let foo = spy(),108 event = {};109 events.foo = [foo];110111 inst.emit('foo', event);112113 expect(foo)114 .to.have.been.calledOnce115 .and.calledWithExactly(event);116 });117118 it('should ignore case', () => {119 let foo = spy(),120 event = {};121 events.foo = [foo];122123 inst.emit('FOO', event);124 inst.emit('Foo', event);125126 expect(foo)127 .to.have.been.calledTwice128 .and.always.calledWithExactly(event);129 });130131 it('should invoke * handlers', () => {132 let star = spy(),133 event = {};134 events['*'] = [star];135136 inst.emit('foo', event);137 inst.emit('bar', event);138139 expect(star)140 .to.have.been.calledTwice141 .and.always.calledWithExactly(event);142 });143 });144 });145});