-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
103 lines (83 loc) · 3.14 KB
/
index.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
import { ColdObservable } from './src/rxjs/cold-observable';
import { HotObservable } from './src/rxjs/hot-observable';
import { Scheduler } from './src/rxjs/scheduler';
import { stripAlignmentChars } from './src/rxjs/strip-alignment-chars';
import { Subscription } from 'rxjs';
export type ObservableWithSubscriptions = ColdObservable | HotObservable;
export { Scheduler } from './src/rxjs/scheduler';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R extends void | Promise<void>> {
toBeObservable(observable: ObservableWithSubscriptions): R;
toHaveSubscriptions(marbles: string | string[]): R;
toHaveNoSubscriptions(): R;
toBeMarble(marble: string): R;
toSatisfyOnFlush(func: () => void): R;
}
}
}
declare module 'expect' {
interface Matchers<R extends void | Promise<void>> {
toBeObservable(observable: ObservableWithSubscriptions): R;
toHaveSubscriptions(marbles: string | string[]): R;
toHaveNoSubscriptions(): R;
toBeMarble(marble: string): R;
toSatisfyOnFlush(func: () => void): R;
}
}
export function hot(marbles: string, values?: object, error?: object): HotObservable {
return new HotObservable(stripAlignmentChars(marbles), values, error);
}
export function cold(marbles: string, values?: object, error?: object): ColdObservable {
return new ColdObservable(stripAlignmentChars(marbles), values, error);
}
export function time(marbles: string): number {
return Scheduler.get().createTime(stripAlignmentChars(marbles));
}
export function schedule(work: () => void, delay: number): Subscription {
return Scheduler.get().schedule(work, delay);
}
const dummyResult = {
message: () => '',
pass: true,
};
expect.extend({
toHaveSubscriptions(actual: ObservableWithSubscriptions, marbles: string | string[]) {
const sanitizedMarbles = Array.isArray(marbles) ? marbles.map(stripAlignmentChars) : stripAlignmentChars(marbles);
Scheduler.get().expectSubscriptions(actual.getSubscriptions()).toBe(sanitizedMarbles);
return dummyResult;
},
toHaveNoSubscriptions(actual: ObservableWithSubscriptions) {
Scheduler.get().expectSubscriptions(actual.getSubscriptions()).toBe([]);
return dummyResult;
},
toBeObservable(actual, expected: ObservableWithSubscriptions) {
Scheduler.get().expectObservable(actual).toBe(expected.marbles, expected.values, expected.error);
return dummyResult;
},
toBeMarble(actual: ObservableWithSubscriptions, marbles: string) {
Scheduler.get().expectObservable(actual).toBe(stripAlignmentChars(marbles));
return dummyResult;
},
toSatisfyOnFlush(actual: ObservableWithSubscriptions, func: () => void) {
Scheduler.get().expectObservable(actual);
// tslint:disable:no-string-literal
const flushTests = Scheduler.get()['flushTests'];
flushTests[flushTests.length - 1].ready = true;
onFlush.push(func);
return dummyResult;
},
});
let onFlush: (() => void)[] = [];
beforeEach(() => {
Scheduler.init();
onFlush = [];
});
afterEach(() => {
Scheduler.get().run(() => {});
while (onFlush.length > 0) {
onFlush.shift()?.();
}
Scheduler.reset();
});