Skip to content

Commit

Permalink
test: add buffer/window equivalence tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant authored and benlesh committed Feb 24, 2021
1 parent 0b9b5fd commit 2a3b08a
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions spec/operators/buffer-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buffer, mergeMap, take } from 'rxjs/operators';
/** @prettier */
import { buffer, mergeMap, take, window, toArray } from 'rxjs/operators';
import { EMPTY, NEVER, throwError, of, Subject } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand All @@ -20,7 +21,7 @@ describe('Observable.prototype.buffer', () => {
const expectedValues = {
x: ['a', 'b', 'c'],
y: ['d', 'e', 'f'],
z: ['g', 'h', 'i']
z: ['g', 'h', 'i'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
});
Expand Down Expand Up @@ -128,7 +129,7 @@ describe('Observable.prototype.buffer', () => {
c: ['6'],
d: [] as string[],
e: ['7', '8', '9'],
f: ['0']
f: ['0'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
});
Expand All @@ -145,7 +146,7 @@ describe('Observable.prototype.buffer', () => {
a: ['3'],
b: ['4', '5'],
c: ['6'],
d: [] as string[]
d: [] as string[],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
Expand All @@ -161,7 +162,7 @@ describe('Observable.prototype.buffer', () => {
const expected = ' ---a-------b--- ';
const expectedValues = {
a: ['3'],
b: ['4', '5']
b: ['4', '5'],
};
expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
Expand All @@ -177,13 +178,13 @@ describe('Observable.prototype.buffer', () => {
const unsub = ' --------------! ';
const expectedValues = {
a: ['3'],
b: ['4', '5']
b: ['4', '5'],
};

const result = a.pipe(
mergeMap((x: any) => of(x)),
buffer(b),
mergeMap((x: any) => of(x)),
mergeMap((x: any) => of(x))
);

expectObservable(result, unsub).toBe(expected, expectedValues);
Expand All @@ -194,13 +195,13 @@ describe('Observable.prototype.buffer', () => {
it('should work with non-empty and selector error', () => {
// Buffer Boundaries onErrorSource (RxJS 4)
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const a = hot('--1--2--^--3-----#', {'3': 3}, new Error('too bad'));
const a = hot('--1--2--^--3-----#', { '3': 3 }, new Error('too bad'));
const subs = ' ^--------!';
const b = hot('--------^--a--b---');
const expected = ' ---a--b--#';
const expectedValues = {
a: [3],
b: [] as string[]
b: [] as string[],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad'));
expectSubscriptions(a.subscriptions).toBe(subs);
Expand All @@ -227,7 +228,7 @@ describe('Observable.prototype.buffer', () => {
const expectedValues = {
a: ['3'],
b: ['4', '5'],
c: ['6']
c: ['6'],
};
expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad'));
expectSubscriptions(a.subscriptions).toBe(subs);
Expand All @@ -244,7 +245,7 @@ describe('Observable.prototype.buffer', () => {
const expected = ' ---a-------b--- ';
const expectedValues = {
a: ['3'],
b: ['4', '5']
b: ['4', '5'],
};

expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues);
Expand Down Expand Up @@ -272,11 +273,9 @@ describe('Observable.prototype.buffer', () => {
const results: any[] = [];
const subject = new Subject<number>();

const source = subject.pipe(
buffer(subject)
).subscribe({
next: value => results.push(value),
complete: () => results.push('complete')
const source = subject.pipe(buffer(subject)).subscribe({
next: (value) => results.push(value),
complete: () => results.push('complete'),
});

subject.next(1);
Expand All @@ -286,4 +285,39 @@ describe('Observable.prototype.buffer', () => {
subject.complete();
expect(results).to.deep.equal([[1], [2], 'complete']);
});

describe('equivalence with the window operator', () => {
const cases = [
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -----B-----B-----B-|',
},
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -----B-----B--| ',
},
{
source: ' -a-b-c-d-e---------|',
notifier: ' -----B-----B-----B-|',
},
{
source: ' -a-b-c-d-e-f-g-h-i-|',
notifier: ' -------------------|',
},
];
cases.forEach(({ source, notifier }, index) => {
it(`should be equivalent for case ${index}`, () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot(source);
const b = hot(notifier);
expectObservable(a.pipe(buffer(b))).toEqual(
a.pipe(
window(b),
mergeMap((w) => w.pipe(toArray()))
)
);
});
});
});
});
});

0 comments on commit 2a3b08a

Please sign in to comment.