Skip to content
Myr

Add generic types and update tests (#107)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add generic types and update tests * Add generic types to `on`, `off` and `emit` to enable some nicer TS usage if you specify what type you're expecting from the `EventData`. * Move the tests to be TypeScript source. This will help catch errors in the tests if there are any type errors. * Create a new test to test the generic types and make sure they pass and error when expected. * Upgrade to Mocha 8. * Did some tidying up of the package.json scripts. * Tweak TS setup to validate tests * Fix d.ts generation and tests

authored by Jack Franklin · committed by GitHub · parent 244cac2 · Browse files · 10 files+265 added−223 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Jason Miller <developit@users.noreply.github.com>

Diff stats

10 files+265 added−223 removed

  • tests:+225 added−202 removed
  • other:+40 added−21 removed

10 files+265 added−223 removed

modified:src/index.ts+7 added-7 removed
added:test/index_test.ts+182 added
1import mitt, { Emitter } from '..';2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);67describe('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 });23});2425describe('mitt#', () => {26 let events, inst: Emitter;2728 beforeEach( () => {29 events = new Map();30 inst = mitt(events);31 });3233 describe('on()', () => {34 it('should be a function', () => {35 expect(inst)36 .to.have.property('on')37 .that.is.a('function');38 });3940 it('should register handler for new type', () => {41 const foo = () => {};42 inst.on('foo', foo);4344 expect(events.get('foo')).to.deep.equal([foo]);45 });4647 it('should register handlers for any type strings', () => {48 const foo = () => {};49 inst.on('constructor', foo);5051 expect(events.get('constructor')).to.deep.equal([foo]);52 });5354 it('should append handler for existing type', () => {55 const foo = () => {};56 const bar = () => {};57 inst.on('foo', foo);58 inst.on('foo', bar);5960 expect(events.get('foo')).to.deep.equal([foo, bar]);61 });6263 it('should NOT normalize case', () => {64 const foo = () => {};65 inst.on('FOO', foo);66 inst.on('Bar', foo);67 inst.on('baz:baT!', foo);6869 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]);74 });7576 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 });8283 // 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 });91 });9293 describe('off()', () => {94 it('should be a function', () => {95 expect(inst)96 .to.have.property('off')97 .that.is.a('function');98 });99100 it('should remove handler for type', () => {101 const foo = () => {};102 inst.on('foo', foo);103 inst.off('foo', foo);104105 expect(events.get('foo')).to.be.empty;106 });107108 it('should NOT normalize case', () => {109 const foo = () => {};110 inst.on('FOO', foo);111 inst.on('Bar', foo);112 inst.on('baz:bat!', foo);113114 inst.off('FOO', foo);115 inst.off('Bar', foo);116 inst.off('baz:baT!', foo);117118 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);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([]);133 });134 });135136 describe('emit()', () => {137 it('should be a function', () => {138 expect(inst)139 .to.have.property('emit')140 .that.is.a('function');141 });142143 it('should invoke handler for type', () => {144 const event = { a: 'b' };145146 inst.on('foo', (one, two?) => {147 expect(one).to.deep.equal(event);148 expect(two).to.be.an('undefined');149 });150151 inst.emit('foo', event);152 });153154 it('should NOT ignore case', () => {155 const onFoo = spy(),156 onFOO = spy();157 events.set('Foo', [onFoo]);158 events.set('FOO', [onFOO]);159160 inst.emit('Foo', 'Foo arg');161 inst.emit('FOO', 'FOO arg');162163 expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');164 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');165 });166167 it('should invoke * handlers', () => {168 const star = spy(),169 ea = { a: 'a' },170 eb = { b: 'b' };171172 events.set('*', [star]);173174 inst.emit('foo', ea);175 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);176 star.resetHistory();177178 inst.emit('bar', eb);179 expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);180 });181 });182});
deleted:test/index.js-182 removed
1import mitt from '..';2import chai, { expect } from 'chai';3import { spy } from 'sinon';4import sinonChai from 'sinon-chai';5chai.use(sinonChai);67describe('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 });23});2425describe('mitt#', () => {26 let events, inst;2728 beforeEach( () => {29 events = new Map();30 inst = mitt(events);31 });3233 describe('on()', () => {34 it('should be a function', () => {35 expect(inst)36 .to.have.property('on')37 .that.is.a('function');38 });3940 it('should register handler for new type', () => {41 const foo = () => {};42 inst.on('foo', foo);4344 expect(events.get('foo')).to.deep.equal([foo]);45 });4647 it('should register handlers for any type strings', () => {48 const foo = () => {};49 inst.on('constructor', foo);5051 expect(events.get('constructor')).to.deep.equal([foo]);52 });5354 it('should append handler for existing type', () => {55 const foo = () => {};56 const bar = () => {};57 inst.on('foo', foo);58 inst.on('foo', bar);5960 expect(events.get('foo')).to.deep.equal([foo, bar]);61 });6263 it('should NOT normalize case', () => {64 const foo = () => {};65 inst.on('FOO', foo);66 inst.on('Bar', foo);67 inst.on('baz:baT!', foo);6869 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]);74 });7576 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 });8283 // 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 });91 });9293 describe('off()', () => {94 it('should be a function', () => {95 expect(inst)96 .to.have.property('off')97 .that.is.a('function');98 });99100 it('should remove handler for type', () => {101 const foo = () => {};102 inst.on('foo', foo);103 inst.off('foo', foo);104105 expect(events.get('foo')).to.be.empty;106 });107108 it('should NOT normalize case', () => {109 const foo = () => {};110 inst.on('FOO', foo);111 inst.on('Bar', foo);112 inst.on('baz:bat!', foo);113114 inst.off('FOO', foo);115 inst.off('Bar', foo);116 inst.off('baz:baT!', foo);117118 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);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([]);133 });134 });135136 describe('emit()', () => {137 it('should be a function', () => {138 expect(inst)139 .to.have.property('emit')140 .that.is.a('function');141 });142143 it('should invoke handler for type', () => {144 const event = { a: 'b' };145146 inst.on('foo', (one, two) => {147 expect(one).to.deep.equal(event);148 expect(two).to.be.an('undefined');149 });150151 inst.emit('foo', event);152 });153154 it('should NOT ignore case', () => {155 const onFoo = spy(),156 onFOO = spy();157 events.set('Foo', [onFoo]);158 events.set('FOO', [onFOO]);159160 inst.emit('Foo', 'Foo arg');161 inst.emit('FOO', 'FOO arg');162163 expect(onFoo).to.have.been.calledOnce.and.calledWith('Foo arg');164 expect(onFOO).to.have.been.calledOnce.and.calledWith('FOO arg');165 });166167 it('should invoke * handlers', () => {168 const star = spy(),169 ea = { a: 'a' },170 eb = { b: 'b' };171172 events.set('*', [star]);173174 inst.emit('foo', ea);175 expect(star).to.have.been.calledOnce.and.calledWith('foo', ea);176 star.resetHistory();177178 inst.emit('bar', eb);179 expect(star).to.have.been.calledOnce.and.calledWith('bar', eb);180 });181 });182});
added:test/test-types-compilation.ts+43 added
deleted:test/types.ts-20 removed
modified:.editorconfig+1 added
8 unmodified lines
910[{package.json,.*rc,*.yml}]11indent_style = space12indent_size = 213insert_final_newline = false1415[*.md]16trim_trailing_whitespace = false17indent_style = space
1 unmodified line
modified:.gitignore+1 added
1/dist2/test-reports3/node_modules4/npm-debug.log5/index.d.ts6package-lock.json7.DS_Store8.idea
modified:package.json+25 added-10 removed
6 unmodified lines
7 "module": "dist/mitt.es.js",8 "esmodules": "dist/mitt.modern.js",9 "main": "dist/mitt.js",10 "umd:main": "dist/mitt.umd.js",11 "typings": "dist/index.d.ts",11 "typings": "index.d.ts",12 "scripts": {13 "test": "npm-run-all --silent typecheck lint testonly",14 "testonly": "mocha --require esm test/**/*.js",13 "test": "npm-run-all --silent typecheck lint mocha test-types",14 "mocha": "mocha test",15 "test-types": "tsc test/test-types-compilation.ts --noEmit",16 "lint": "eslint src test --ext ts --ext js",16 "typecheck": "tsc **/*.ts --noEmit",17 "typecheck": "tsc --noEmit",18 "bundle": "microbundle",19 "build": "npm-run-all --silent clean -p bundle -s docs",20 "clean": "rimraf dist",21 "docs": "documentation readme src/index.ts --section API -q --parse-extension ts",
12 unmodified lines
34 ],35 "license": "MIT",36 "files": [37 "src",37 "dist"38 "dist",39 "index.d.ts"40 ],41 "mocha": {42 "extension": [43 "ts"44 ],45 "require": [46 "ts-node/register",47 "esm"48 ],49 "spec": [50 "test/*_test.ts"51 ]52 },53 "eslintConfig": {54 "extends": [55 "developit",56 "plugin:@typescript-eslint/eslint-recommended",
24 unmodified lines
81 "@typescript-eslint/no-empty-function": 082 }83 },84 "eslintIgnore": [71 "dist"85 "dist",86 "index.d.ts"87 ],88 "devDependencies": {89 "@types/chai": "^4.2.11",90 "@types/mocha": "^7.0.2",
5 unmodified lines
96 "documentation": "^13.0.0",97 "eslint": "^7.1.0",98 "eslint-config-developit": "^1.2.0",99 "esm": "^3.2.25",85 "microbundle": "^0.12.0",86 "mocha": "^7.2.0",100 "microbundle": "^0.12.3",101 "mocha": "^8.0.1",102 "npm-run-all": "^4.1.5",103 "rimraf": "^3.0.2",104 "sinon": "^9.0.2",105 "sinon-chai": "^3.5.0",91 "ts-node": "^8.10.1",106 "ts-node": "^8.10.2",107 "typescript": "^3.9.3"108 }94}109}
modified:README.md+1 added-1 removed
108 unmodified lines
109### mitt110111Mitt: Tiny (~200b) functional event emitter / pubsub.112113Returns **Mitt**113Returns **Mitt** 114115### on116117Register an event handler for the given type.
54 unmodified lines
modified:tsconfig.json+5 added-3 removed