-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathsetProps.spec.js
153 lines (139 loc) · 4.88 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
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('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 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('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)
})
})