-
Notifications
You must be signed in to change notification settings - Fork 3k
/
AnimationFrameScheduler-spec.ts
171 lines (151 loc) · 5.4 KB
/
AnimationFrameScheduler-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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { animationFrameScheduler, Subscription, merge } from 'rxjs';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
import { animationFrameProvider } from 'rxjs/internal/scheduler/animationFrameProvider';
import { intervalProvider } from 'rxjs/internal/scheduler/intervalProvider';
const animationFrame = animationFrameScheduler;
/** @test {Scheduler} */
describe('Scheduler.animationFrame', () => {
let testScheduler: TestScheduler;
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});
it('should exist', () => {
expect(animationFrame).exist;
});
it('should act like the async scheduler if delay > 0', () => {
testScheduler.run(({ animate, cold, expectObservable, time }) => {
animate(' ----------x--');
const a = cold(' a ');
const ta = time(' ----| ');
const b = cold(' b ');
const tb = time(' --------| ');
const expected = '----a---b----';
const result = merge(
a.pipe(delay(ta, animationFrame)),
b.pipe(delay(tb, animationFrame))
);
expectObservable(result).toBe(expected);
});
});
it('should cancel animationFrame actions when delay > 0', () => {
testScheduler.run(({ animate, cold, expectObservable, flush, time }) => {
const requestSpy = sinon.spy(animationFrameProvider, 'requestAnimationFrame');
const setSpy = sinon.spy(intervalProvider, 'setInterval');
const clearSpy = sinon.spy(intervalProvider, 'clearInterval');
animate(' ----------x--');
const a = cold(' a ');
const ta = time(' ----| ');
const subs = ' ^-! ';
const expected = '-------------';
const result = merge(
a.pipe(delay(ta, animationFrame))
);
expectObservable(result, subs).toBe(expected);
flush();
expect(requestSpy).to.have.not.been.called;
expect(setSpy).to.have.been.calledOnce;
expect(clearSpy).to.have.been.calledOnce;
requestSpy.restore();
setSpy.restore();
clearSpy.restore();
});
});
it('should schedule an action to happen later', (done) => {
let actionHappened = false;
animationFrame.schedule(() => {
actionHappened = true;
done();
});
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
}
});
it('should execute recursively scheduled actions in separate asynchronous contexts', (done) => {
let syncExec1 = true;
let syncExec2 = true;
animationFrame.schedule(function (index) {
if (index === 0) {
this.schedule(1);
animationFrame.schedule(() => { syncExec1 = false; });
} else if (index === 1) {
this.schedule(2);
animationFrame.schedule(() => { syncExec2 = false; });
} else if (index === 2) {
this.schedule(3);
} else if (index === 3) {
if (!syncExec1 && !syncExec2) {
done();
} else {
done(new Error('Execution happened synchronously.'));
}
}
}, 0, 0);
});
it('should cancel the animation frame if all scheduled actions unsubscribe before it executes', (done) => {
let animationFrameExec1 = false;
let animationFrameExec2 = false;
const action1 = animationFrame.schedule(() => { animationFrameExec1 = true; });
const action2 = animationFrame.schedule(() => { animationFrameExec2 = true; });
expect(animationFrame._scheduled).to.exist;
expect(animationFrame.actions.length).to.equal(2);
action1.unsubscribe();
action2.unsubscribe();
expect(animationFrame.actions.length).to.equal(0);
expect(animationFrame._scheduled).to.equal(undefined);
animationFrame.schedule(() => {
expect(animationFrameExec1).to.equal(false);
expect(animationFrameExec2).to.equal(false);
done();
});
});
it('should execute the rest of the scheduled actions if the first action is canceled', (done) => {
let actionHappened = false;
let secondSubscription: Subscription | null = null;
const firstSubscription = animationFrame.schedule(() => {
actionHappened = true;
if (secondSubscription) {
secondSubscription.unsubscribe();
}
done(new Error('The first action should not have executed.'));
});
secondSubscription = animationFrame.schedule(() => {
if (!actionHappened) {
done();
}
});
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
} else {
firstSubscription.unsubscribe();
}
});
it('should not execute rescheduled actions when flushing', (done) => {
let flushCount = 0;
let scheduledIndices: number[] = [];
let originalFlush = animationFrame.flush;
animationFrame.flush = (...args) => {
++flushCount;
originalFlush.apply(animationFrame, args);
if (flushCount === 2) {
animationFrame.flush = originalFlush;
try {
expect(scheduledIndices).to.deep.equal([0, 1]);
done();
} catch (error) {
done(error);
}
}
};
animationFrame.schedule(function (index) {
if (flushCount < 2) {
this.schedule(index! + 1);
scheduledIndices.push(index! + 1);
}
}, 0, 0);
scheduledIndices.push(0);
});
});