/
apiSetup.spec.ts
297 lines (253 loc) · 6.66 KB
/
apiSetup.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { h, ref, reactive, isReactive, toRef, isRef } from 'v3'
import { nextTick } from 'core/util'
import { effect } from 'v3/reactivity/effect'
import Vue from 'vue'
function renderToString(comp: any) {
const vm = new Vue(comp).$mount()
return vm.$el.outerHTML
}
describe('api: setup context', () => {
it('should expose return values to template render context', () => {
const Comp = {
setup() {
return {
// ref should auto-unwrap
ref: ref('foo'),
// object exposed as-is
object: reactive({ msg: 'bar' }),
// primitive value exposed as-is
value: 'baz'
}
},
render() {
return h('div', `${this.ref} ${this.object.msg} ${this.value}`)
}
}
expect(renderToString(Comp)).toMatch(`<div>foo bar baz</div>`)
})
it('should support returning render function', () => {
const Comp = {
setup() {
return () => {
return h('div', 'hello')
}
}
}
expect(renderToString(Comp)).toMatch(`<div>hello</div>`)
})
it('props', async () => {
const count = ref(0)
let dummy
const Parent = {
render: () => h(Child, { props: { count: count.value } })
}
const Child = {
props: { count: Number },
setup(props) {
effect(() => {
dummy = props.count
})
return () => h('div', props.count)
}
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div>0</div>`)
expect(dummy).toBe(0)
// props should be reactive
count.value++
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div>1</div>`)
expect(dummy).toBe(1)
})
it('context.attrs', async () => {
const toggle = ref(true)
const Parent = {
render: () =>
h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
}
const Child = {
// explicit empty props declaration
// puts everything received in attrs
// disable implicit fallthrough
inheritAttrs: false,
setup(_props: any, { attrs }: any) {
return () => h('div', { attrs })
}
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
// should update even though it's not reactive
toggle.value = false
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
})
// vuejs/core #4161
it('context.attrs in child component slots', async () => {
const toggle = ref(true)
const Wrapper = {
template: `<div><slot/></div>`
}
const Child = {
inheritAttrs: false,
setup(_: any, { attrs }: any) {
return () => {
return h(Wrapper, [h('div', { attrs })])
}
}
}
const Parent = {
render: () =>
h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
// should update even though it's not reactive
toggle.value = false
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
})
it('context.attrs in child component scoped slots', async () => {
const toggle = ref(true)
const Wrapper = {
template: `<div><slot/></div>`
}
const Child = {
inheritAttrs: false,
setup(_: any, { attrs }: any) {
return () => {
return h(Wrapper, {
scopedSlots: {
default: () => h('div', { attrs })
}
})
}
}
}
const Parent = {
render: () =>
h(Child, { attrs: toggle.value ? { id: 'foo' } : { class: 'baz' } })
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div id="foo"></div>`)
// should update even though it's not reactive
toggle.value = false
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div class="baz"></div>`)
})
it('context.slots', async () => {
const id = ref('foo')
const Child = {
setup(_props: any, { slots }: any) {
return () => h('div', [...slots.foo(), ...slots.bar()])
}
}
const Parent = {
components: { Child },
setup() {
return { id }
},
template: `<Child>
<template #foo>{{ id }}</template>
<template #bar>bar</template>
</Child>`
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div>foobar</div>`)
// should update even though it's not reactive
id.value = 'baz'
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div>bazbar</div>`)
})
it('context.emit', async () => {
const count = ref(0)
const spy = vi.fn()
const Child = {
props: {
count: {
type: Number,
default: 1
}
},
setup(props, { emit }) {
return () =>
h(
'div',
{
on: { click: () => emit('inc', props.count + 1) }
},
props.count
)
}
}
const Parent = {
components: { Child },
setup: () => ({
count,
onInc(newVal: number) {
spy()
count.value = newVal
}
}),
template: `<Child :count="count" @inc="onInc" />`
}
const vm = new Vue(Parent).$mount()
expect(vm.$el.outerHTML).toMatch(`<div>0</div>`)
// emit should trigger parent handler
triggerEvent(vm.$el as HTMLElement, 'click')
expect(spy).toHaveBeenCalled()
await nextTick()
expect(vm.$el.outerHTML).toMatch(`<div>1</div>`)
})
it('directive resolution', () => {
const spy = vi.fn()
new Vue({
setup: () => ({
__sfc: true,
vDir: {
inserted: spy
}
}),
template: `<div v-dir />`
}).$mount()
expect(spy).toHaveBeenCalled()
})
// #12561
it('setup props should be reactive', () => {
const msg = ref('hi')
const Child = {
props: ['msg'],
setup: props => {
expect(isReactive(props)).toBe(true)
expect(isRef(toRef(props, 'foo'))).toBe(true)
return () => {}
}
}
new Vue({
setup() {
return h => h(Child, { props: { msg } })
}
}).$mount()
})
it('should not track dep accessed in setup', async () => {
const spy = vi.fn()
const msg = ref('hi')
const Child = {
setup: () => {
msg.value
return () => {}
}
}
new Vue({
setup() {
return h => {
spy()
return h(Child)
}
}
}).$mount()
expect(spy).toHaveBeenCalledTimes(1)
msg.value = 'bye'
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
})