Skip to content

Commit 60cee88

Browse files
committed
test: add specs for 'visibilitychange'
1 parent 530a8ca commit 60cee88

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

test/index.spec.ts

+55-17
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,69 @@ function assertDeepEqual(actual, expected) {
1616
}
1717
}
1818

19+
function setPageActive(isActive: boolean) {
20+
Object.defineProperty(document, 'hidden', {
21+
value: !isActive,
22+
configurable: true,
23+
});
24+
}
25+
1926
describe('Basic behaviour', function() {
20-
let listener: (event: any) => void;
2127
let scheduler: Rx.TestScheduler;
2228

2329
beforeEach(() => {
2430
Object.defineProperty(document, 'hidden', {
2531
value: false,
32+
configurable: true,
2633
});
2734

2835
scheduler = new Rx.TestScheduler(assertDeepEqual);
2936

3037
document.addEventListener = function(eventType, callback) {
31-
listener = callback;
38+
// Noop
3239
};
3340

3441
document.removeEventListener = () => void(0);
3542
document.dispatchEvent = () => void(0);
3643
});
3744

3845
test('It should poll the source$ every interval', () => {
46+
const source$ = Rx.Observable.of(1);
47+
const polling$ = polling(source$, { interval: 20 }, scheduler).take(3);
48+
const expected = '1-1-(1|)';
49+
50+
scheduler.expectObservable(polling$).toBe(expected, { 1: 1 });
51+
scheduler.flush();
52+
});
53+
54+
test('It should not poll if the tab is inactive', () => {
55+
setPageActive(false);
56+
3957
const source$ = Rx.Observable.of('Hello');
4058
const polling$ = polling(source$, { interval: 20 }, scheduler).take(3);
41-
const expected = 'x-y-(z|)';
59+
const expected = '----';
4260

43-
scheduler.expectObservable(polling$).toBe(expected, { x: 'Hello', y: 'Hello', z: 'Hello' });
61+
scheduler.expectObservable(polling$).toBe(expected);
4462
scheduler.flush();
4563
});
4664

47-
test('It should retry on error', () => {
48-
/**
49-
* This test is a bit tricky. It tests that the source$ errored only once
50-
* and that the error has been recovered. It MUST although avoid erroring twice
51-
* because `.retryWhen` doesn't reset its state after recover. This cause the
52-
* second error to continue the series of increasing delays, like 2 consequent
53-
* errors would do.
54-
* @see https://github.com/ReactiveX/rxjs/issues/1413
55-
*/
56-
const source$ = scheduler.createColdObservable('-1-2-#');
57-
const expected = '-1-2----1-(2|)';
58-
const polling$ = polling(source$, { interval: 60, esponentialUnit: 10 }, scheduler).take(4);
65+
test('It should restart polling if the tab changes to active', () => {
66+
setPageActive(false);
67+
document.addEventListener = function(eventType, listener) {
68+
// At frame 40 simulate 'visibilitychange' Event
69+
Rx.Observable.timer(40, null, scheduler)
70+
.map(() => 'event')
71+
.subscribe(() => {
72+
setPageActive(true);
73+
listener();
74+
});
75+
};
5976

60-
scheduler.expectObservable(polling$).toBe(expected, { 1: '1', 2: '2' });
77+
const source$ = Rx.Observable.of(1);
78+
const polling$ = polling(source$, { interval: 20 }, scheduler).take(1);
79+
const expected = '----(1|)';
80+
81+
scheduler.expectObservable(polling$).toBe(expected, { 1: 1 });
6182
scheduler.flush();
6283
});
6384
});
@@ -80,6 +101,23 @@ describe('Backoff behaviour', function() {
80101
document.dispatchEvent = () => void(0);
81102
});
82103

104+
test('It should retry on error', () => {
105+
/**
106+
* This test is a bit tricky. It tests that the source$ errored only once
107+
* and that the error has been recovered. It MUST although avoid erroring twice
108+
* because `.retryWhen` doesn't reset its state after recover. This cause the
109+
* second error to continue the series of increasing delays, like 2 consequent
110+
* errors would do.
111+
* @see https://github.com/ReactiveX/rxjs/issues/1413
112+
*/
113+
const source$ = scheduler.createColdObservable('-1-2-#');
114+
const expected = '-1-2----1-(2|)';
115+
const polling$ = polling(source$, { interval: 60, esponentialUnit: 10 }, scheduler).take(4);
116+
117+
scheduler.expectObservable(polling$).toBe(expected, { 1: '1', 2: '2' });
118+
scheduler.flush();
119+
});
120+
83121
test('It should retry with esponential backoff if the strategy is \'esponential\'', () => {
84122
const timerMock = jest.fn(() => {
85123
// Emit immediately

0 commit comments

Comments
 (0)