forked from ReactiveX/rxjs
/
sample-spec.ts
77 lines (69 loc) · 2.04 KB
/
sample-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
import { of } from 'rxjs';
import { sample } from 'rxjs/operators';
import { asInteropObservable } from '../../spec/helpers/interop-helper';
it('should enforce parameter', () => {
of(1, 2, 3).pipe(sample()); // $ExpectError
});
it('should accept observable as notifier parameter', () => {
of(1, 2, 3).pipe(sample(of(4))); // $ExpectType Observable<number>
of(1, 2, 3).pipe(sample(of('a'))); // $ExpectType Observable<number>
});
it('should accept interop observable notifier', () => {
of(1, 2, 3).pipe(sample(asInteropObservable(of(true)))); // $ExpectType Observable<number>
});
it('should accept promise notifier', () => {
of(1, 2, 3).pipe(sample(Promise.resolve(true))); // $ExpectType Observable<number>
});
it('should async iterable notifier', () => {
const asyncRange = {
from: 1,
to: 2,
[Symbol.asyncIterator]() {
return {
current: this.from,
last: this.to,
async next() {
await Promise.resolve();
const done = (this.current > this.last);
return {
done,
value: done ? this.current++ : undefined
};
}
};
}
};
of(1, 2, 3).pipe(sample(asyncRange)); // $ExpectType Observable<number>
});
it('should accept iterable notifier', () => {
const syncRange = {
from: 1,
to: 2,
[Symbol.iterator]() {
return {
current: this.from,
last: this.to,
next() {
const done = (this.current > this.last);
return {
done,
value: done ? this.current++ : undefined
};
}
};
}
};
of(1, 2, 3).pipe(sample(syncRange)); // $ExpectType Observable<number>
});
it('should accept readable stream notifier', () => {
const readableStream = new ReadableStream<string>({
pull(controller) {
controller.enqueue('x');
controller.close();
},
});
of(1, 2, 3).pipe(sample(readableStream)); // $ExpectType Observable<number>
});
it('should enforce types of the notifier', () => {
of(1, 2, 3).pipe(sample(8)); // $ExpectError
});