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: update pairwise tests to run mode #6511

Merged
merged 1 commit into from
Jul 8, 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
183 changes: 94 additions & 89 deletions spec/operators/pairwise-spec.ts
Original file line number Diff line number Diff line change
@@ -1,133 +1,139 @@
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
/** @prettier */
import { TestScheduler } from 'rxjs/testing';
import { pairwise, take } from 'rxjs/operators';
import { Subject, Observable } from 'rxjs';
import { expect } from 'chai';
import { observableMatcher } from '../helpers/observableMatcher';

/** @test {pairwise} */
describe('pairwise operator', () => {
it('should group consecutive emissions as arrays of two', () => {
const e1 = hot('--a--b-c----d--e---|');
const expected = '-----u-v----w--x---|';

const values = {
u: ['a', 'b'],
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e']
};
let testScheduler: TestScheduler;

const source = (<any>e1).pipe(pairwise());
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});

expectObservable(source).toBe(expected, values);
it('should group consecutive emissions as arrays of two', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b-c----d--e---|');
const e1subs = ' ^------------------!';
const expected = '-----u-v----w--x---|';

const values = {
u: ['a', 'b'],
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e'],
};

expectObservable(e1.pipe(pairwise())).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should pairwise things', () => {
const e1 = hot('--a--^--b--c--d--e--f--g--|');
const e1subs = '^ !';
const expected = '------v--w--x--y--z--|';

const values = {
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e'],
y: ['e', 'f'],
z: ['f', 'g']
};

const source = (<any>e1).pipe(pairwise());

expectObservable(source).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('--a--^--b--c--d--e--f--g--|');
const e1subs = ' ^--------------------!';
const expected = ' ------v--w--x--y--z--|';

const values = {
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e'],
y: ['e', 'f'],
z: ['f', 'g'],
};

expectObservable(e1.pipe(pairwise())).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should not emit on single-element streams', () => {
const e1 = hot('-----^--b----|');
const e1subs = '^ !';
const expected = '--------|';
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('--a--^--b----|');
const e1subs = ' ^-------!';
const expected = ' --------|';

const values = {
};

const source = (<any>e1).pipe(pairwise());

expectObservable(source).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectObservable(e1.pipe(pairwise())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should handle mid-stream throw', () => {
const e1 = hot('--a--^--b--c--d--e--#');
const e1subs = '^ !';
const expected = '------v--w--x--#';

const values = {
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e']
};

const source = (<any>e1).pipe(pairwise());

expectObservable(source).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('--a--^--b--c--d--e--#');
const e1subs = ' ^--------------!';
const expected = ' ------v--w--x--#';

const values = {
v: ['b', 'c'],
w: ['c', 'd'],
x: ['d', 'e'],
};

expectObservable(e1.pipe(pairwise())).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should handle empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '|';

const source = (<any>e1).pipe(pairwise());
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' | ');
const e1subs = ' (^!)';
const expected = '| ';

expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectObservable(e1.pipe(pairwise())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should handle never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';

const source = (<any>e1).pipe(pairwise());
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' -');
const e1subs = ' ^';
const expected = '-';

expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectObservable(e1.pipe(pairwise())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should handle throw', () => {
const e1 = cold('#');
const e1subs = '(^!)';
const expected = '#';

const source = (<any>e1).pipe(pairwise());
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const e1 = cold(' # ');
const e1subs = ' (^!)';
const expected = '# ';

expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectObservable(e1.pipe(pairwise())).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should be recursively re-enterable', () => {
const results = new Array<[string, string]>();

const subject = new Subject<string>();

subject
.pipe(
pairwise(),
take(3)
)
.subscribe(pair => {
results.push(pair);
subject.next('c');
});
subject.pipe(pairwise(), take(3)).subscribe((pair) => {
results.push(pair);
subject.next('c');
});

subject.next('a');
subject.next('b');

expect(results).to.deep.equal([['a', 'b'], ['b', 'c'], ['c', 'c']]);
expect(results).to.deep.equal([
['a', 'b'],
['b', 'c'],
['c', 'c'],
]);
});

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 @@ -136,10 +142,9 @@ describe('pairwise operator', () => {
}
});

synchronousObservable.pipe(
pairwise(),
take(2),
).subscribe(() => { /* noop */ });
synchronousObservable.pipe(pairwise(), take(2)).subscribe(() => {
/* noop */
});

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