Repository navigation
Expand file tree
/
Copy pathindex.test.js
More file actions
87 lines (78 loc) · 2.03 KB
/
Copy pathindex.test.js
File metadata and controls
87 lines (78 loc) · 2.03 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { on, once, emit, unsubscribeOf } = require("./index");
const expect = (process, desc) => {
if (process()) console.log('✅', desc);
else console.log('❌', desc);
return expect;
}
const INC = 'INC';
const DEC = 'DEC';
let refunds;
let counter = 0;
const unsubscribeINC = on(INC, () => {
counter++
return counter;
});
const incremental = () => {
counter++
return counter;
}
on(INC, incremental);
once(DEC, () => {
counter--;
return counter;
});
/**
* expects:
* [action] [variable] [result] [desc]
* S1: counter 0 nothing
* S2:emit(INC) counter 2 two unit
* S2.1 refunds [1,2] ...
* S3:emit(DEC) counter 1 one unit
* S4:unsubscribeINC() counter 1 don't effect of anything
* S5:emit(INC) counter 2 just one more
* S6:emit(DEC) counter 2 without any effect because it's once
* S7:unsubscribeOf(incremental) counter 2 don't effect of anything
* S8:emit(INC) counter 2 without effect because doesn't has listener
* S8.1 refunds [] ...
*/
expect
(() => {
// nothing
return counter === 0
}, 'S1')
(() => {
refunds = emit(INC)
return counter === 2
}, 'S2')
(() => {
// nothing
return refunds[0] === 1 && refunds[1] === 2 && refunds.length === 2
}, 'S2.1')
(() => {
emit(DEC)
return counter === 1
}, 'S3')
(() => {
unsubscribeINC()
return counter === 1
}, 'S4')
(() => {
emit(INC)
return counter === 2
}, 'S5')
(() => {
emit(DEC)
return counter === 2
}, 'S6')
(() => {
unsubscribeOf(INC, incremental)
return counter === 2
}, 'S7')
(() => {
refunds = emit(INC)
return counter === 2
}, 'S8')
(() => {
// nothing
return refunds.length === 0
}, 'S8.1');