forked from just-jeb/jest-marbles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
86 lines (67 loc) · 2.6 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
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';
export type ObservableWithSubscriptions = ColdObservable | HotObservable;
export { Scheduler } from './src/rxjs/scheduler';
declare global {
namespace jest {
interface Matchers<R, T> {
toBeObservable(observable: ObservableWithSubscriptions): void;
toHaveSubscriptions(marbles: string | string[]): void;
toHaveNoSubscriptions(): void;
toBeMarble(marble: string): void;
toSatisfyOnFlush(func: () => void): void;
}
}
}
export function hot(marbles: string, values?: any, error?: any): HotObservable {
return new HotObservable(stripAlignmentChars(marbles), values, error);
}
export function cold(marbles: string, values?: any, error?: any): ColdObservable {
return new ColdObservable(stripAlignmentChars(marbles), values, error);
}
export function time(marbles: string): number {
return Scheduler.get().createTime(stripAlignmentChars(marbles));
}
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().flush();
while (onFlush.length > 0) {
// @ts-ignore
onFlush.shift()();
}
Scheduler.reset();
});