rxjs-consecutive-operator
Version:
RxJS operator for mapping multiple consecutively emitted values at once
66 lines (55 loc) • 2.72 kB
text/typescript
import { TestScheduler } from 'rxjs';
import { expect } from 'chai';
import { consecutive } from '../src/consecutive';
describe('consecutive', () => {
let scheduler: TestScheduler;
beforeEach(() => {
scheduler = new TestScheduler((actual, expected) =>
void expect(actual).to.deep.equal(expected));
});
it('takes two consecutive values from stream and uses operator on them', () => {
const operator = (a: string, b: string) => a + b;
const source = scheduler.createColdObservable('--a--b---c---d---|');
const expected = '-----p---q---r---|';
const values = {
p: operator('a', 'b'),
q: operator('b', 'c'),
r: operator('c', 'd')
};
scheduler.expectObservable(source.let(s => consecutive(s, operator))).toBe(expected, values);
scheduler.flush();
});
it('uses operator length to decide how many values to use', () => {
const operator = (a: string, b: string, c: string) => a + b + c;
const source = scheduler.createColdObservable('--a--b---c---d---|');
const expected = '---------p---q---|';
const values = {
p: operator('a', 'b', 'c'),
q: operator('b', 'c', 'd')
};
scheduler.expectObservable(source.let(s => consecutive(s, operator))).toBe(expected, values);
scheduler.flush();
});
it('uses explicitly given number to decide how many values to use', () => {
const operator = (a: string, b: string, c: string) => a + b + c;
const source = scheduler.createColdObservable('--a--b---c---d---|');
const expected = '-----p---q---r---|';
const values = {
p: operator('a', 'b', undefined as any),
q: operator('b', 'c', undefined as any),
r: operator('c', 'd', undefined as any)
};
scheduler.expectObservable(source.let(s => consecutive(s, operator, 2))).toBe(expected, values);
scheduler.flush();
});
it('throws if operator length could not be determined', () => {
const operator = (...args: string[]) => '';
const source = scheduler.createColdObservable('--a--b---c---d---|');
expect(() => source.let(s => consecutive(s, operator))).to.throw();
});
it('does not throw if operator length could not be determined but explicit number was provided', () => {
const operator = (...args: string[]) => '';
const source = scheduler.createColdObservable('--a--b---c---d---|');
expect(() => source.let(s => consecutive(s, operator, 3))).not.to.throw();
});
});