-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventListener.test.ts
57 lines (51 loc) · 1.86 KB
/
EventListener.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { expect } from 'chai';
import 'mocha';
import { EventListener } from '../../src';
describe('EventListener', () => {
let listenerResult;
const callback = (data) => {
listenerResult = data;
};
beforeEach(function () {
listenerResult = null;
});
describe('#constructor()', function () {
it('should create a new EventListener object', function () {
expect(new EventListener('test', callback, null)).to.have.keys([
'namespace',
'callback',
'once',
'emitter'
]);
});
});
describe('#execute()', function () {
it('should execute the callback', function () {
new EventListener('test', callback, null).execute(this, true);
expect(listenerResult).to.equal(true);
});
});
describe('#close()', function () {
it('should remove itself from `emitter`', function () {
const listener = new EventListener('test', callback, null);
listener.open();
expect(listener.emitter.listeners.length).to.equal(1);
listener.close();
expect(listener.emitter.listeners.length).to.equal(0);
});
});
describe('#open()', function () {
it('should add itself back to `emitter` (EventEmitter)', function () {
const listener = new EventListener('test', callback);
listener.open(true);
expect(listener.emitter.listeners.length).to.equal(1);
});
});
describe('#once', function () {
it('should only execute the listener once and remove itself from `emitter`', function () {
const listener = new EventListener('test', callback, null);
listener.execute(this, true);
expect(listener.emitter.listeners.length).to.equal(0);
});
});
});