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

test(take): refactor test use rxsandbox #3266

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"rollup-plugin-inject": "^2.0.0",
"rollup-plugin-node-resolve": "^2.0.0",
"rx": "latest",
"rx-sandbox": "0.0.10",
"rxjs": "latest",
"shx": "^0.2.2",
"sinon": "^2.1.0",
Expand Down
164 changes: 89 additions & 75 deletions spec/operators/take-spec.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,154 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;

const Subject = Rx.Subject;
const Observable = Rx.Observable;
import { hotObservable, coldObservable, getObservableMessage, expectedObservable, expectedSubscription, rxSandbox } from 'rx-sandbox';
import { take, mergeMap } from '../../src/operators';
import { ArgumentOutOfRangeError } from '../../src/internal/util/ArgumentOutOfRangeError';
import { Observable } from '../../src/internal/Observable';
import { Subject } from '../../src/internal/Subject';
import { of } from '../../src/create';
import { Observer } from '../../src/internal/Observer';
const { marbleAssert } = rxSandbox;

/** @test {take} */
describe('Observable.prototype.take', () => {
asDiagram('take(2)')('should take two values of an observable with many values', () => {
const e1 = cold('--a-----b----c---d--|');
const e1subs = '^ ! ';
const expected = '--a-----(b|) ';
let hot: hotObservable, cold: coldObservable, getMessages: getObservableMessage, e: expectedObservable, s: expectedSubscription;
beforeEach(() => ({ hot, cold, e, s, getMessages } = rxSandbox.create(true)));

it('should take two values of an observable with many values', () => {
const e1 = cold(' --a-----b----c---d--|');
const e1subs = s(' ^-------! ');
const expected = e('--a-----(b|) ');

expectObservable(e1.take(2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(2)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should work with empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '|';
const e1 = cold('|');
const e1subs = s('(^!)');
const expected = e('|');

expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should go on forever on never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';
const e1 = cold(' -');
const e1subs = s(' ^');
const expected = e('-');

expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should be empty on take(0)', () => {
const e1 = hot('--a--^--b----c---d--|');
const e1subs = []; // Don't subscribe at all
const expected = '|';
const expected = e('|');

expectObservable(e1.take(0)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(0)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal(e1subs);
});

it('should take one value of an observable with one value', () => {
const e1 = hot('---(a|)');
const e1subs = '^ ! ';
const expected = '---(a|)';
const e1 = hot(' ---(a|)');
const e1subs = s(' ^--! ');
const expected = e('---(a|)');

expectObservable(e1.take(1)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(1)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should take one values of an observable with many values', () => {
const e1 = hot('--a--^--b----c---d--|');
const e1subs = '^ ! ';
const expected = '---(b|) ';
const e1subs = s(' ^--! ');
const expected = e(' ---(b|) ');

expectObservable(e1.take(1)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(1)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should error on empty', () => {
const e1 = hot('--a--^----|');
const e1subs = '^ !';
const expected = '-----|';
const e1subs = s(' ^----!');
const expected = e(' -----|');

expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should propagate error from the source observable', () => {
const e1 = hot('---^---#', null, 'too bad');
const e1subs = '^ !';
const expected = '----#';
const e1subs = s(' ^---!');
const expected = e('----#', null, 'too bad');

expectObservable(e1.take(42)).toBe(expected, null, 'too bad');
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should propagate error from an observable with values', () => {
const e1 = hot('---^--a--b--#');
const e1subs = '^ !';
const expected = '---a--b--#';
const e1 = hot(' ---^--a--b--#');
const e1subs = s(' ^--------!');
const expected = e('---a--b--#');

expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should allow unsubscribing explicitly and early', () => {
const e1 = hot('---^--a--b-----c--d--e--|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '---a--b--- ';

expectObservable(e1.take(42), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const e1 = hot(' ---^--a--b-----c--d--e--|');
const unsub = ' ---------! ';
const e1subs = s(' ^--------! ');
const expected = e(' ---a--b--- ');

const value = getMessages(e1.pipe(take(42)), unsub);
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should work with throw', () => {
const e1 = cold('#');
const e1subs = '(^!)';
const expected = '#';
const e1 = cold(' #');
const e1subs = s(' (^!)');
const expected = e('#');

expectObservable(e1.take(42)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
const value = getMessages(e1.pipe(take(42)));
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should throw if total is less than zero', () => {
expect(() => { Observable.range(0, 10).take(-1); })
.to.throw(Rx.ArgumentOutOfRangeError);
.to.throw(ArgumentOutOfRangeError);
});

it('should not break unsubscription chain when unsubscribed explicitly', () => {
const e1 = hot('---^--a--b-----c--d--e--|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '---a--b--- ';
const unsub = ' ---------! ';
const e1subs = s(' ^--------! ');
const expected = e('---a--b--- ');

const result = e1
.mergeMap((x: string) => Observable.of(x))
.take(42)
.mergeMap((x: string) => Observable.of(x));

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
.pipe(
mergeMap((x) => of(x)),
take(42),
mergeMap((x) => of(x))
);

const value = getMessages(result, unsub);
marbleAssert(value).to.equal(expected);
marbleAssert(e1.subscriptions).to.equal([e1subs]);
});

it('should unsubscribe from the source when it reaches the limit', () => {
const source = Observable.create(observer => {
const source = Observable.create((observer: Observer<number>) => {
expect(observer.closed).to.be.false;
observer.next(42);
expect(observer.closed).to.be.true;
Expand Down