/
pairwise-spec.ts
129 lines (98 loc) · 3.19 KB
/
pairwise-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { pairwise, take } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { expect } from 'chai';
declare function asDiagram(arg: string): Function;
/** @test {pairwise} */
describe('pairwise operator', () => {
asDiagram('pairwise')('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']
};
const source = (<any>e1).pipe(pairwise());
expectObservable(source).toBe(expected, values);
});
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);
});
it('should not emit on single-element streams', () => {
const e1 = hot('-----^--b----|');
const e1subs = '^ !';
const expected = '--------|';
const values = {
};
const source = (<any>e1).pipe(pairwise());
expectObservable(source).toBe(expected, values);
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);
});
it('should handle empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '|';
const source = (<any>e1).pipe(pairwise());
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';
const source = (<any>e1).pipe(pairwise());
expectObservable(source).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should handle throw', () => {
const e1 = cold('#');
const e1subs = '(^!)';
const expected = '#';
const source = (<any>e1).pipe(pairwise());
expectObservable(source).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.next('a');
subject.next('b');
expect(results).to.deep.equal([['a', 'b'], ['b', 'c'], ['c', 'c']]);
});
});