-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathfinalize-spec.ts
More file actions
109 lines (82 loc) · 2.38 KB
/
finalize-spec.ts
File metadata and controls
109 lines (82 loc) · 2.38 KB
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
import { hasNext, hasErr, noNext } from '../asynciterablehelpers.js';
import { range, throwError } from 'ix/asynciterable/index.js';
import { finalize, tap, flatMap } from 'ix/asynciterable/operators/index.js';
test('AsyncIterable#finalize defers behavior', async () => {
let done = false;
const xs = range(0, 2).pipe(
finalize(async () => {
done = true;
})
);
expect(done).toBeFalsy();
const it = xs[Symbol.asyncIterator]();
expect(done).toBeFalsy();
await hasNext(it, 0);
expect(done).toBeFalsy();
await hasNext(it, 1);
expect(done).toBeFalsy();
await noNext(it);
expect(done).toBeTruthy();
});
test('AsyncIterable#finalize calls even with error', async () => {
let done = false;
const err = new Error();
const xs = throwError(err).pipe(
finalize(async () => {
done = true;
})
);
expect(done).toBeFalsy();
const it = xs[Symbol.asyncIterator]();
expect(done).toBeFalsy();
await hasErr(it, err);
expect(done).toBeTruthy();
});
test('AsyncIterable#finalize calls with downstream error', async () => {
let done = false;
const err = new Error();
const xs = range(0, 2).pipe(
finalize(async () => {
done = true;
}),
tap(async () => {
throw err;
})
);
expect(done).toBeFalsy();
const it = xs[Symbol.asyncIterator]();
expect(done).toBeFalsy();
await hasErr(it, err);
expect(done).toBeTruthy();
});
test('AsyncIterable#finalize calls with downstream error from flattening', async () => {
let done = false;
// let srcValues = [] as number[];
const err = new Error();
const xs = range(0, 4).pipe(
finalize(async () => {
done = true;
}),
flatMap(async (x) => {
// srcValues.push(x);
if (x === 1) {
return throwError(err);
}
return [x];
})
);
expect(done).toBeFalsy();
const it = xs[Symbol.asyncIterator]();
expect(done).toBeFalsy();
await hasNext(it, 0);
await hasErr(it, err);
await noNext(it);
expect(done).toBeTruthy();
// The source will yield one more value after the throwError(err),
// because the internal Promise.race([outer, inner]) call resolves
// to the last outer value before the inner error. This is an artifact
// of the JS Promise scheduler's breadth-first scheduling behavior, not
// a bug in IxJS.
// TODO: This is broken in google-closure-compiler?
// expect(srcValues).toEqual([0, 1, 2]);
});