Skip to content

Commit 48c0c1c

Browse files
strantryyx990803
authored andcommitted
Added check in merge strat for watches if child is already an array (fix #5652) (#5653)
* Added check for if child is already an array If the child is already an array it does not need to be wrapped again. Fixing #5652 * Added unit test for watch merge strat * Moved test to own describe * Added test for merging multiple extends
1 parent 7561b94 commit 48c0c1c

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/core/util/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object {
172172
}
173173
ret[key] = parent
174174
? parent.concat(child)
175-
: [child]
175+
: Array.isArray(child) ? child : [child]
176176
}
177177
return ret
178178
}

test/unit/features/options/extends.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,44 @@ describe('Options extends', () => {
4747
expect(vm.c).toBe(3)
4848
})
4949
})
50+
51+
describe('Options extends with Object.prototype.watch', () => {
52+
beforeAll(function () {
53+
if (!Object.prototype.watch) {
54+
// eslint-disable-next-line no-extend-native
55+
Object.prototype.watch = {
56+
remove: true
57+
}
58+
}
59+
})
60+
afterAll(function () {
61+
if (Object.prototype.watch && Object.prototype.watch.remove) {
62+
delete Object.prototype.watch
63+
}
64+
})
65+
it('should work with global mixins', done => {
66+
Vue.use({
67+
install: function () {
68+
Vue.mixin({})
69+
}
70+
})
71+
const spy = jasmine.createSpy('watch')
72+
const A = Vue.extend({
73+
data: function () {
74+
return { a: 1 }
75+
},
76+
watch: {
77+
a: spy
78+
},
79+
created: function () {
80+
this.a = 2
81+
}
82+
})
83+
new Vue({
84+
extends: A
85+
})
86+
waitForUpdate(() => {
87+
expect(spy).toHaveBeenCalledWith(2, 1)
88+
}).then(done)
89+
})
90+
})

test/unit/features/options/watch.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,43 @@ describe('Options watch', () => {
104104
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
105105
}).then(done)
106106
})
107+
108+
it('correctly merges multiple extends', done => {
109+
var spy2 = jasmine.createSpy('A')
110+
var spy3 = jasmine.createSpy('B')
111+
var A = Vue.extend({
112+
data: function () {
113+
return {
114+
a: 0,
115+
b: 0
116+
}
117+
},
118+
watch: {
119+
b: spy
120+
}
121+
})
122+
123+
var B = Vue.extend({
124+
extends: A,
125+
watch: {
126+
a: spy2
127+
}
128+
})
129+
130+
var C = Vue.extend({
131+
extends: B,
132+
watch: {
133+
a: spy3
134+
}
135+
})
136+
137+
var vm = new C()
138+
vm.a = 1
139+
140+
waitForUpdate(() => {
141+
expect(spy).not.toHaveBeenCalled()
142+
expect(spy2).toHaveBeenCalledWith(1, 0)
143+
expect(spy3).toHaveBeenCalledWith(1, 0)
144+
}).then(done)
145+
})
107146
})

0 commit comments

Comments
 (0)