forked from whatsup/whatsup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefer.test.ts
118 lines (88 loc) · 3.52 KB
/
defer.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { cause } from '../src/cause'
import { whatsUp } from '../src/observer'
describe('Defer', () => {
const delay = <T>(t: number) => new Promise<T>((r) => setTimeout(r, t))
it(`should trigger context.update`, async () => {
const mock = jest.fn()
const cau = cause(function* (ctx) {
ctx.defer(() => delay(300))
yield 'Hello'
yield 'World'
})
whatsUp(cau, mock)
expect(mock).lastCalledWith('Hello')
await delay(400)
expect(mock).lastCalledWith('World')
})
it(`should return {done, value}`, async () => {
const mock = jest.fn()
const cau = cause(function* (ctx) {
const result = ctx.defer(() => new Promise((r) => setTimeout(() => r('World'), 300)))
yield { ...result }
yield { ...result }
})
whatsUp(cau, mock)
expect(mock.mock.calls[0][0]).toEqual({ done: false })
await new Promise((r) => setTimeout(r, 400))
expect(mock.mock.calls[1][0]).toEqual({ done: true, value: 'World' })
})
it(`should serve scopes`, async () => {
const mock = jest.fn()
const cau = cause(function* (ctx) {
// this
// and this
// is
// is
// sync
// async
// flow
// flow
//
let str = ''
str += 'this '
ctx.defer(async () => delay(0).then(() => (str += 'and this ')))
str += 'is '
ctx.defer(async () => delay(0).then(() => (str += 'is ')))
str += 'sync '
ctx.defer(async () => delay(0).then(() => (str += 'async ')))
str += 'flow '
ctx.defer(async () => delay(0).then(() => (str += 'flow ')))
const result = ctx.defer(async () => str)
yield str
yield result.value
})
whatsUp(cau, mock)
expect(mock).lastCalledWith('this is sync flow ')
await new Promise((r) => setTimeout(r, 400))
expect(mock).lastCalledWith('this is sync flow and this is async flow ')
})
it(`should preserves the order of scopes`, async () => {
const mock = jest.fn()
let a0: string, a1: string, a2: string, a3: string
let b0: string, b1: string, b2: string, b3: string
const cau = cause(function* (ctx) {
let str = ''
a0 = str += 'this '
ctx.defer(async () => delay(0).then(() => (b0 = str += 'and this ')))
a1 = str += 'is '
ctx.defer(async () => delay(0).then(() => (b1 = str += 'is ')))
a2 = str += 'sync '
ctx.defer(async () => delay(0).then(() => (b2 = str += 'async ')))
a3 = str += 'flow '
ctx.defer(async () => delay(0).then(() => (b3 = str += 'flow ')))
yield str
yield ''
})
whatsUp(cau, mock)
expect(mock).lastCalledWith('this is sync flow ')
await new Promise((r) => setTimeout(r, 100))
expect(a0!).toBe('this ')
expect(a1!).toBe('this is ')
expect(a2!).toBe('this is sync ')
expect(a3!).toBe('this is sync flow ')
expect(b0!).toBe('this is sync flow and this ')
expect(b1!).toBe('this is sync flow and this is ')
expect(b2!).toBe('this is sync flow and this is async ')
expect(b3!).toBe('this is sync flow and this is async flow ')
})
})