1
+ /** @prettier */
1
2
import { observeOn , mergeMap , take } from 'rxjs/operators' ;
2
3
import { TestScheduler } from 'rxjs/testing' ;
3
4
import { expect } from 'chai' ;
4
- import { hot , expectObservable , expectSubscriptions } from '../helpers/marble-testing' ;
5
- import { of , Observable , asapScheduler , queueScheduler } from 'rxjs' ;
6
-
7
- declare const rxTestScheduler : TestScheduler ;
5
+ import { of , Observable , queueScheduler } from 'rxjs' ;
6
+ import { observableMatcher } from '../helpers/observableMatcher' ;
8
7
9
8
/** @test {observeOn} */
10
- describe ( 'observeOn operator' , ( ) => {
9
+ describe ( 'observeOn' , ( ) => {
10
+ let testScheduler : TestScheduler ;
11
+
12
+ beforeEach ( ( ) => {
13
+ testScheduler = new TestScheduler ( observableMatcher ) ;
14
+ } ) ;
15
+
11
16
it ( 'should observe on specified scheduler' , ( ) => {
12
- const e1 = hot ( '--a--b--|' ) ;
13
- const expected = '--a--b--|' ;
14
- const sub = '^ !' ;
17
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
18
+ const e1 = hot ( ' --a--b--|' ) ;
19
+ const e1subs = ' ^-------!' ;
20
+ const expected = '--a--b--|' ;
15
21
16
- expectObservable ( e1 . pipe ( observeOn ( rxTestScheduler ) ) ) . toBe ( expected ) ;
17
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
22
+ expectObservable ( e1 . pipe ( observeOn ( testScheduler ) ) ) . toBe ( expected ) ;
23
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
24
+ } ) ;
18
25
} ) ;
19
26
20
27
it ( 'should observe after specified delay' , ( ) => {
21
- const e1 = hot ( '--a--b--| ' ) ;
22
- const expected = '-----a--b--|' ;
23
- const sub = '^ ! ' ;
24
-
25
- expectObservable ( e1 . pipe ( observeOn ( rxTestScheduler , 30 ) ) ) . toBe ( expected ) ;
26
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
28
+ testScheduler . run ( ( { hot, time, expectObservable, expectSubscriptions } ) => {
29
+ const e1 = hot ( ' --a----b-| ' ) ;
30
+ const e1subs = ' ^--------! ' ;
31
+ const delay = time ( ' ---| ' ) ;
32
+ // ---|
33
+ // ---|
34
+ const expected = ' -----a----b-|' ;
35
+
36
+ expectObservable ( e1 . pipe ( observeOn ( testScheduler , delay ) ) ) . toBe ( expected ) ;
37
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
38
+ } ) ;
27
39
} ) ;
28
40
29
41
it ( 'should observe when source raises error' , ( ) => {
30
- const e1 = hot ( '--a--#' ) ;
31
- const expected = '--a--#' ;
32
- const sub = '^ !' ;
42
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
43
+ const e1 = hot ( ' --a--#' ) ;
44
+ const e1subs = ' ^----!' ;
45
+ const expected = '--a--#' ;
33
46
34
- expectObservable ( e1 . pipe ( observeOn ( rxTestScheduler ) ) ) . toBe ( expected ) ;
35
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
47
+ expectObservable ( e1 . pipe ( observeOn ( testScheduler ) ) ) . toBe ( expected ) ;
48
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
49
+ } ) ;
36
50
} ) ;
37
51
38
52
it ( 'should observe when source is empty' , ( ) => {
39
- const e1 = hot ( '-----|' ) ;
40
- const expected = '-----|' ;
41
- const sub = '^ !' ;
53
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
54
+ const e1 = hot ( ' -----|' ) ;
55
+ const e1subs = ' ^----!' ;
56
+ const expected = '-----|' ;
42
57
43
- expectObservable ( e1 . pipe ( observeOn ( rxTestScheduler ) ) ) . toBe ( expected ) ;
44
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
58
+ expectObservable ( e1 . pipe ( observeOn ( testScheduler ) ) ) . toBe ( expected ) ;
59
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
60
+ } ) ;
45
61
} ) ;
46
62
47
63
it ( 'should observe when source does not complete' , ( ) => {
48
- const e1 = hot ( '-----' ) ;
49
- const expected = '-----' ;
50
- const sub = '^ ' ;
64
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
65
+ const e1 = hot ( ' -----' ) ;
66
+ const e1subs = ' ^----' ;
67
+ const expected = '-----' ;
51
68
52
- expectObservable ( e1 . pipe ( observeOn ( rxTestScheduler ) ) ) . toBe ( expected ) ;
53
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
69
+ expectObservable ( e1 . pipe ( observeOn ( testScheduler ) ) ) . toBe ( expected ) ;
70
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
71
+ } ) ;
54
72
} ) ;
55
73
56
74
it ( 'should allow unsubscribing early and explicitly' , ( ) => {
57
- const e1 = hot ( '--a--b--|' ) ;
58
- const sub = '^ ! ' ;
59
- const expected = '--a-- ' ;
60
- const unsub = ' ! ' ;
75
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
76
+ const e1 = hot ( ' --a--b--|' ) ;
77
+ const e1subs = ' ^---! ' ;
78
+ const expected = '--a-- ' ;
79
+ const unsub = ' ----! ' ;
61
80
62
- const result = e1 . pipe ( observeOn ( rxTestScheduler ) ) ;
81
+ const result = e1 . pipe ( observeOn ( testScheduler ) ) ;
63
82
64
- expectObservable ( result , unsub ) . toBe ( expected ) ;
65
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
83
+ expectObservable ( result , unsub ) . toBe ( expected ) ;
84
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
85
+ } ) ;
66
86
} ) ;
67
87
68
88
it ( 'should not break unsubscription chains when the result is unsubscribed explicitly' , ( ) => {
69
- const e1 = hot ( '--a--b--|' ) ;
70
- const sub = '^ ! ' ;
71
- const expected = '--a-- ' ;
72
- const unsub = ' ! ' ;
73
-
74
- const result = e1 . pipe (
75
- mergeMap ( ( x : string ) => of ( x ) ) ,
76
- observeOn ( rxTestScheduler ) ,
77
- mergeMap ( ( x : string ) => of ( x ) )
78
- ) ;
79
-
80
- expectObservable ( result , unsub ) . toBe ( expected ) ;
81
- expectSubscriptions ( e1 . subscriptions ) . toBe ( sub ) ;
89
+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
90
+ const e1 = hot ( ' --a--b--|' ) ;
91
+ const e1subs = ' ^---! ' ;
92
+ const expected = '--a-- ' ;
93
+ const unsub = ' ----! ' ;
94
+
95
+ const result = e1 . pipe (
96
+ mergeMap ( ( x ) => of ( x ) ) ,
97
+ observeOn ( testScheduler ) ,
98
+ mergeMap ( ( x ) => of ( x ) )
99
+ ) ;
100
+
101
+ expectObservable ( result , unsub ) . toBe ( expected ) ;
102
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
103
+ } ) ;
82
104
} ) ;
83
105
84
106
it ( 'should stop listening to a synchronous observable when unsubscribed' , ( ) => {
85
107
const sideEffects : number [ ] = [ ] ;
86
- const synchronousObservable = new Observable < number > ( subscriber => {
108
+ const synchronousObservable = new Observable < number > ( ( subscriber ) => {
87
109
// This will check to see if the subscriber was closed on each loop
88
110
// when the unsubscribe hits (from the `take`), it should be closed
89
111
for ( let i = 0 ; ! subscriber . closed && i < 10 ; i ++ ) {
@@ -92,10 +114,9 @@ describe('observeOn operator', () => {
92
114
}
93
115
} ) ;
94
116
95
- synchronousObservable . pipe (
96
- observeOn ( queueScheduler ) ,
97
- take ( 3 ) ,
98
- ) . subscribe ( ( ) => { /* noop */ } ) ;
117
+ synchronousObservable . pipe ( observeOn ( queueScheduler ) , take ( 3 ) ) . subscribe ( ( ) => {
118
+ /* noop */
119
+ } ) ;
99
120
100
121
expect ( sideEffects ) . to . deep . equal ( [ 0 , 1 , 2 ] ) ;
101
122
} ) ;
0 commit comments