aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJack Franklin <[email protected]>2020-05-27 18:39:01 +0100
committerGitHub <[email protected]>2020-05-27 13:39:01 -0400
commiteb2be7caf6c88a41a184ef5ea6e4675f0f372771 (patch)
tree1e17bf59398b1cff97c5e2711b484edd8f9f8c8c /test
parent59cc3d1bc3b85d347baa8883b6206ba88a6071c0 (diff)
Export Mitt types for TS consumers (#101)
* Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src
Diffstat (limited to 'test')
-rw-r--r--test/index.js20
-rw-r--r--test/types.ts20
2 files changed, 37 insertions, 3 deletions
diff --git a/test/index.js b/test/index.js
index dceb4b7..a837d36 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,11 +1,25 @@
-import mitt from '../src';
+import mitt from '..';
import chai, { expect } from 'chai';
import { spy } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
-it('should default export be a function', () => {
- expect(mitt).to.be.a('function');
+describe('mitt', () => {
+ it('should default export be a function', () => {
+ expect(mitt).to.be.a('function');
+ });
+
+ it('should accept an optional event handler map', () => {
+ expect(() => mitt(new Map())).not.to.throw;
+ const map = new Map();
+ const a = spy();
+ const b = spy();
+ map.set('foo', [a, b]);
+ const events = mitt(map);
+ events.emit('foo');
+ expect(a).to.have.been.calledOnce;
+ expect(b).to.have.been.calledOnce;
+ });
});
describe('mitt#', () => {
diff --git a/test/types.ts b/test/types.ts
new file mode 100644
index 0000000..23334bb
--- /dev/null
+++ b/test/types.ts
@@ -0,0 +1,20 @@
+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);