@@ -16,48 +16,69 @@ function assertDeepEqual(actual, expected) {
16
16
}
17
17
}
18
18
19
+ function setPageActive ( isActive : boolean ) {
20
+ Object . defineProperty ( document , 'hidden' , {
21
+ value : ! isActive ,
22
+ configurable : true ,
23
+ } ) ;
24
+ }
25
+
19
26
describe ( 'Basic behaviour' , function ( ) {
20
- let listener : ( event : any ) => void ;
21
27
let scheduler : Rx . TestScheduler ;
22
28
23
29
beforeEach ( ( ) => {
24
30
Object . defineProperty ( document , 'hidden' , {
25
31
value : false ,
32
+ configurable : true ,
26
33
} ) ;
27
34
28
35
scheduler = new Rx . TestScheduler ( assertDeepEqual ) ;
29
36
30
37
document . addEventListener = function ( eventType , callback ) {
31
- listener = callback ;
38
+ // Noop
32
39
} ;
33
40
34
41
document . removeEventListener = ( ) => void ( 0 ) ;
35
42
document . dispatchEvent = ( ) => void ( 0 ) ;
36
43
} ) ;
37
44
38
45
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
+
39
57
const source$ = Rx . Observable . of ( 'Hello' ) ;
40
58
const polling$ = polling ( source$ , { interval : 20 } , scheduler ) . take ( 3 ) ;
41
- const expected = 'x-y-(z|) ' ;
59
+ const expected = '---- ' ;
42
60
43
- scheduler . expectObservable ( polling$ ) . toBe ( expected , { x : 'Hello' , y : 'Hello' , z : 'Hello' } ) ;
61
+ scheduler . expectObservable ( polling$ ) . toBe ( expected ) ;
44
62
scheduler . flush ( ) ;
45
63
} ) ;
46
64
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
+ } ;
59
76
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 } ) ;
61
82
scheduler . flush ( ) ;
62
83
} ) ;
63
84
} ) ;
@@ -80,6 +101,23 @@ describe('Backoff behaviour', function() {
80
101
document . dispatchEvent = ( ) => void ( 0 ) ;
81
102
} ) ;
82
103
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
+
83
121
test ( 'It should retry with esponential backoff if the strategy is \'esponential\'' , ( ) => {
84
122
const timerMock = jest . fn ( ( ) => {
85
123
// Emit immediately
0 commit comments