-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.spec.js
126 lines (118 loc) · 2.63 KB
/
data.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
import Vue from 'vue'
describe('Options data', () => {
it('should proxy and be reactive', done => {
const data = { msg: 'foo' }
const vm = new Vue({
data,
template: '<div>{{ msg }}</div>'
}).$mount()
expect(vm.$data).toEqual({ msg: 'foo' })
expect(vm.$data).toBe(data)
data.msg = 'bar'
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('bar')
}).then(done)
})
it('should merge data properly', () => {
const Test = Vue.extend({
data () {
return { a: 1 }
}
})
let vm = new Test({
data: { b: 2 }
})
expect(vm.a).toBe(1)
expect(vm.b).toBe(2)
// no instance data
vm = new Test()
expect(vm.a).toBe(1)
// no child-val
const Extended = Test.extend({})
vm = new Extended()
expect(vm.a).toBe(1)
// recursively merge objects
const WithObject = Vue.extend({
data () {
return {
obj: {
a: 1
}
}
}
})
vm = new WithObject({
data: {
obj: {
b: 2
}
}
})
expect(vm.obj.a).toBe(1)
expect(vm.obj.b).toBe(2)
})
it('should warn non-function during extend', () => {
Vue.extend({
data: { msg: 'foo' }
})
expect('The "data" option should be a function').toHaveBeenWarned()
})
it('should warn non object return', () => {
new Vue({
data () {}
})
expect('data functions should return an object').toHaveBeenWarned()
})
it('should warn replacing root $data', () => {
const vm = new Vue({
data: {}
})
vm.$data = {}
expect('Avoid replacing instance root $data').toHaveBeenWarned()
})
it('should have access to props', () => {
const Test = {
props: ['a'],
render () {},
data () {
return {
b: this.a
}
}
}
const vm = new Vue({
template: `<test ref="test" :a="1"></test>`,
components: { Test }
}).$mount()
expect(vm.$refs.test.b).toBe(1)
})
it('should have access to methods', () => {
const vm = new Vue({
methods: {
get () {
return { a: 1 }
}
},
data () {
return this.get()
}
})
expect(vm.a).toBe(1)
})
it('should called with this', () => {
const vm = new Vue({
template: '<div><child></child></div>',
provide: { foo: 1 },
components: {
child: {
template: '<span>{{bar}}</span>',
inject: ['foo'],
data ({ foo }) {
return { bar: 'foo:' + foo }
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>foo:1</span>')
})
})