-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservable.test.js
More file actions
78 lines (66 loc) · 2.24 KB
/
Observable.test.js
File metadata and controls
78 lines (66 loc) · 2.24 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
const Observable = require("./Observable");
jest.useFakeTimers();
describe("Observable", () => {
it("should propagate data", () => {
const observable = new Observable((open, next, fail, done, external) => {
open();
next(1);
next(2);
done(false);
});
const received = [];
const open = jest.fn();
const next = jest.fn(value => received.push(value));
const fail = jest.fn();
const done = jest.fn(cancelled => expect(cancelled).toEqual(false));
observable.listen(open, next, fail, done);
expect(open).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledTimes(2);
expect(fail).toHaveBeenCalledTimes(0);
expect(done).toHaveBeenCalledTimes(1);
expect(received).toEqual([1, 2]);
});
it("should cancel the propagation", () => {
// Observable timer that produces
// data base on specified duration
const timer = duration =>
new Observable((open, next, fail, done, external) => {
open();
const id = setInterval(() => next(1), duration);
const cancel = () => {
clearInterval(id);
done(true);
};
// listens to external
// entity for cancellation token
external
.filter((value) => value === Observable.CANCEL)
.tap(cancel)
.listen();
});
const received = [];
const open = jest.fn();
const next = jest.fn(value => received.push(value));
const fail = jest.fn();
const done = jest.fn(cancelled => expect(cancelled).toEqual(true));
const cancel = jest.fn();
// Observable timer that produces
// cancellation token every 3 second
const cancellation = timer(3000)
.tap(cancel)
.map(() => Observable.CANCEL);
// Observable timer that produces
// data every second
timer(1000).listen(open, next, fail, done, cancellation);
// advance to 9 second
// to check if cancellation observable
// has been cleaned up too
jest.advanceTimersByTime(9000);
expect(open).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledTimes(3);
expect(fail).toHaveBeenCalledTimes(0);
expect(done).toHaveBeenCalledTimes(1);
expect(cancel).toHaveBeenCalledTimes(1);
expect(received).toEqual([1, 1, 1]);
});
});