-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolate.test.ts
57 lines (49 loc) · 1.32 KB
/
isolate.test.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
import { isolate, shared, free, un } from '../src';
test('should work basic operations with isolate', async () => {
const destr_1 = jest.fn();
const destr_2 = jest.fn();
const destr_3 = jest.fn();
const destr_4 = jest.fn();
let unsub: any;
let unsubs: any;
const A = () => {
un(() => destr_1());
const finish = isolate.unsafe();
unsub = isolate(() => un(() => destr_2()));
un(() => destr_3());
unsubs = finish();
un(() => destr_4());
};
shared(A);
free();
expect(destr_1).toBeCalled();
expect(destr_4).toBeCalled();
expect(destr_2).not.toBeCalled();
unsub();
expect(destr_2).toBeCalled();
unsubs();
expect(destr_3).toBeCalled();
});
test('should work isolate with argument', async () => {
const destr_1 = jest.fn();
let unsub_1: any, unsub_2: any;
const A = () => {
un(() => destr_1());
unsub_1 = isolate(() => un(() => destr_1()));
un(() => destr_1());
unsub_2 = isolate(() => un(() => destr_1()));
};
shared(A);
free();
expect(destr_1).toBeCalledTimes(2);
unsub_1();
expect(destr_1).toBeCalledTimes(3);
unsub_2();
expect(destr_1).toBeCalledTimes(4);
});
test('should work isolate with no context', async () => {
const destr_1 = jest.fn();
const unsub_1 = isolate(() => un(() => destr_1()));
unsub_1();
expect(destr_1).toBeCalledTimes(1);
});