-
Notifications
You must be signed in to change notification settings - Fork 234
/
props.spec.ts
279 lines (246 loc) · 7.07 KB
/
props.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
import { mount, shallowMount } from '../src'
import WithProps from './components/WithProps.vue'
import PropWithSymbol from './components/PropWithSymbol.vue'
import Hello from './components/Hello.vue'
import { defineComponent, h } from 'vue'
describe('props', () => {
it('returns a single prop applied to a component', () => {
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
expect(wrapper.props('msg')).toEqual('ABC')
})
it('returns all props applied to a component', () => {
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
expect(wrapper.props()).toEqual({ msg: 'ABC' })
})
it('returns undefined if props does not exist', () => {
const wrapper = mount(WithProps, { props: { msg: 'ABC' } })
expect(wrapper.props('foo')).toEqual(undefined)
})
it('returns empty object for components without props', () => {
const wrapper = mount(Hello)
expect(wrapper.props()).toEqual({})
})
it('returns applied props on a stubbed component with boolean', () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: {
foo: String,
bar: Boolean,
object: Object
}
}
const Component = {
data: () => ({ object: {} }),
template: '<div><foo foo="foo" bar :object="object" /></div>',
components: { Foo }
}
const wrapper = mount(Component, {
global: {
stubs: ['Foo']
}
})
expect(wrapper.findComponent({ name: 'Foo' }).props()).toEqual({
bar: true,
foo: 'foo',
object: {}
})
})
it('returns props on a stubbed component with a custom implementation', () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: ['foo', 'bar', 'object']
}
const FooStub = {
name: 'Foo',
template: 'FooStub',
props: {
foo: String,
bar: Boolean,
object: Object
}
}
const Component = {
data: () => ({ object: {} }),
template: '<div><foo foo="foo" bar :object="object" /></div>',
components: { Foo }
}
const wrapper = mount(Component, {
global: {
stubs: { Foo: FooStub }
}
})
expect(wrapper.findComponent({ name: 'Foo' }).props()).toEqual({
bar: true,
foo: 'foo',
object: {}
})
})
it('should return the updated props on 2 way binding', async () => {
const component = defineComponent({
template: '<button @click="increment"></button>',
props: {
modelValue: {
type: Number,
required: true
}
},
emits: ['update:modelValue'],
setup(props, ctx) {
return {
increment: () => ctx.emit('update:modelValue', props.modelValue + 1)
}
}
})
const wrapper = mount(component, {
props: {
modelValue: 1,
'onUpdate:modelValue': async (modelValue: number) =>
wrapper.setProps({ modelValue })
}
})
expect(wrapper.props('modelValue')).toBe(1)
await wrapper.trigger('click')
expect(wrapper.props('modelValue')).toBe(2)
})
it('returns reactive props on a stubbed component', async () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: {
foo: String
}
}
const Component = defineComponent({
data: () => ({ foo1: 'old value', foo2: 'foo2 value' }),
template:
'<div><foo id="foo1" :foo="foo1" /><foo id="foo2" :foo="foo2"/><button @click="buttonClick">Click me</button></div>',
methods: {
buttonClick() {
this.foo1 = 'new value'
}
},
components: { Foo }
})
const wrapper = mount(Component, {
global: {
stubs: ['Foo']
}
})
const [fooCmp1, barCmp1] = wrapper.findAllComponents({ name: 'Foo' })
expect(fooCmp1.props()).toEqual({ foo: 'old value' })
expect(barCmp1.props()).toEqual({ foo: 'foo2 value' })
expect(wrapper.find('#foo1').attributes().foo).toBe('old value')
expect(wrapper.find('#foo2').attributes().foo).toBe('foo2 value')
await wrapper.find('button').trigger('click')
const [fooCmp2, barCmp2] = wrapper.findAllComponents({ name: 'Foo' })
expect(fooCmp2.props()).toEqual({ foo: 'new value' })
expect(barCmp2.props()).toEqual({ foo: 'foo2 value' })
expect(wrapper.find('#foo1').attributes().foo).toBe('new value')
expect(wrapper.find('#foo2').attributes().foo).toBe('foo2 value')
})
it('returns reactive props on a stubbed component shallow case', async () => {
const Foo = {
name: 'Foo',
template: 'Foo',
props: {
foo: String
}
}
const Component = defineComponent({
data: () => ({ foo: 'old value' }),
template:
'<div><foo :foo="foo" /><button @click="buttonClick">Click me</button></div>',
methods: {
buttonClick() {
this.foo = 'new value'
}
},
components: { Foo }
})
const wrapper = mount(Component, {
shallow: true
})
let fooCmp = wrapper.findComponent({ name: 'Foo' })
expect(fooCmp.props()).toEqual({
foo: 'old value'
})
await wrapper.find('button').trigger('click')
expect(fooCmp.props()).toEqual({
foo: 'new value'
})
})
it('https://github.com/vuejs/vue-test-utils-next/issues/440', async () => {
const Foo = defineComponent({
name: 'Foo',
props: {
foo: String
},
emits: ['update:foo'],
setup(props, ctx) {
return () =>
h(
'div',
{
onClick: () => {
ctx.emit('update:foo', 'world')
}
},
props.foo
)
}
})
const wrapper = mount(Foo, {
props: {
foo: 'hello'
}
})
expect(wrapper.text()).toEqual('hello')
await wrapper.trigger('click')
expect(wrapper.text()).toEqual('hello')
})
describe('edge case with symbol props and stubs', () => {
it('works with Symbol as default', () => {
const Comp = defineComponent({
template: `<div>Symbol: {{ sym }}</div>`,
props: {
sym: {
type: Symbol,
default: () => Symbol()
}
}
})
const wrapper = shallowMount(Comp)
expect(wrapper.html()).toBe('<div>Symbol: Symbol()</div>')
})
it('works with symbol as array syntax', () => {
const Comp = defineComponent({
name: 'Comp',
template: `<div>Symbol: {{ sym }}</div>`,
props: ['sym']
})
const wrapper = shallowMount({
render() {
return h(Comp, { sym: Symbol() })
}
})
expect(wrapper.html()).toBe('<comp-stub sym="Symbol()"></comp-stub>')
})
it('works with symbol as default from SFC', () => {
const App = defineComponent({
template: `<PropWithSymbol :sym="sym" />`,
components: { PropWithSymbol },
data() {
return {
sym: Symbol()
}
}
})
const wrapper = shallowMount(App)
expect(wrapper.html()).toBe(
'<prop-with-symbol-stub sym="Symbol()"></prop-with-symbol-stub>'
)
})
})
})