forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastValueFrom-spec.ts
47 lines (43 loc) · 1.37 KB
/
lastValueFrom-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
import { interval, lastValueFrom, EMPTY, EmptyError, throwError, of } from 'rxjs';
import { expect } from 'chai';
import { finalize, take } from 'rxjs/operators';
describe('lastValueFrom', () => {
it('should emit the last value as a promise', async () => {
let finalized = false;
const source = interval(2).pipe(
take(10),
finalize(() => (finalized = true))
);
const result = await lastValueFrom(source);
expect(result).to.equal(9);
expect(finalized).to.be.true;
});
it('should error for empty observables', async () => {
const source = EMPTY;
let error: any = null;
try {
await lastValueFrom(source);
} catch (err) {
error = err;
}
expect(error).to.be.an.instanceOf(EmptyError);
});
it('should error for errored observables', async () => {
const source = throwError(new Error('blorp!'));
let error: any = null;
try {
await lastValueFrom(source);
} catch (err) {
error = err;
}
expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.equal('blorp!');
});
it('should work with a synchronous observable', async () => {
let finalized = false;
const source = of('apples', 'bananas').pipe(finalize(() => (finalized = true)));
const result = await lastValueFrom(source);
expect(result).to.equal('bananas');
expect(finalized).to.be.true;
});
});