-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_spec.js
162 lines (148 loc) · 3.77 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
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
var Vue = require('../../../../src/vue')
var _ = require('../../../../src/util')
var nextTick = _.nextTick
describe('Data API', function () {
var vm
beforeEach(function () {
spyOn(_, 'warn')
vm = new Vue({
data: {
a: 1,
b: {
c: 2
}
},
filters: {
double: function (v) {
return v * 2
}
}
})
})
it('$get', function () {
expect(vm.$get('a')).toBe(1)
expect(vm.$get('b["c"]')).toBe(2)
expect(vm.$get('a + b.c')).toBe(3)
expect(vm.$get('c')).toBeUndefined()
// invalid, should warn
vm.$get('a(')
expect(hasWarned(_, 'Invalid expression')).toBe(true)
})
it('$set', function () {
vm.$set('a', 2)
expect(vm.a).toBe(2)
vm.$set('b["c"]', 3)
expect(vm.b.c).toBe(3)
// setting unexisting
vm.$set('c.d', 2)
expect(vm.c.d).toBe(2)
// invalid, should throw
if (leftHandThrows()) {
// if creating a function with invalid left hand
// expression throws, the exp parser will catch the
// error and warn.
vm.$set('c + d', 1)
expect(hasWarned(_, 'Invalid setter function body')).toBe(true)
} else {
// otherwise it will throw when calling the setter.
expect(function () {
try {
vm.$set('c + d', 1)
} catch (e) {
return true
}
}()).toBe(true)
}
})
it('$add', function () {
vm._digest = jasmine.createSpy()
vm.$add('c', 1)
expect(vm.c).toBe(1)
expect(vm._data.c).toBe(1)
expect(vm._digest).toHaveBeenCalled()
// reserved key should not be proxied
vm.$add('_c', 1)
expect(vm._c).toBeUndefined()
})
it('$delete', function () {
vm._digest = jasmine.createSpy()
vm.$delete('a')
expect(vm.hasOwnProperty('a')).toBe(false)
expect(vm._data.hasOwnProperty('a')).toBe(false)
expect(vm._digest).toHaveBeenCalled()
// reserved key should not be deleted
vm.$delete('_data')
expect(vm._data).toBeTruthy()
})
it('$watch', function (done) {
var spy = jasmine.createSpy()
// test immediate invoke
var unwatch = vm.$watch('a + b.c', spy, false, true)
expect(spy).toHaveBeenCalledWith(3, undefined)
vm.a = 2
nextTick(function () {
expect(spy).toHaveBeenCalledWith(4, 3)
// unwatch
unwatch()
vm.a = 3
nextTick(function () {
expect(spy.calls.count()).toBe(2)
done()
})
})
})
it('deep $watch', function (done) {
var oldB = vm.b
var spy = jasmine.createSpy()
vm.$watch('b', spy, true)
vm.b.c = 3
nextTick(function () {
expect(spy).toHaveBeenCalledWith(oldB, oldB)
vm.b = { c: 4 }
nextTick(function () {
expect(spy).toHaveBeenCalledWith(vm.b, oldB)
done()
})
})
})
it('$eval', function () {
expect(vm.$eval('a')).toBe(1)
expect(vm.$eval('b.c')).toBe(2)
expect(vm.$eval('a + b.c | double')).toBe(6)
})
it('$interpolate', function () {
expect(vm.$interpolate('abc')).toBe('abc')
expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
})
if (typeof console !== 'undefined') {
it('$log', function () {
var oldLog = console.log
var spy = jasmine.createSpy()
console.log = function (val) {
expect(val.a).toBe(1)
expect(val.b.c).toBe(2)
spy()
}
vm.$log()
expect(spy.calls.count()).toBe(1)
console.log = function (val) {
expect(val.c).toBe(2)
spy()
}
vm.$log('b')
expect(spy.calls.count()).toBe(2)
console.log = oldLog
})
}
})
/**
* check if creating a new Function with invalid left-hand
* assignment would throw
*/
function leftHandThrows () {
try {
var fn = new Function('a + b = 1')
} catch (e) {
return true
}
}