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

refactor(bufferWhen-spec): update bufferWhen operator tests to use pipeable operators #3646

Merged
merged 1 commit into from May 9, 2018
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
84 changes: 43 additions & 41 deletions spec/operators/bufferWhen-spec.ts
@@ -1,13 +1,12 @@
import { expect } from 'chai';
import * as Rx from 'rxjs/Rx';
import { of, EMPTY } from 'rxjs';
import { bufferWhen, mergeMap, takeWhile } from 'rxjs/operators';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare function asDiagram(arg: string): Function;

const Observable = Rx.Observable;

/** @test {bufferWhen} */
describe('Observable.prototype.bufferWhen', () => {
describe('bufferWhen operator', () => {
asDiagram('bufferWhen')('should emit buffers that close and reopen', () => {
const e1 = hot('--a--^---b---c---d---e---f---g---------|');
const e2 = cold( '--------------(s|)');
Expand All @@ -19,7 +18,7 @@ describe('Observable.prototype.bufferWhen', () => {
z: []
};

expectObservable(e1.bufferWhen(() => e2)).toBe(expected, values);
expectObservable(e1.pipe(bufferWhen(() => e2))).toBe(expected, values);
});

it('should emit buffers using constying cold closings', () => {
Expand All @@ -37,7 +36,7 @@ describe('Observable.prototype.bufferWhen', () => {
};

let i = 0;
const result = e1.bufferWhen(() => closings[i++]);
const result = e1.pipe(bufferWhen(() => closings[i++]));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -61,7 +60,7 @@ describe('Observable.prototype.bufferWhen', () => {
};

let i = 0;
const result = e1.bufferWhen(() => closings[i++].obs);
const result = e1.pipe(bufferWhen(() => closings[i++].obs));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -88,7 +87,7 @@ describe('Observable.prototype.bufferWhen', () => {
};

let i = 0;
const result = e1.bufferWhen(() => closings[i++]);
const result = e1.pipe(bufferWhen(() => closings[i++]));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -113,7 +112,7 @@ describe('Observable.prototype.bufferWhen', () => {
};

let i = 0;
const result = e1.bufferWhen(() => closings[i++]);
const result = e1.pipe(bufferWhen(() => closings[i++]));

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -138,10 +137,11 @@ describe('Observable.prototype.bufferWhen', () => {
};

let i = 0;
const result = e1
.mergeMap((x: any) => Observable.of(x))
.bufferWhen(() => closings[i++])
.mergeMap((x: any) => Observable.of(x));
const result = e1.pipe(
mergeMap((x: any) => of(x)),
bufferWhen(() => closings[i++]),
mergeMap((x: any) => of(x))
);

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -162,12 +162,14 @@ describe('Observable.prototype.bufferWhen', () => {
const values = { x: ['b', 'c', 'd'] };

let i = 0;
const result = e1.bufferWhen(() => {
if (i === 1) {
throw 'error';
}
return closings[i++];
});
const result = e1.pipe(
bufferWhen(() => {
if (i === 1) {
throw 'error';
}
return closings[i++];
})
);

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -186,7 +188,7 @@ describe('Observable.prototype.bufferWhen', () => {
const values = { x: ['b', 'c', 'd'] };

let i = 0;
const result = e1.bufferWhen(() => closings[i++]);
const result = e1.pipe(bufferWhen(() => closings[i++]));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -206,7 +208,7 @@ describe('Observable.prototype.bufferWhen', () => {
const values = { x: ['b', 'c', 'd'] };

let i = 0;
const result = e1.bufferWhen(() => closings[i++]);
const result = e1.pipe(bufferWhen(() => closings[i++]));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -225,7 +227,7 @@ describe('Observable.prototype.bufferWhen', () => {
x: ['b', 'c', 'd']
};

const result = e1.bufferWhen(() => e2);
const result = e1.pipe(bufferWhen(() => e2));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
Expand All @@ -240,7 +242,7 @@ describe('Observable.prototype.bufferWhen', () => {
x: []
};

const result = e1.bufferWhen(() => e2);
const result = e1.pipe(bufferWhen(() => e2));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -255,7 +257,7 @@ describe('Observable.prototype.bufferWhen', () => {
x: []
};

const result = e1.bufferWhen(() => e2);
const result = e1.pipe(bufferWhen(() => e2));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -277,7 +279,7 @@ describe('Observable.prototype.bufferWhen', () => {
x: []
};

const source = e1.bufferWhen(() => e2);
const source = e1.pipe(bufferWhen(() => e2));

expectObservable(source, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -292,29 +294,29 @@ describe('Observable.prototype.bufferWhen', () => {
x: ['b', 'c', 'd', 'e', 'f', 'g', 'h']
};

expectObservable(e1.bufferWhen(() => e2)).toBe(expected, values);
expectObservable(e1.pipe(bufferWhen(() => e2))).toBe(expected, values);
});

// bufferWhen is not supposed to handle a factory that returns always empty
// closing Observables, because doing such would constantly recreate a new
// buffer in a synchronous infinite loop until the stack overflows. This also
// happens with buffer in RxJS 4.
it('should NOT handle hot inner empty', (done: MochaDone) => {
const source = Observable.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
const closing = Observable.empty();
const source = of(1, 2, 3, 4, 5, 6, 7, 8, 9);
const closing = EMPTY;
const TOO_MANY_INVOCATIONS = 30;

source
.bufferWhen(() => closing)
.takeWhile((val: any, index: number) => index < TOO_MANY_INVOCATIONS)
.subscribe((val: any) => {
expect(Array.isArray(val)).to.be.true;
expect(val.length).to.equal(0);
}, (err: any) => {
done(new Error('should not be called'));
}, () => {
done();
});
source.pipe(
bufferWhen(() => closing),
takeWhile((val: any, index: number) => index < TOO_MANY_INVOCATIONS)
).subscribe((val: any) => {
expect(Array.isArray(val)).to.be.true;
expect(val.length).to.equal(0);
}, (err: any) => {
done(new Error('should not be called'));
}, () => {
done();
});
});

it('should handle inner throw', () => {
Expand All @@ -327,7 +329,7 @@ describe('Observable.prototype.bufferWhen', () => {
x: ['b', 'c', 'd', 'e', 'f', 'g', 'h']
};

const result = e1.bufferWhen(() => e2);
const result = e1.pipe(bufferWhen(() => e2));

expectObservable(result).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -347,7 +349,7 @@ describe('Observable.prototype.bufferWhen', () => {
z: []
};

const source = e1.bufferWhen(() => e2);
const source = e1.pipe(bufferWhen(() => e2));

expectObservable(source, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand Down