1+ /** @prettier */
12import { expect } from 'chai' ;
23import { mapTo , mergeMap , take } from 'rxjs/operators' ;
3- import { hot , cold , expectObservable , expectSubscriptions } from '../helpers/marble- testing' ;
4+ import { TestScheduler } from 'rxjs/ testing' ;
45import { of , Observable } from 'rxjs' ;
6+ import { observableMatcher } from '../helpers/observableMatcher' ;
57
68/** @test {mapTo} */
7- describe ( 'mapTo operator' , ( ) => {
9+ describe ( 'mapTo' , ( ) => {
10+ let testScheduler : TestScheduler ;
11+
12+ beforeEach ( ( ) => {
13+ testScheduler = new TestScheduler ( observableMatcher ) ;
14+ } ) ;
15+
816 it ( 'should map multiple values' , ( ) => {
9- const a = cold ( '--1--2--3--|' ) ;
10- const asubs = '^ !' ;
11- const expected = '--a--a--a--|' ;
17+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
18+ const e1 = cold ( ' --1--2--3--|' ) ;
19+ const e1subs = ' ^----------!' ;
20+ const expected = '--a--a--a--|' ;
1221
13- expectObservable ( a . pipe ( mapTo ( 'a' ) ) ) . toBe ( expected ) ;
14- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
22+ expectObservable ( e1 . pipe ( mapTo ( 'a' ) ) ) . toBe ( expected ) ;
23+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
24+ } ) ;
1525 } ) ;
1626
1727 it ( 'should map one value' , ( ) => {
18- const a = cold ( '--7--|' ) ;
19- const asubs = '^ !' ;
20- const expected = '--y--|' ;
28+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
29+ const e1 = cold ( ' --7--|' ) ;
30+ const e1subs = ' ^----!' ;
31+ const expected = '--y--|' ;
2132
22- expectObservable ( a . pipe ( mapTo ( 'y' ) ) ) . toBe ( expected ) ;
23- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
33+ expectObservable ( e1 . pipe ( mapTo ( 'y' ) ) ) . toBe ( expected ) ;
34+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
35+ } ) ;
2436 } ) ;
2537
2638 it ( 'should allow unsubscribing explicitly and early' , ( ) => {
27- const a = cold ( '--1--2--3--|' ) ;
28- const unsub = ' ! ' ;
29- const asubs = '^ ! ' ;
30- const expected = '--x--x- ' ;
31-
32- expectObservable ( a . pipe ( mapTo ( 'x' ) ) , unsub ) . toBe ( expected ) ;
33- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
39+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
40+ const e1 = cold ( ' --1--2--3--|' ) ;
41+ const e1subs = ' ^-----! ' ;
42+ const expected = '--x--x- ' ;
43+ const unsub = ' ------! ' ;
44+
45+ expectObservable ( e1 . pipe ( mapTo ( 'x' ) ) , unsub ) . toBe ( expected ) ;
46+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
47+ } ) ;
3448 } ) ;
3549
3650 it ( 'should propagate errors from observable that emits only errors' , ( ) => {
37- const a = cold ( '--#' , undefined , 'too bad' ) ;
38- const asubs = '^ !' ;
39- const expected = '--#' ;
51+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
52+ const e1 = cold ( ' --#' , undefined , 'too bad' ) ;
53+ const e1subs = ' ^-!' ;
54+ const expected = '--#' ;
4055
41- expectObservable ( a . pipe ( mapTo ( 1 ) ) ) . toBe ( expected , null , 'too bad' ) ;
42- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
56+ expectObservable ( e1 . pipe ( mapTo ( 1 ) ) ) . toBe ( expected , null , 'too bad' ) ;
57+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
58+ } ) ;
4359 } ) ;
4460
45- it ( 'should propagate errors from observable that emit values' , ( ) => {
46- const a = cold ( '--1--2--#' , undefined , 'too bad' ) ;
47- const asubs = '^ !' ;
48- const expected = '--x--x--#' ;
61+ it ( 'should propagate errors from observable that emit values, then errors' , ( ) => {
62+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
63+ const e1 = cold ( ' --1--2--#' , undefined , 'too bad' ) ;
64+ const e1subs = ' ^-------!' ;
65+ const expected = '--x--x--#' ;
4966
50- expectObservable ( a . pipe ( mapTo ( 'x' ) ) ) . toBe ( expected , undefined , 'too bad' ) ;
51- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
67+ expectObservable ( e1 . pipe ( mapTo ( 'x' ) ) ) . toBe ( expected , undefined , 'too bad' ) ;
68+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
69+ } ) ;
5270 } ) ;
5371
5472 it ( 'should not map an empty observable' , ( ) => {
55- const a = cold ( '|' ) ;
56- const asubs = '(^!)' ;
57- const expected = '|' ;
73+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
74+ const e1 = cold ( ' | ' ) ;
75+ const e1subs = ' (^!)' ;
76+ const expected = '| ' ;
5877
59- expectObservable ( a . pipe ( mapTo ( - 1 ) ) ) . toBe ( expected ) ;
60- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
78+ expectObservable ( e1 . pipe ( mapTo ( - 1 ) ) ) . toBe ( expected ) ;
79+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
80+ } ) ;
6181 } ) ;
6282
6383 it ( 'should map twice' , ( ) => {
64- const a = hot ( '-0----1-^-2---3--4-5--6--7-8-|' ) ;
65- const asubs = '^ !' ;
66- const expected = '--h---h--h-h--h--h-h-|' ;
67-
68- const r = a . pipe (
69- mapTo ( - 1 ) ,
70- mapTo ( 'h' )
71- ) ;
72-
73- expectObservable ( r ) . toBe ( expected ) ;
74- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
84+ testScheduler . run ( ( { hot, expectObservable, expectSubscriptions } ) => {
85+ const e1 = hot ( '-0----1-^-2---3--4-5--6--7-8-|' ) ;
86+ const e1subs = ' ^--------------------!' ;
87+ const expected = ' --h---h--h-h--h--h-h-|' ;
88+
89+ // prettier-ignore
90+ const result = e1 . pipe (
91+ mapTo ( - 1 ) ,
92+ mapTo ( 'h' )
93+ ) ;
94+
95+ expectObservable ( result ) . toBe ( expected ) ;
96+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
97+ } ) ;
7598 } ) ;
7699
77100 it ( 'should not break unsubscription chain when unsubscribed explicitly' , ( ) => {
78- const a = cold ( '--1--2--3--|' ) ;
79- const unsub = ' ! ' ;
80- const asubs = '^ ! ' ;
81- const expected = '--x--x- ' ;
82-
83- const r = a . pipe (
84- mergeMap ( ( x : string ) => of ( x ) ) ,
85- mapTo ( 'x' ) ,
86- mergeMap ( ( x : string ) => of ( x ) )
87- ) ;
88-
89- expectObservable ( r , unsub ) . toBe ( expected ) ;
90- expectSubscriptions ( a . subscriptions ) . toBe ( asubs ) ;
101+ testScheduler . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
102+ const e1 = cold ( ' --1--2--3--|' ) ;
103+ const e1subs = ' ^-----! ' ;
104+ const expected = '--x--x- ' ;
105+ const unsub = ' ------! ' ;
106+
107+ const result = e1 . pipe (
108+ mergeMap ( ( x : string ) => of ( x ) ) ,
109+ mapTo ( 'x' ) ,
110+ mergeMap ( ( x : string ) => of ( x ) )
111+ ) ;
112+
113+ expectObservable ( result , unsub ) . toBe ( expected ) ;
114+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
115+ } ) ;
91116 } ) ;
92117
93118 it ( 'should stop listening to a synchronous observable when unsubscribed' , ( ) => {
94119 const sideEffects : number [ ] = [ ] ;
95- const synchronousObservable = new Observable < number > ( subscriber => {
120+ const synchronousObservable = new Observable < number > ( ( subscriber ) => {
96121 // This will check to see if the subscriber was closed on each loop
97122 // when the unsubscribe hits (from the `take`), it should be closed
98123 for ( let i = 0 ; ! subscriber . closed && i < 10 ; i ++ ) {
@@ -101,10 +126,9 @@ describe('mapTo operator', () => {
101126 }
102127 } ) ;
103128
104- synchronousObservable . pipe (
105- mapTo ( 0 ) ,
106- take ( 3 ) ,
107- ) . subscribe ( ( ) => { /* noop */ } ) ;
129+ synchronousObservable . pipe ( mapTo ( 0 ) , take ( 3 ) ) . subscribe ( ( ) => {
130+ /* noop */
131+ } ) ;
108132
109133 expect ( sideEffects ) . to . deep . equal ( [ 0 , 1 , 2 ] ) ;
110134 } ) ;
0 commit comments