-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
apiSetupContext.spec.ts
209 lines (180 loc) 路 4.8 KB
/
apiSetupContext.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
import { ref, reactive } from '@vue/reactivity'
import {
renderToString,
h,
nodeOps,
render,
serializeInner,
nextTick,
watch,
defineComponent,
triggerEvent,
TestElement
} from '@vue/runtime-test'
// reference: https://vue-composition-api-rfc.netlify.com/api.html#setup
describe('api: setup context', () => {
it('should expose return values to template render context', () => {
const Comp = defineComponent({
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 `${this.ref} ${this.object.msg} ${this.value}`
}
})
expect(renderToString(h(Comp))).toMatch(`foo bar baz`)
})
it('should support returning render function', () => {
const Comp = {
setup() {
return () => {
return h('div', 'hello')
}
}
}
expect(renderToString(h(Comp))).toMatch(`hello`)
})
it('props', async () => {
const count = ref(0)
let dummy
const Parent = {
render: () => h(Child, { count: count.value })
}
const Child = defineComponent({
setup(props: { count: number }) {
watch(() => {
dummy = props.count
})
return () => h('div', props.count)
}
})
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div>0</div>`)
expect(dummy).toBe(0)
// props should be reactive
count.value++
await nextTick()
expect(serializeInner(root)).toMatch(`<div>1</div>`)
expect(dummy).toBe(1)
})
it('setup props should resolve the correct types from props object', async () => {
const count = ref(0)
let dummy
const Parent = {
render: () => h(Child, { count: count.value })
}
const Child = defineComponent({
props: {
count: Number
},
setup(props) {
watch(() => {
dummy = props.count
})
return () => h('div', props.count)
}
})
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div>0</div>`)
expect(dummy).toBe(0)
// props should be reactive
count.value++
await nextTick()
expect(serializeInner(root)).toMatch(`<div>1</div>`)
expect(dummy).toBe(1)
})
it('context.attrs', async () => {
const toggle = ref(true)
const Parent = {
render: () => h(Child, toggle.value ? { id: 'foo' } : { class: 'baz' })
}
const Child = {
// explicit empty props declaration
// puts everything received in attrs
// disable implicit fallthrough
inheritAttrs: false,
props: {},
setup(props: any, { attrs }: any) {
return () => h('div', attrs)
}
}
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div id="foo"></div>`)
// should update even though it's not reactive
toggle.value = false
await nextTick()
expect(serializeInner(root)).toMatch(`<div class="baz"></div>`)
})
it('context.slots', async () => {
const id = ref('foo')
const Parent = {
render: () =>
h(Child, null, {
foo: () => id.value,
bar: () => 'bar'
})
}
const Child = {
setup(props: any, { slots }: any) {
return () => h('div', [...slots.foo(), ...slots.bar()])
}
}
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div>foobar</div>`)
// should update even though it's not reactive
id.value = 'baz'
await nextTick()
expect(serializeInner(root)).toMatch(`<div>bazbar</div>`)
})
it('context.emit', async () => {
const count = ref(0)
const spy = jest.fn()
const Parent = {
render: () =>
h(Child, {
count: count.value,
onInc: (newVal: number) => {
spy()
count.value = newVal
}
})
}
const Child = defineComponent({
props: {
count: {
type: Number,
default: 1
}
},
setup(props, { emit }) {
return () =>
h(
'div',
{
onClick: () => emit('inc', props.count + 1)
},
props.count
)
}
})
const root = nodeOps.createElement('div')
render(h(Parent), root)
expect(serializeInner(root)).toMatch(`<div>0</div>`)
// emit should trigger parent handler
triggerEvent(root.children[0] as TestElement, 'click')
expect(spy).toHaveBeenCalled()
await nextTick()
expect(serializeInner(root)).toMatch(`<div>1</div>`)
})
})