Skip to content

Commit 4f6b101

Browse files
committed
improve $props test case
1 parent 87ffd0d commit 4f6b101

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

test/unit/features/instance/properties.spec.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,49 @@ describe('Instance properties', () => {
8080
expect(calls).toEqual(['outer:undefined', 'middle:outer', 'inner:middle', 'next:undefined'])
8181
})
8282

83-
it('$props', () => {
84-
var Comp = Vue.extend({
83+
it('$props', done => {
84+
const Comp = Vue.extend({
8585
props: ['msg'],
86-
template: '<div>{{ msg }}</div>'
86+
template: '<div>{{ msg }} {{ $props.msg }}</div>'
8787
})
88-
var vm = new Comp({
88+
const vm = new Comp({
8989
propsData: {
9090
msg: 'foo'
9191
}
92-
})
92+
}).$mount()
93+
// check render
94+
expect(vm.$el.textContent).toContain('foo foo')
95+
// warn set
96+
vm.$props = {}
97+
expect('$props is readonly').toHaveBeenWarned()
9398
// check existence
9499
expect(vm.$props.msg).toBe('foo')
95100
// check change
96-
Vue.set(vm, 'msg', 'bar')
101+
vm.msg = 'bar'
97102
expect(vm.$props.msg).toBe('bar')
103+
waitForUpdate(() => {
104+
expect(vm.$el.textContent).toContain('bar bar')
105+
}).then(() => {
106+
vm.$props.msg = 'baz'
107+
expect(vm.msg).toBe('baz')
108+
}).then(() => {
109+
expect(vm.$el.textContent).toContain('baz baz')
110+
}).then(done)
111+
})
112+
113+
it('warn mutating $props', () => {
114+
const Comp = {
115+
props: ['msg'],
116+
render () {},
117+
mounted () {
118+
expect(this.$props.msg).toBe('foo')
119+
this.$props.msg = 'bar'
120+
}
121+
}
122+
new Vue({
123+
template: `<comp ref="comp" msg="foo" />`,
124+
components: { Comp }
125+
}).$mount()
126+
expect(`Avoid mutating a prop`).toHaveBeenWarned()
98127
})
99128
})

0 commit comments

Comments
 (0)