forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast-spec.ts
145 lines (116 loc) · 4.55 KB
/
last-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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
/** @test {last} */
describe('Observable.prototype.last', () => {
asDiagram('last')('should take the last value of an observable', () => {
const e1 = hot('--a----b--c--|');
const e1subs = '^ !';
const expected = '-------------(c|)';
expectObservable(e1.last()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should error on nothing sent but completed', () => {
const e1 = hot('--a--^----|');
const e1subs = '^ !';
const expected = '-----#';
expectObservable(e1.last()).toBe(expected, null, new Rx.EmptyError());
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should error on empty', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '#';
expectObservable(e1.last()).toBe(expected, null, new Rx.EmptyError());
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should go on forever on never', () => {
const e1 = cold('-');
const e1subs = '^';
const expected = '-';
expectObservable(e1.last()).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should return last element matches with predicate', () => {
const e1 = hot('--a--b--a--b--|');
const e1subs = '^ !';
const expected = '--------------(b|)';
const predicate = function (value) {
return value === 'b';
};
expectObservable(e1.last(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should allow unsubscribing explicitly and early', () => {
const e1 = hot('--a--b--c--d--|');
const unsub = ' ! ';
const e1subs = '^ ! ';
const expected = '-------- ';
expectObservable(e1.last(), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should not break unsubscription chains when result is unsubscribed explicitly', () => {
const e1 = hot('--a--b--c--d--|');
const e1subs = '^ ! ';
const expected = '-------- ';
const unsub = ' ! ';
const result = e1
.mergeMap((x: string) => Rx.Observable.of(x))
.last()
.mergeMap((x: string) => Rx.Observable.of(x));
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should return a default value if no element found', () => {
const e1 = cold('|');
const e1subs = '(^!)';
const expected = '(a|)';
expectObservable(e1.last(null, null, 'a')).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should not return default value if an element is found', () => {
const e1 = hot('--a---^---b---c---d---|');
const e1subs = '^ !';
const expected = '----------------(d|)';
expectObservable(e1.last(null, null, 'x')).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should support a result selector argument', () => {
const e1 = hot('--a--^---b---c---d---e--|');
const e1subs = '^ !';
const expected = '-------------------(x|)';
const predicate = function (x) { return x === 'c'; };
const resultSelector = function (x, i) {
expect(i).to.equal(1);
expect(x).to.equal('c');
return 'x';
};
expectObservable(e1.last(predicate, resultSelector)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should raise error when predicate throws', () => {
const e1 = hot('--a--^---b---c---d---e--|');
const e1subs = '^ ! ';
const expected = '--------# ';
const predicate = function (x) {
if (x === 'c') {
throw 'error';
} else {
return false;
}
};
expectObservable(e1.last(predicate)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
it('should raise error when result selector throws', () => {
const e1 = hot('--a--^---b---c---d---e--|');
const e1subs = '^ ! ';
const expected = '--------# ';
const predicate = function (x) { return x === 'c'; };
const resultSelector = function (x, i) {
throw 'error';
};
expectObservable(e1.last(predicate, resultSelector)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});