Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(observeOn): convert observeOn tests to run mode #6209

Merged
merged 1 commit into from Apr 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
151 changes: 92 additions & 59 deletions spec/operators/subscribeOn-spec.ts
@@ -1,97 +1,131 @@
/** @prettier */
import { expect } from 'chai';
import { hot, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { subscribeOn, mergeMap, take } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { of, Observable, queueScheduler } from 'rxjs';

declare const rxTestScheduler: TestScheduler;
import { observableMatcher } from '../helpers/observableMatcher';

/** @test {subscribeOn} */
describe('subscribeOn operator', () => {
describe('subscribeOn', () => {
let testScheduler: TestScheduler;

beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});

it('should subscribe on specified scheduler', () => {
const e1 = hot('--a--b--|');
const expected = '--a--b--|';
const sub = '^ !';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--|');
const expected = '--a--b--|';
const sub = ' ^-------!';

const result = e1.pipe(subscribeOn(testScheduler));

expectObservable(e1.pipe(subscribeOn(rxTestScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should start subscribe after specified delay', () => {
const e1 = hot('--a--b--|');
const expected = '-----b--|';
const sub = ' ^ !';
testScheduler.run(({ hot, time, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--|');
const expected = ' -----b--|';
const delay = time('---| ');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const sub = ' ---^----!';

const result = e1.pipe(subscribeOn(testScheduler, delay));

expectObservable(e1.pipe(subscribeOn(rxTestScheduler, 30))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should subscribe when source raises error', () => {
const e1 = hot('--a--#');
const expected = '--a--#';
const sub = '^ !';
it('should unsubscribe when source raises error', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--#');
const expected = '--a--#';
const sub = ' ^----!';

expectObservable(e1.pipe(subscribeOn(rxTestScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
const result = e1.pipe(subscribeOn(testScheduler));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should subscribe when source is empty', () => {
const e1 = hot('----|');
const expected = '----|';
const sub = '^ !';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' ----|');
const expected = '----|';
const sub = ' ^---!';

expectObservable(e1.pipe(subscribeOn(rxTestScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
const result = e1.pipe(subscribeOn(testScheduler));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should subscribe when source does not complete', () => {
const e1 = hot('----');
const expected = '----';
const sub = '^ ';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' ----');
const expected = '----';
const sub = ' ^---';

const result = e1.pipe(subscribeOn(testScheduler));

expectObservable(e1.pipe(subscribeOn(rxTestScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should allow unsubscribing early and explicitly', () => {
const e1 = hot('--a--b--|');
const sub = '^ ! ';
const expected = '--a-- ';
const unsub = ' ! ';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--|');
const sub = ' ^---! ';
const expected = '--a-- ';
const unsub = ' ----! ';

const result = e1.pipe(subscribeOn(rxTestScheduler));
const result = e1.pipe(subscribeOn(testScheduler));

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should not break unsubscription chains when the result is unsubscribed explicitly', () => {
const e1 = hot('--a--b--|');
const sub = '^ ! ';
const expected = '--a-- ';
const unsub = ' ! ';

const result = e1.pipe(
mergeMap((x: string) => of(x)),
subscribeOn(rxTestScheduler),
mergeMap((x: string) => of(x))
);

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--|');
const sub = ' ^---! ';
const expected = '--a-- ';
const unsub = ' ----! ';

const result = e1.pipe(
mergeMap((x: string) => of(x)),
subscribeOn(testScheduler),
mergeMap((x: string) => of(x))
);

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(sub);
});
});

it('should properly support a delayTime of Infinity', () => {
const e1 = hot('--a--b--|');
const expected = '---------';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--|');
const expected = '---------';

const result = e1.pipe(subscribeOn(testScheduler, Infinity));

expectObservable(e1.pipe(subscribeOn(rxTestScheduler, Infinity))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe([]);
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe([]);
});
});

it('should stop listening to a synchronous observable when unsubscribed', () => {
const sideEffects: number[] = [];
const synchronousObservable = new Observable<number>(subscriber => {
const synchronousObservable = new Observable<number>((subscriber) => {
// This will check to see if the subscriber was closed on each loop
// when the unsubscribe hits (from the `take`), it should be closed
for (let i = 0; !subscriber.closed && i < 10; i++) {
Expand All @@ -100,10 +134,9 @@ describe('subscribeOn operator', () => {
}
});

synchronousObservable.pipe(
subscribeOn(queueScheduler),
take(3),
).subscribe(() => { /* noop */ });
synchronousObservable.pipe(subscribeOn(queueScheduler), take(3)).subscribe(() => {
/* noop */
});

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