forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetProps.spec.js
227 lines (207 loc) · 6.74 KB
/
setProps.spec.js
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 { compileToFunctions } from 'vue-template-compiler'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
import { describeWithShallowAndMount, vueVersion } from '~resources/utils'
describeWithShallowAndMount('setProps', mountingMethod => {
let info
beforeEach(() => {
info = sinon.stub(console, 'info')
})
afterEach(() => {
info.restore()
})
it('sets component props and updates DOM when called on Vue instance', () => {
const prop1 = 'prop 1'
const prop2 = 'prop 2'
const propsData = { prop1: 'a prop', prop2 }
const wrapper = mountingMethod(ComponentWithProps, { propsData })
wrapper.setProps({ prop1 })
expect(wrapper.find('.prop-1').element.textContent).to.equal(prop1)
expect(wrapper.find('.prop-2').element.textContent).to.equal(prop2)
})
it('sets props and updates when called with same object', () => {
const TestComponent = {
template: '<div v-if="obj && obj.shouldRender()" />',
props: ['obj']
}
const obj = {
shouldRender: () => false
}
const wrapper = mountingMethod(TestComponent)
obj.shouldRender = () => true
wrapper.setProps({ obj })
expect(wrapper.is('div')).to.equal(true)
})
it('throws error if component does not include props key', () => {
const TestComponent = {
template: '<div></div>'
}
const message = `[vue-test-utils]: wrapper.setProps() called ` +
`with prop1 property which is not defined on the component`
const fn = () => mountingMethod(TestComponent).setProps({ prop1: 'prop' })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('throws error when called on functional vnode', () => {
const AFunctionalComponent = {
render: (h, context) => h('div', context.prop1),
functional: true
}
const message =
'[vue-test-utils]: wrapper.setProps() cannot be called on a functional component'
const fn = () =>
mountingMethod(AFunctionalComponent).setProps({ prop1: 'prop' })
expect(fn)
.to.throw()
.with.property('message', message)
// find on functional components isn't supported in Vue < 2.3
if (vueVersion < 2.3) {
return
}
const TestComponent = {
template: '<div><a-functional-component /></div>',
components: {
AFunctionalComponent
}
}
const fn2 = () =>
mountingMethod(TestComponent)
.find(AFunctionalComponent)
.setProps({ prop1: 'prop' })
expect(fn2)
.to.throw()
.with.property('message', message)
})
it('sets component props, and updates DOM when propsData was not initially passed', () => {
const prop1 = 'prop 1'
const prop2 = 'prop s'
const wrapper = mountingMethod(ComponentWithProps)
wrapper.setProps({ prop1, prop2 })
expect(wrapper.find('.prop-1').element.textContent).to.equal(prop1)
expect(wrapper.find('.prop-2').element.textContent).to.equal(prop2)
})
it('runs watch function when prop is updated', () => {
const wrapper = mountingMethod(ComponentWithWatch)
const prop1 = 'testest'
wrapper.setProps({ prop1 })
expect(wrapper.vm.prop2).to.equal(prop1)
})
it('runs watch function after all props are updated', () => {
const wrapper = mountingMethod(ComponentWithWatch)
const prop1 = 'testest'
wrapper.setProps({ prop2: 'newProp', prop1 })
expect(info.args[1][0]).to.equal(prop1)
})
it('should not run watchers if prop updated is null', () => {
const TestComponent = {
template: `
<div>
<div v-if="!message">There is no message yet</div>
<div v-else>{{ reversedMessage }}</div>
</div>
`,
computed: {
reversedMessage: function () {
return this.message
.split('')
.reverse()
.join('')
}
},
props: ['message']
}
const wrapper = mountingMethod(TestComponent, {
propsData: {
message: 'message'
}
})
wrapper.setProps({ message: null })
expect(wrapper.text()).to.equal('There is no message yet')
})
it('runs watchers correctly', () => {
const TestComponent = {
template: `<div id="app">
{{ stringified }}
</div>`,
props: ['collection'],
data: () => ({
data: ''
}),
computed: {
stringified () {
return this.collection.join(',')
}
},
watch: {
collection: 'execute'
},
methods: {
execute () {
this.data = this.stringified
}
}
}
const wrapper = mountingMethod(TestComponent, {
propsData: { collection: [] }
})
expect(wrapper.vm.stringified).to.equal('')
expect(wrapper.vm.data).to.equal('')
wrapper.setProps({ collection: [1, 2, 3] })
expect(wrapper.vm.stringified).to.equal('1,2,3')
expect(wrapper.vm.data).to.equal('1,2,3')
wrapper.vm.collection.push(4, 5)
expect(wrapper.vm.stringified).to.equal('1,2,3,4,5')
expect(wrapper.vm.data).to.equal('1,2,3,4,5')
})
it('should same reference when called with same object', () => {
const TestComponent = {
template: `<div></div>`,
props: ['obj']
}
const wrapper = mountingMethod(TestComponent)
const obj = {}
wrapper.setProps({ obj })
expect(wrapper.props().obj).to.equal(obj)
})
it('throws an error if property is same reference', () => {
const TestComponent = {
template: `<div></div>`,
props: ['obj']
}
const obj = {}
const wrapper = mountingMethod(TestComponent, {
propsData: {
obj
}
})
const message = '[vue-test-utils]: wrapper.setProps() called with the same object of the existing obj property. You must call wrapper.setProps() with a new object to trigger reactivity'
const fn = () => wrapper.setProps({ obj })
expect(fn)
.to.throw()
.with.property('message', message)
})
it('updates watched prop', () => {
const TestComponent = {
template: '<div />',
props: ['propA'],
mounted () {
this.$watch('propA', function () {
this.propA
}, { immediate: true }
)
}
}
const wrapper = mountingMethod(TestComponent, { propsData: { propA: 'none' }})
wrapper.setProps({ propA: 'value' })
expect(wrapper.props().propA).to.equal('value')
expect(wrapper.vm.propA).to.equal('value')
})
it('throws an error if node is not a Vue instance', () => {
const message = 'wrapper.setProps() can only be called on a Vue instance'
const compiled = compileToFunctions('<div><p></p></div>')
const wrapper = mountingMethod(compiled)
const p = wrapper.find('p')
expect(() => p.setProps({ ready: true })).throw(Error, message)
})
})