forked from montserat/event-emitter-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.ts
58 lines (48 loc) · 1.54 KB
/
tests.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
58
import { EventEmitter } from "./emitter";
import { EventStatistics } from "./event-statistics";
import { ResultsTester } from "./results-tester";
import { triggerRandomly } from "./utils";
enum EventName {
EventSuccess = "Success",
EventFail = "Fail",
}
const EVENT_NAMES = [EventName.EventSuccess, EventName.EventFail];
function assertItWorks() {
const emitter = new EventEmitter<EventName>();
triggerRandomly(() => emitter.emit(EventName.EventSuccess), 300);
triggerRandomly(() => emitter.emit(EventName.EventFail), 300);
const repository = new EventStatistics<EventName>();
const handler = new EventStatistics<EventName>();
emitter.subscribe(EventName.EventSuccess, () => {
handler.setStats(
EventName.EventSuccess,
handler.getStats(EventName.EventSuccess) + 1
);
repository.setStats(
EventName.EventSuccess,
repository.getStats(EventName.EventSuccess) + 1
);
});
emitter.subscribe(EventName.EventFail, () => {
handler.setStats(
EventName.EventFail,
handler.getStats(EventName.EventFail) + 1
);
});
const resultsTester = new ResultsTester({
eventNames: EVENT_NAMES,
emitter,
handler,
repository,
});
resultsTester.showStats().then((map) => {
if (map.get(EventName.EventSuccess) !== true) {
throw new Error(`Assert(EventName.EventSuccess === true) error`);
}
if (map.get(EventName.EventFail) !== false) {
throw new Error(`Assert(EventName.EventSuccess === false) error`);
}
console.log("Tests finished");
});
}
assertItWorks();