/
IteratorObservable-spec.ts
184 lines (156 loc) · 4.88 KB
/
IteratorObservable-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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { expect } from 'chai';
import { fromIterable } from 'rxjs/internal/observable/fromIterable';
import { iterator as symbolIterator } from 'rxjs/internal/symbol/iterator';
import { TestScheduler } from 'rxjs/testing';
import { Notification, queueScheduler, Subscriber } from 'rxjs';
import { observeOn, materialize, take, toArray } from 'rxjs/operators';
declare const expectObservable: any;
declare const rxTestScheduler: TestScheduler;
describe('fromIterable', () => {
it('should not accept null (or truthy-equivalent to null) iterator', () => {
expect(() => {
fromIterable(null as any, undefined);
}).to.throw(Error, 'Iterable cannot be null');
expect(() => {
fromIterable(void 0 as any, undefined);
}).to.throw(Error, 'Iterable cannot be null');
});
it('should emit members of an array iterator', (done) => {
const expected = [10, 20, 30, 40];
fromIterable([10, 20, 30, 40], undefined)
.subscribe(
(x) => { expect(x).to.equal(expected.shift()); },
(x) => {
done(new Error('should not be called'));
}, () => {
expect(expected.length).to.equal(0);
done();
}
);
});
it('should get new iterator for each subscription', () => {
const expected = [
Notification.createNext(10),
Notification.createNext(20),
Notification.createComplete()
];
const e1 = fromIterable<number>(new Int32Array([10, 20]), undefined).pipe(observeOn(rxTestScheduler));
let v1, v2: Array<Notification<any>>;
e1.pipe(materialize(), toArray()).subscribe((x) => v1 = x);
e1.pipe(materialize(), toArray()).subscribe((x) => v2 = x);
rxTestScheduler.flush();
expect(v1).to.deep.equal(expected);
expect(v2!).to.deep.equal(expected);
});
it('should finalize generators if the subscription ends', () => {
const iterator = {
finalized: false,
next() {
return { value: 'duck', done: false };
},
return() {
this.finalized = true;
}
};
const iterable = {
[symbolIterator]() {
return iterator;
}
};
const results: any[] = [];
fromIterable(iterable as any, undefined)
.pipe(take(3))
.subscribe(
x => results.push(x),
null,
() => results.push('GOOSE!')
);
expect(results).to.deep.equal(['duck', 'duck', 'duck', 'GOOSE!']);
expect(iterator.finalized).to.be.true;
});
it('should finalize generators if the subscription and it is scheduled', () => {
const iterator = {
finalized: false,
next() {
return { value: 'duck', done: false };
},
return() {
this.finalized = true;
}
};
const iterable = {
[symbolIterator]() {
return iterator;
}
};
const results: any[] = [];
fromIterable(iterable as any, queueScheduler)
.pipe(take(3))
.subscribe(
x => results.push(x),
null,
() => results.push('GOOSE!')
);
expect(results).to.deep.equal(['duck', 'duck', 'duck', 'GOOSE!']);
expect(iterator.finalized).to.be.true;
});
it('should emit members of an array iterator on a particular scheduler', () => {
const source = fromIterable(
[10, 20, 30, 40],
rxTestScheduler
);
const values = { a: 10, b: 20, c: 30, d: 40 };
expectObservable(source).toBe('(abcd|)', values);
});
it('should emit members of an array iterator on a particular scheduler, ' +
'but is unsubscribed early', (done) => {
const expected = [10, 20, 30, 40];
const source = fromIterable(
[10, 20, 30, 40],
queueScheduler
);
const subscriber = Subscriber.create(
(x) => {
expect(x).to.equal(expected.shift());
if (x === 30) {
subscriber.unsubscribe();
done();
}
}, (x) => {
done(new Error('should not be called'));
}, () => {
done(new Error('should not be called'));
});
source.subscribe(subscriber);
});
it('should emit characters of a string iterator', (done) => {
const expected = ['f', 'o', 'o'];
fromIterable('foo', undefined)
.subscribe(
(x) => { expect(x).to.equal(expected.shift()); },
(x) => {
done(new Error('should not be called'));
}, () => {
expect(expected.length).to.equal(0);
done();
}
);
});
it('should be possible to unsubscribe in the middle of the iteration', (done) => {
const expected = [10, 20, 30];
const subscriber = Subscriber.create(
(x) => {
expect(x).to.equal(expected.shift());
if (x === 30) {
subscriber.unsubscribe();
done();
}
}, (x) => {
done(new Error('should not be called'));
}, () => {
done(new Error('should not be called'));
}
);
fromIterable([10, 20, 30, 40, 50, 60], undefined).subscribe(subscriber);
});
});