improve $props test case

This commit is contained in:
Evan You 2017-02-14 11:31:30 -05:00
parent 87ffd0da9f
commit 4f6b1014b3

View File

@ -80,20 +80,49 @@ describe('Instance properties', () => {
expect(calls).toEqual(['outer:undefined', 'middle:outer', 'inner:middle', 'next:undefined'])
})
it('$props', () => {
var Comp = Vue.extend({
it('$props', done => {
const Comp = Vue.extend({
props: ['msg'],
template: '<div>{{ msg }}</div>'
template: '<div>{{ msg }} {{ $props.msg }}</div>'
})
var vm = new Comp({
const vm = new Comp({
propsData: {
msg: 'foo'
}
})
}).$mount()
// check render
expect(vm.$el.textContent).toContain('foo foo')
// warn set
vm.$props = {}
expect('$props is readonly').toHaveBeenWarned()
// check existence
expect(vm.$props.msg).toBe('foo')
// check change
Vue.set(vm, 'msg', 'bar')
vm.msg = 'bar'
expect(vm.$props.msg).toBe('bar')
waitForUpdate(() => {
expect(vm.$el.textContent).toContain('bar bar')
}).then(() => {
vm.$props.msg = 'baz'
expect(vm.msg).toBe('baz')
}).then(() => {
expect(vm.$el.textContent).toContain('baz baz')
}).then(done)
})
it('warn mutating $props', () => {
const Comp = {
props: ['msg'],
render () {},
mounted () {
expect(this.$props.msg).toBe('foo')
this.$props.msg = 'bar'
}
}
new Vue({
template: `<comp ref="comp" msg="foo" />`,
components: { Comp }
}).$mount()
expect(`Avoid mutating a prop`).toHaveBeenWarned()
})
})