Skip to content

Commit a400122

Browse files
chore: update observeOn tests to run mode (#6240)
1 parent 9b756f8 commit a400122

File tree

1 file changed

+77
-56
lines changed

1 file changed

+77
-56
lines changed

spec/operators/observeOn-spec.ts

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,111 @@
1+
/** @prettier */
12
import { observeOn, mergeMap, take } from 'rxjs/operators';
23
import { TestScheduler } from 'rxjs/testing';
34
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';
87

98
/** @test {observeOn} */
10-
describe('observeOn operator', () => {
9+
describe('observeOn', () => {
10+
let testScheduler: TestScheduler;
11+
12+
beforeEach(() => {
13+
testScheduler = new TestScheduler(observableMatcher);
14+
});
15+
1116
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--|';
1521

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+
});
1825
});
1926

2027
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+
});
2739
});
2840

2941
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--#';
3346

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+
});
3650
});
3751

3852
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 = '-----|';
4257

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+
});
4561
});
4662

4763
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 = '-----';
5168

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+
});
5472
});
5573

5674
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 = ' ----! ';
6180

62-
const result = e1.pipe(observeOn(rxTestScheduler));
81+
const result = e1.pipe(observeOn(testScheduler));
6382

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+
});
6686
});
6787

6888
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+
});
82104
});
83105

84106
it('should stop listening to a synchronous observable when unsubscribed', () => {
85107
const sideEffects: number[] = [];
86-
const synchronousObservable = new Observable<number>(subscriber => {
108+
const synchronousObservable = new Observable<number>((subscriber) => {
87109
// This will check to see if the subscriber was closed on each loop
88110
// when the unsubscribe hits (from the `take`), it should be closed
89111
for (let i = 0; !subscriber.closed && i < 10; i++) {
@@ -92,10 +114,9 @@ describe('observeOn operator', () => {
92114
}
93115
});
94116

95-
synchronousObservable.pipe(
96-
observeOn(queueScheduler),
97-
take(3),
98-
).subscribe(() => { /* noop */ });
117+
synchronousObservable.pipe(observeOn(queueScheduler), take(3)).subscribe(() => {
118+
/* noop */
119+
});
99120

100121
expect(sideEffects).to.deep.equal([0, 1, 2]);
101122
});

0 commit comments

Comments
 (0)