-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathrenderWatch.spec.ts
227 lines (207 loc) · 5.29 KB
/
renderWatch.spec.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { defineComponent } from 'vue'
import {
nextTick,
onBeforeUpdate,
onEffectCleanup,
onUpdated,
ref,
render,
renderEffect,
renderWatch,
template,
watchEffect,
watchPostEffect,
watchSyncEffect,
} from '../src'
let host: HTMLElement
const initHost = () => {
host = document.createElement('div')
host.setAttribute('id', 'host')
document.body.appendChild(host)
}
beforeEach(() => {
initHost()
})
afterEach(() => {
host.remove()
})
const createDemo = (
setupFn: (porps: any, ctx: any) => any,
renderFn: (ctx: any) => any,
) => {
const demo = defineComponent({
setup(...args) {
const returned = setupFn(...args)
Object.defineProperty(returned, '__isScriptSetup', {
enumerable: false,
value: true,
})
return returned
},
})
demo.render = (ctx: any) => {
const t0 = template('<div></div>')
renderFn(ctx)
return t0()
}
return () => render(demo as any, {}, '#host')
}
describe('renderWatch', () => {
test('effect', async () => {
let dummy: any
const source = ref(0)
renderEffect(() => {
dummy = source.value
})
await nextTick()
expect(dummy).toBe(0)
source.value++
await nextTick()
expect(dummy).toBe(1)
})
test('watch', async () => {
let dummy: any
const source = ref(0)
renderWatch(source, () => {
dummy = source.value
})
await nextTick()
expect(dummy).toBe(undefined)
source.value++
await nextTick()
expect(dummy).toBe(1)
})
test('should run with the scheduling order', async () => {
const calls: string[] = []
const mount = createDemo(
() => {
// setup
const source = ref(0)
const renderSource = ref(0)
const change = () => source.value++
const changeRender = () => renderSource.value++
// Life Cycle Hooks
onUpdated(() => {
calls.push(`updated ${source.value}`)
})
onBeforeUpdate(() => {
calls.push(`beforeUpdate ${source.value}`)
})
// Watch API
watchPostEffect(() => {
const current = source.value
calls.push(`post ${current}`)
onEffectCleanup(() => calls.push(`post cleanup ${current}`))
})
watchEffect(() => {
const current = source.value
calls.push(`pre ${current}`)
onEffectCleanup(() => calls.push(`pre cleanup ${current}`))
})
watchSyncEffect(() => {
const current = source.value
calls.push(`sync ${current}`)
onEffectCleanup(() => calls.push(`sync cleanup ${current}`))
})
return { source, change, renderSource, changeRender }
},
// render
(_ctx) => {
// Render Watch API
renderEffect(() => {
const current = _ctx.renderSource
calls.push(`renderEffect ${current}`)
onEffectCleanup(() => calls.push(`renderEffect cleanup ${current}`))
})
renderWatch(
() => _ctx.renderSource,
(value) => {
calls.push(`renderWatch ${value}`)
onEffectCleanup(() => calls.push(`renderWatch cleanup ${value}`))
},
)
},
)
// Mount
const instance = mount()
const { change, changeRender } = instance.setupState as any
expect(calls).toEqual(['pre 0', 'sync 0', 'renderEffect 0'])
calls.length = 0
await nextTick()
expect(calls).toEqual(['post 0'])
calls.length = 0
// Update
changeRender()
change()
expect(calls).toEqual(['sync cleanup 0', 'sync 1'])
calls.length = 0
await nextTick()
expect(calls).toEqual([
'pre cleanup 0',
'pre 1',
'beforeUpdate 1',
'renderEffect cleanup 0',
'renderEffect 1',
'renderWatch 1',
'post cleanup 0',
'post 1',
'updated 1',
])
})
test('errors should include the execution location with beforeUpdate hook', async () => {
const mount = createDemo(
// setup
() => {
const source = ref()
const update = () => source.value++
onBeforeUpdate(() => {
throw 'error in beforeUpdate'
})
return { source, update }
},
// render
(ctx) => {
renderEffect(() => {
ctx.source
})
},
)
const instance = mount()
const { update } = instance.setupState as any
await expect(async () => {
update()
await nextTick()
}).rejects.toThrow('error in beforeUpdate')
expect(
'[Vue warn] Unhandled error during execution of beforeUpdate hook',
).toHaveBeenWarned()
})
test('errors should include the execution location with updated hook', async () => {
const mount = createDemo(
// setup
() => {
const source = ref(0)
const update = () => source.value++
onUpdated(() => {
throw 'error in updated'
})
return { source, update }
},
// render
(ctx) => {
renderEffect(() => {
ctx.source
})
},
)
const instance = mount()
const { update } = instance.setupState as any
await expect(async () => {
update()
await nextTick()
}).rejects.toThrow('error in updated')
expect(
'[Vue warn] Unhandled error during execution of updated',
).toHaveBeenWarned()
})
})