-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathindex.spec.ts
More file actions
59 lines (54 loc) · 1.32 KB
/
index.spec.ts
File metadata and controls
59 lines (54 loc) · 1.32 KB
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
58
59
import { Observable } from '.';
describe('Observable', () => {
describe('once', () => {
let observable: Observable;
let handler: () => void;
let callCount = 0;
beforeEach(() => {
handler = function () {
callCount++;
};
observable = new Observable();
observable.once('test', handler);
});
afterEach(() => {
callCount = 0;
handler = null;
observable = null;
});
function notify() {
observable.notify({ eventName: 'test', object: observable });
}
function notifyWrong() {
observable.notify({ eventName: 'test2', object: observable });
}
it('fires just once', () => {
notify();
notify();
expect(callCount).toBe(1);
});
it('does not fire for other events', () => {
notifyWrong();
expect(callCount).toBe(0);
});
});
describe('once', () => {
it('fire once when fired recursively', () => {
const observable = new Observable();
let callCount1 = 0;
let callCount2 = 0;
const handler2 = function () {
callCount2++;
};
const handler1 = function () {
callCount1++;
observable.once('test', handler2);
observable.notify({ eventName: 'test', object: observable });
};
observable.once('test', handler1);
observable.notify({ eventName: 'test', object: observable });
expect(callCount1).toBe(1);
expect(callCount2).toBe(1);
});
});
});