diff options
| author | iyegoroff <[email protected]> | 2021-06-23 00:22:49 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-22 17:22:49 -0400 |
| commit | 8f439b8ae54d3864c24ddf7205297031d64fbad6 (patch) | |
| tree | 579690447e9aa847f70d527c7ed00b11c409e62f /test | |
| parent | 22c5dcba10736aecb1f39ee88d9f85278108c988 (diff) | |
Stronger typing (#114)
* improved typing
* upd readme
* upd readme
* upd test & readme
* removed magic 1
* removed ts-toolbelt dependency
Diffstat (limited to 'test')
| -rw-r--r-- | test/index_test.ts | 25 | ||||
| -rw-r--r-- | test/test-types-compilation.ts | 75 |
2 files changed, 73 insertions, 27 deletions
diff --git a/test/index_test.ts b/test/index_test.ts index 5c25b55..ba1dbff 100644 --- a/test/index_test.ts +++ b/test/index_test.ts @@ -1,4 +1,4 @@ -import mitt, { Emitter } from '..'; +import mitt, { Emitter, EventHandlerMap } from '..'; import chai, { expect } from 'chai'; import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; @@ -15,7 +15,7 @@ describe('mitt', () => { const a = spy(); const b = spy(); map.set('foo', [a, b]); - const events = mitt(map); + const events = mitt<{ foo: undefined }>(map); events.emit('foo'); expect(a).to.have.been.calledOnce; expect(b).to.have.been.calledOnce; @@ -23,9 +23,21 @@ describe('mitt', () => { }); describe('mitt#', () => { - let events, inst: Emitter; - - beforeEach( () => { + const eventType = Symbol('eventType'); + type Events = { + foo: unknown; + constructor: unknown; + FOO: unknown; + bar: unknown; + Bar: unknown; + 'baz:bat!': unknown; + 'baz:baT!': unknown; + Foo: unknown; + [eventType]: unknown; + }; + let events: EventHandlerMap<Events>, inst: Emitter<Events>; + + beforeEach(() => { events = new Map(); inst = mitt(events); }); @@ -83,7 +95,6 @@ describe('mitt#', () => { it('can take symbols for event types', () => { const foo = () => {}; - const eventType = Symbol('eventType'); inst.on(eventType, foo); expect(events.get(eventType)).to.deep.equal([foo]); }); @@ -151,7 +162,7 @@ describe('mitt#', () => { it('should invoke handler for type', () => { const event = { a: 'b' }; - inst.on('foo', (one, two?) => { + inst.on('foo', (one, two?: unknown) => { 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 index 00510da..476e631 100644 --- a/test/test-types-compilation.ts +++ b/test/test-types-compilation.ts @@ -2,42 +2,77 @@ import mitt from '..'; -const emitter = mitt(); +interface SomeEventData { + name: string; +} + +const emitter = mitt<{ + foo: string; + someEvent: SomeEventData; + bar?: number; +}>(); + +const barHandler = (x?: number) => {}; +const fooHandler = (x: string) => {}; +const wildcardHandler = ( + _type: 'foo' | 'bar' | 'someEvent', + _event: string | SomeEventData | number | undefined +) => {}; /* - * Check that if on is provided a generic, it only accepts handlers of that type + * Check that 'on' args are inferred correctly */ { - const badHandler = (x: number) => {}; - const goodHandler = (x: string) => {}; + // @ts-expect-error + emitter.on('foo', barHandler); + emitter.on('foo', fooHandler); + emitter.on('bar', barHandler); // @ts-expect-error - emitter.on<string>('foo', badHandler); - emitter.on<string>('foo', goodHandler); + emitter.on('bar', fooHandler); + + emitter.on('*', wildcardHandler); + // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string + emitter.on('*', fooHandler); + // @ts-expect-error + emitter.on('*', barHandler); } /* - * Check that if off is provided a generic, it only accepts handlers of that type + * Check that 'off' args are inferred correctly */ { - const badHandler = (x: number) => {}; - const goodHandler = (x: string) => {}; + // @ts-expect-error + emitter.off('foo', barHandler); + emitter.off('foo', fooHandler); + emitter.off('bar', barHandler); // @ts-expect-error - emitter.off<string>('foo', badHandler); - emitter.off<string>('foo', goodHandler); -} + emitter.off('bar', fooHandler); + emitter.off('*', wildcardHandler); + // fooHandler is ok, because ('foo' | 'bar' | 'someEvent') extends string + emitter.off('*', fooHandler); + // @ts-expect-error + emitter.off('*', barHandler); +} /* - * Check that if emitt is provided a generic, it only accepts event data of that type + * Check that 'emit' args are inferred correctly */ { - interface SomeEventData { - name: string; - } - // @ts-expect-error - emitter.emit<SomeEventData>('foo', 'NOT VALID'); - emitter.emit<SomeEventData>('foo', { name: 'jack' }); -} + // @ts-expect-error + emitter.emit('someEvent', 'NOT VALID'); + emitter.emit('someEvent', { name: 'jack' }); + + // @ts-expect-error + emitter.emit('foo'); + // @ts-expect-error + emitter.emit('foo', 1); + emitter.emit('foo', 'string'); + emitter.emit('bar'); + emitter.emit('bar', 1); + // @ts-expect-error + emitter.emit('bar', 'string'); +} |
