Skip to content

Commit a6c8f40

Browse files
committed
test $data swapping for all prop types
1 parent 58740c1 commit a6c8f40

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

src/instance/scope.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ exports._setData = function (newData) {
5959
var oldData = this._data
6060
this._data = newData
6161
var keys, key, i
62-
// copy props
62+
// copy props.
63+
// this should only happen during a v-repeat of component
64+
// that also happens to have compiled props.
6365
var props = this.$options.props
6466
if (props) {
6567
i = props.length
6668
while (i--) {
6769
key = props[i]
68-
if (key !== '$data') {
70+
if (key !== '$data' && !newData.hasOwnProperty(key)) {
6971
newData.$set(key, oldData[key])
7072
}
7173
}

test/unit/specs/instance/scope_spec.js

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,86 @@ describe('Instance Scope', function () {
3333

3434
describe('$data', function () {
3535

36-
var vm = new Vue({
37-
props: ['c'],
38-
data: {
39-
a: 1
40-
}
41-
})
42-
4336
it('should initialize props', function () {
37+
var vm = new Vue({
38+
props: ['c'],
39+
data: {
40+
a: 1
41+
}
42+
})
4443
expect(vm.hasOwnProperty('c')).toBe(true)
4544
})
4645

4746
it('replace $data', function () {
48-
vm.c = 3
47+
var vm = new Vue({
48+
data: {
49+
a: 1
50+
}
51+
})
4952
vm.$data = { b: 2 }
5053
// proxy new key
5154
expect(vm.b).toBe(2)
5255
// unproxy old key that's no longer present
5356
expect(vm.hasOwnProperty('a')).toBe(false)
54-
// should copy props
57+
})
58+
59+
it('replace $data and handle props', function () {
60+
var el = document.createElement('div')
61+
var vm = new Vue({
62+
el: el,
63+
template: '<test a="{{a}}" b="{{*b}}" c="{{<c}}" d="{{>d}}"></test>',
64+
data: {
65+
a: 1,
66+
b: 2,
67+
c: 3,
68+
d: 0
69+
},
70+
components: {
71+
test: {
72+
props: ['a', 'b', 'c', 'd'],
73+
data: function () {
74+
return {
75+
d: 4
76+
}
77+
}
78+
}
79+
}
80+
})
81+
var child = vm._children[0]
82+
expect(child.a).toBe(1)
83+
expect(child.b).toBe(2)
84+
expect(child.c).toBe(3)
85+
expect(vm.d).toBe(4)
86+
expect(child.d).toBe(4)
87+
88+
// test new data without prop fields:
89+
// should just copy
90+
child.$data = {}
91+
expect(child.a).toBe(1)
92+
expect(child.b).toBe(2)
93+
expect(child.c).toBe(3)
94+
expect(vm.d).toBe(4)
95+
expect(child.d).toBe(4)
96+
97+
// test new data with value:
98+
child.$data = {
99+
a: 2, // two-way
100+
b: 3, // one-time
101+
c: 4, // one-way-down
102+
d: 5 // one-way-up
103+
}
104+
// two-way
105+
expect(child.a).toBe(2)
106+
expect(vm.a).toBe(2)
107+
// one-time
108+
expect(child.b).toBe(3)
109+
expect(vm.b).toBe(2)
110+
// one-way down
111+
expect(child.c).toBe(4)
55112
expect(vm.c).toBe(3)
113+
// one-way up
114+
expect(vm.d).toBe(5)
115+
expect(child.d).toBe(5)
56116
})
57117

58118
})
@@ -178,4 +238,4 @@ describe('Instance Scope', function () {
178238

179239
})
180240

181-
})
241+
})

0 commit comments

Comments
 (0)