-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoRun.spec.ts
113 lines (106 loc) · 2.6 KB
/
autoRun.spec.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* eslint-env jest */
import {filter, skip} from 'rxjs/operators';
import SubX from '../src/index';
describe('autoRun', () => {
test('default', () => {
const p = SubX.create({number: 1});
let count = 0;
SubX.autoRun(p, () => {
count += 1;
return p.number * 10;
});
p.number = 2;
p.number = 3;
p.number = 4;
p.number = 5;
expect(count).toBe(5);
});
test('stream', () => {
const p = SubX.create({number: 1});
const stream$ = SubX.autoRun(p, () => p.number * 10);
const data: number[] = [];
stream$.subscribe(i => data.push(i));
p.number = 2;
p.number = 3;
p.number = 4;
p.number = 5;
expect(data).toEqual([10, 20, 30, 40, 50]);
});
test('stream complete', () => {
const p = SubX.create({number: 1});
let count = 0;
const stream$ = SubX.autoRun(p, () => {
count += 1;
return p.number * 10;
});
const data: number[] = [];
stream$.subscribe(i => data.push(i));
p.number = 2;
p.number = 3;
stream$.complete();
p.number = 4;
p.number = 5;
expect(count).toBe(3);
expect(data).toEqual([10, 20, 30]);
});
test('operators', () => {
const p = SubX.create({number: 1});
let count = 0;
const stream$ = SubX.autoRun(
p,
() => {
count += 1;
return p.number * 10;
},
filter(() => p.number % 2 === 1)
);
const data: number[] = [];
stream$.subscribe(i => data.push(i));
p.number = 2;
p.number = 3;
p.number = 4;
p.number = 5;
expect(count).toBe(3);
expect(data).toEqual([10, 30, 50]);
});
test('operators 2', () => {
const p = SubX.create({number: 1});
let count = 0;
const stream$ = SubX.autoRun(
p,
() => {
count += 1;
return p.number * 10;
},
filter(() => p.number % 2 === 0)
);
const data: number[] = [];
stream$.subscribe(i => data.push(i));
p.number = 2;
p.number = 3;
p.number = 4;
p.number = 5;
expect(count).toBe(3);
expect(data).toEqual([10, 20, 40]); // operators doesn't affect first value
});
test('operators 3', () => {
const p = SubX.create({number: 1});
let count = 0;
const stream$ = SubX.autoRun(
p,
() => {
count += 1;
return p.number * 10;
},
filter(() => p.number % 2 === 0)
);
const data: number[] = [];
stream$.pipe(skip(1)).subscribe(i => data.push(i));
p.number = 2;
p.number = 3;
p.number = 4;
p.number = 5;
expect(count).toBe(3);
expect(data).toEqual([20, 40]); // we explicitly skip the first value
});
});