diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/index_test.ts (renamed from test/index.js) | 6 | ||||
| -rw-r--r-- | test/test-types-compilation.ts | 43 | ||||
| -rw-r--r-- | test/types.ts | 20 |
3 files changed, 46 insertions, 23 deletions
diff --git a/test/index.js b/test/index_test.ts index a837d36..1f1d9bb 100644 --- a/test/index.js +++ b/test/index_test.ts @@ -1,4 +1,4 @@ -import mitt from '..'; +import mitt, { Emitter } from '..'; import chai, { expect } from 'chai'; import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; @@ -23,7 +23,7 @@ describe('mitt', () => { }); describe('mitt#', () => { - let events, inst; + let events, inst: Emitter; beforeEach( () => { events = new Map(); @@ -143,7 +143,7 @@ describe('mitt#', () => { it('should invoke handler for type', () => { const event = { a: 'b' }; - inst.on('foo', (one, two) => { + inst.on('foo', (one, two?) => { expect(one).to.deep.equal(event); expect(two).to.be.an('undefined'); }); diff --git a/test/test-types-compilation.ts b/test/test-types-compilation.ts new file mode 100644 index 0000000..00510da --- /dev/null +++ b/test/test-types-compilation.ts @@ -0,0 +1,43 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unused-vars */ + +import mitt from '..'; + +const emitter = mitt(); + +/* + * Check that if on is provided a generic, it only accepts handlers of that type + */ +{ + const badHandler = (x: number) => {}; + const goodHandler = (x: string) => {}; + + // @ts-expect-error + emitter.on<string>('foo', badHandler); + emitter.on<string>('foo', goodHandler); +} + +/* + * Check that if off is provided a generic, it only accepts handlers of that type + */ +{ + const badHandler = (x: number) => {}; + const goodHandler = (x: string) => {}; + + // @ts-expect-error + emitter.off<string>('foo', badHandler); + emitter.off<string>('foo', goodHandler); +} + + +/* + * Check that if emitt is provided a generic, it only accepts event data of that type + */ +{ + interface SomeEventData { + name: string; + } + // @ts-expect-error + emitter.emit<SomeEventData>('foo', 'NOT VALID'); + emitter.emit<SomeEventData>('foo', { name: 'jack' }); +} + diff --git a/test/types.ts b/test/types.ts deleted file mode 100644 index 23334bb..0000000 --- a/test/types.ts +++ /dev/null @@ -1,20 +0,0 @@ -import mitt, { EventHandlerList, EventHandlerMap } from '..'; - -const events = mitt(); -function foo() {} -events.on('foo', foo); -events.emit('foo', 'hello'); - -// handler return type should be ignored: -events.on('foo', async e => e * 42); - -// event map type -const map = new Map<string, EventHandlerList>([ - ['foo', [foo]] -]); -const events2 = mitt(map); -events2.emit('foo', 'hello'); - -// event map type & iterables -const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] }))); -mitt(map2); |
