aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJack Franklin <[email protected]>2020-07-15 15:31:43 +0100
committerGitHub <[email protected]>2020-07-15 10:31:43 -0400
commit5116df46a020ae498eec6783cfe9147fc9add015 (patch)
tree93be70d93df33d1d60c7c3e4cfed0ae408e272cb /test
parent244cac2aa3d6831774129d6dc4942465dcef8099 (diff)
Add generic types and update tests (#107)
* 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 Co-authored-by: Jason Miller <[email protected]>
Diffstat (limited to 'test')
-rw-r--r--test/index_test.ts (renamed from test/index.js)6
-rw-r--r--test/test-types-compilation.ts43
-rw-r--r--test/types.ts20
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);