Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0331538

Browse files
hoebbelsBedbzn
authored andcommitted
test(cdk): fix broken tests
1 parent 2b9a873 commit 0331538

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

libs/cdk/render-strategies/spec/onStrategy.spec.ts

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
1-
import { ɵglobal } from '@angular/core';
21
import { TestScheduler } from 'rxjs/testing';
32
import { jestMatcher } from '@test-helpers';
4-
import { onStrategy, RX_NATIVE_STRATEGIES } from '@rx-angular/cdk/render-strategies';
5-
import { animationFrameScheduler, observeOn, of } from 'rxjs';
3+
import {
4+
onStrategy,
5+
RX_NATIVE_STRATEGIES,
6+
RxStrategyCredentials,
7+
} from '@rx-angular/cdk/render-strategies';
8+
import { animationFrameScheduler, observeOn, tap } from 'rxjs';
69

710
describe('onStrategy', () => {
8-
911
let testScheduler: TestScheduler;
10-
let handles = [];
11-
let animationRuns = 0;
12-
function _animate() {
13-
handles.forEach(handle => handle());
14-
}
15-
let _animationFrameRestore;
16-
let _cancelAnimationFrameRestore;
12+
const animationFrameStrategy: RxStrategyCredentials = {
13+
name: 'animationFrame',
14+
work: (cdRef) => cdRef.detectChanges(),
15+
behavior: ({ work }) => {
16+
return (o$) =>
17+
o$.pipe(
18+
observeOn(animationFrameScheduler),
19+
tap(() => work())
20+
);
21+
},
22+
};
1723
beforeEach(() => {
1824
testScheduler = new TestScheduler(jestMatcher);
19-
_animationFrameRestore = ɵglobal.requestAnimationFrame;
20-
_cancelAnimationFrameRestore = ɵglobal.cancelAnimationFrame;
21-
ɵglobal.requestAnimationFrame = (cb?) => {
22-
handles[animationRuns] = cb;
23-
animationRuns++;
24-
return animationRuns;
25-
}
26-
ɵglobal.cancelAnimationFrame = (id: number) => {
27-
handles = handles.splice(id, 1);
28-
}
29-
});
30-
31-
32-
afterEach(() => {
33-
ɵglobal.requestAnimationFrame = _animationFrameRestore;
34-
ɵglobal.cancelAnimationFrame = _cancelAnimationFrameRestore;
35-
animationRuns = 0;
36-
handles = [];
3725
});
3826

3927
it('should emit 42', () => {
40-
testScheduler.run(({expectObservable}) => {
28+
testScheduler.run(({ expectObservable }) => {
4129
const work = jest.fn();
4230
const values = { x: 42 };
4331
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.native, work);
4432
const expected = '(x|)';
45-
expectObservable(stream$).toBe(expected, values)
33+
expectObservable(stream$).toBe(expected, values);
4634
});
4735
});
4836

4937
it('should throw error', () => {
50-
testScheduler.run(({expectObservable}) => {
38+
testScheduler.run(({ expectObservable }) => {
5139
const error = new Error('error');
5240
const work = () => {
5341
throw error;
54-
}
42+
};
5543
const value = 42;
5644
const errorValues = [error, value];
5745
const expected = '#';
@@ -65,20 +53,22 @@ describe('onStrategy', () => {
6553
const work = jest.fn();
6654
animate(' --------x');
6755
const expected = '--------(x|)';
68-
of(null).pipe(observeOn(animationFrameScheduler)).subscribe(() => {
69-
_animate();
70-
});
7156
const values = { x: 42 };
72-
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.local, work);
57+
const stream$ = onStrategy(values.x, animationFrameStrategy, work);
7358
expectObservable(stream$).toBe(expected, values);
7459
});
7560
});
7661

7762
it('it should call work once when async', () => {
7863
const work = jest.fn();
79-
onStrategy(42, RX_NATIVE_STRATEGIES.local, work).subscribe();
80-
expect(work).not.toHaveBeenCalled();
81-
_animate();
64+
testScheduler.run(({ expectObservable, animate }) => {
65+
animate(' --------x');
66+
const expected = '--------(x|)';
67+
const values = { x: 42 };
68+
const stream$ = onStrategy(values.x, animationFrameStrategy, work);
69+
expect(work).not.toHaveBeenCalled();
70+
expectObservable(stream$).toBe(expected, values);
71+
});
8272
expect(work).toHaveBeenCalledTimes(1);
8373
});
8474

libs/cdk/zone-less/rxjs/spec/scheduler/asapScheduler.spec.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@ import { RxTestScheduler } from '../../src/testing/test-scheduler';
44
import { jestMatcher } from '@test-helpers';
55
import { Subscription, SchedulerAction, merge } from 'rxjs';
66
import { delay } from 'rxjs/operators';
7-
import { AsapAction } from '../../src/scheduler/asap/AsapAction';
87
import { AsapScheduler } from '../../src/scheduler/asap/AsapScheduler';
8+
import { asapScheduler } from '../../src/scheduler/asap/asap';
99

10-
let asap: AsapScheduler;
10+
const asap: AsapScheduler = asapScheduler;
1111

1212
/** @test {Scheduler} */
1313
describe('Scheduler.asap', () => {
1414
let testScheduler: RxTestScheduler;
15+
const proxyZone = window['Zone']['ProxyZoneSpec'];
1516

1617
beforeEach(() => {
1718
testScheduler = new RxTestScheduler(jestMatcher);
18-
asap = new AsapScheduler(AsapAction);
19+
window['Zone']['ProxyZoneSpec'] = undefined;
1920
jest.clearAllTimers();
2021
jest.useRealTimers();
2122
jest.clearAllMocks();
2223
});
2324

25+
afterEach(() => {
26+
window['Zone']['ProxyZoneSpec'] = proxyZone;
27+
});
28+
2429
it('should exist', () => {
2530
expect(asap).toBeDefined();
2631
});
@@ -66,6 +71,10 @@ describe('Scheduler.asap', () => {
6671
jest.useFakeTimers();
6772
// callThrough is missing from the declarations installed by the typings tool in stable
6873
const intervalSpy = jest.spyOn(intervalProvider, 'setInterval');
74+
intervalProvider.delegate = {
75+
setInterval: setInterval,
76+
clearInterval: clearInterval,
77+
};
6978
const period = 50;
7079
const state = { index: 0, period };
7180
type State = typeof state;
@@ -80,17 +89,22 @@ describe('Scheduler.asap', () => {
8089
expect(state).toHaveProperty('index', 0);
8190
expect(intervalSpy).toHaveBeenCalledTimes(1);
8291
jest.advanceTimersByTime(period);
83-
// expect(state).toHaveProperty('index', 1);
92+
expect(state).toHaveProperty('index', 1);
8493
expect(intervalSpy).toHaveBeenCalledTimes(2);
8594
jest.advanceTimersByTime(period);
8695
expect(state).toHaveProperty('index', 2);
8796
expect(intervalSpy).toHaveBeenCalledTimes(3);
97+
intervalProvider.delegate = undefined;
8898
});
8999

90100
it('should reuse the interval for recursively scheduled actions with the same delay', () => {
91101
jest.useFakeTimers();
92102
// callThrough is missing from the declarations installed by the typings tool in stable
93103
const intervalSpy = jest.spyOn(intervalProvider, 'setInterval');
104+
intervalProvider.delegate = {
105+
setInterval: setInterval,
106+
clearInterval: clearInterval,
107+
};
94108
const period = 50;
95109
const state = { index: 0, period };
96110
type State = typeof state;
@@ -103,12 +117,13 @@ describe('Scheduler.asap', () => {
103117
asap.schedule(dispatch as any, period, state);
104118
expect(state).toHaveProperty('index', 0);
105119
expect(intervalSpy).toHaveBeenCalledTimes(1);
106-
jest.advanceTimersByTime(period + 1);
120+
jest.advanceTimersByTime(period);
107121
expect(state).toHaveProperty('index', 1);
108122
expect(intervalSpy).toHaveBeenCalledTimes(1);
109123
jest.advanceTimersByTime(period);
110124
expect(state).toHaveProperty('index', 2);
111125
expect(intervalSpy).toHaveBeenCalledTimes(1);
126+
intervalProvider.delegate = undefined;
112127
});
113128

114129
it('should schedule an action to happen later', (done) => {

0 commit comments

Comments
 (0)