forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval-spec.ts
184 lines (170 loc) · 5.73 KB
/
interval-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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @prettier */
import { expect } from 'chai';
import { NEVER, interval, asapScheduler, animationFrameScheduler, queueScheduler } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { take, concat } from 'rxjs/operators';
import * as sinon from 'sinon';
import { observableMatcher } from '../helpers/observableMatcher';
/** @test {interval} */
describe('interval', () => {
let rxTestScheduler: TestScheduler;
beforeEach(() => {
rxTestScheduler = new TestScheduler(observableMatcher);
});
it('should create an observable emitting periodically', () => {
rxTestScheduler.run(({ expectObservable, time }) => {
const values = {
a: 0,
b: 1,
c: 2,
d: 3,
e: 4,
f: 5,
};
const period = time('--| ');
// --|
// --|
// --|
// --|
// --|
// --|
const expected = ' --a-b-c-d-e-f-';
const e1 = interval(period).pipe(
take(6), // make it actually finite, so it can be rendered
concat(NEVER) // but pretend it's infinite by not completing
);
expectObservable(e1).toBe(expected, values);
});
});
it('should set up an interval', () => {
rxTestScheduler.run(({ expectObservable, time }) => {
const period = time('----------| ');
// ----------|
// ----------|
// ----------|
// ----------|
// ----------|
// ----------|
const unsubs = ' ---------------------------------------------------------------------------!';
const expected = ' ----------0---------1---------2---------3---------4---------5---------6-----';
expectObservable(interval(period), unsubs).toBe(expected, [0, 1, 2, 3, 4, 5, 6]);
});
});
it('should emit when relative interval set to zero', () => {
rxTestScheduler.run(({ expectObservable, time }) => {
const period = time('| ');
const expected = ' (0123456|)';
const e1 = interval(period).pipe(take(7));
expectObservable(e1).toBe(expected, [0, 1, 2, 3, 4, 5, 6]);
});
});
it('should consider negative interval as zero', () => {
rxTestScheduler.run(({ expectObservable }) => {
const expected = '(0123456|)';
const e1 = interval(-1).pipe(take(7));
expectObservable(e1).toBe(expected, [0, 1, 2, 3, 4, 5, 6]);
});
});
it('should emit values until unsubscribed', (done) => {
const values: number[] = [];
const expected = [0, 1, 2, 3, 4, 5, 6];
const e1 = interval(5);
const subscription = e1.subscribe({
next: (x: number) => {
values.push(x);
if (x === 6) {
subscription.unsubscribe();
expect(values).to.deep.equal(expected);
done();
}
},
error: (err: any) => {
done(new Error('should not be called'));
},
complete: () => {
done(new Error('should not be called'));
},
});
});
it('should create an observable emitting periodically with the AsapScheduler', (done) => {
const sandbox = sinon.createSandbox();
const fakeTimer = sandbox.useFakeTimers();
const period = 10;
const events = [0, 1, 2, 3, 4, 5];
const source = interval(period, asapScheduler).pipe(take(6));
source.subscribe({
next(x) {
expect(x).to.equal(events.shift());
},
error(e) {
sandbox.restore();
done(e);
},
complete() {
expect(asapScheduler.actions.length).to.equal(0);
expect(asapScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
},
});
let i = -1,
n = events.length;
while (++i < n) {
fakeTimer.tick(period);
}
});
it('should create an observable emitting periodically with the QueueScheduler', (done) => {
const sandbox = sinon.createSandbox();
const fakeTimer = sandbox.useFakeTimers();
const period = 10;
const events = [0, 1, 2, 3, 4, 5];
const source = interval(period, queueScheduler).pipe(take(6));
source.subscribe({
next(x) {
expect(x).to.equal(events.shift());
},
error(e) {
sandbox.restore();
done(e);
},
complete() {
expect(queueScheduler.actions.length).to.equal(0);
expect(queueScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
},
});
let i = -1,
n = events.length;
while (++i < n) {
fakeTimer.tick(period);
}
});
it('should create an observable emitting periodically with the AnimationFrameScheduler', (done) => {
const sandbox = sinon.createSandbox();
const fakeTimer = sandbox.useFakeTimers();
const period = 10;
const events = [0, 1, 2, 3, 4, 5];
const source = interval(period, animationFrameScheduler).pipe(take(6));
source.subscribe({
next(x) {
expect(x).to.equal(events.shift());
},
error(e) {
sandbox.restore();
done(e);
},
complete() {
expect(animationFrameScheduler.actions.length).to.equal(0);
expect(animationFrameScheduler._scheduled).to.equal(undefined);
sandbox.restore();
done();
},
});
let i = -1,
n = events.length;
while (++i < n) {
fakeTimer.tick(period);
}
});
});