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
This commit is contained in:
strantr 2017-06-06 06:48:31 +01:00 committed by Evan You
parent 7561b94eeb
commit 48c0c1ceb5
3 changed files with 81 additions and 1 deletions

View File

@ -172,7 +172,7 @@ strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object {
}
ret[key] = parent
? parent.concat(child)
: [child]
: Array.isArray(child) ? child : [child]
}
return ret
}

View File

@ -47,3 +47,44 @@ describe('Options extends', () => {
expect(vm.c).toBe(3)
})
})
describe('Options extends with Object.prototype.watch', () => {
beforeAll(function () {
if (!Object.prototype.watch) {
// eslint-disable-next-line no-extend-native
Object.prototype.watch = {
remove: true
}
}
})
afterAll(function () {
if (Object.prototype.watch && Object.prototype.watch.remove) {
delete Object.prototype.watch
}
})
it('should work with global mixins', done => {
Vue.use({
install: function () {
Vue.mixin({})
}
})
const spy = jasmine.createSpy('watch')
const A = Vue.extend({
data: function () {
return { a: 1 }
},
watch: {
a: spy
},
created: function () {
this.a = 2
}
})
new Vue({
extends: A
})
waitForUpdate(() => {
expect(spy).toHaveBeenCalledWith(2, 1)
}).then(done)
})
})

View File

@ -104,4 +104,43 @@ describe('Options watch', () => {
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
}).then(done)
})
it('correctly merges multiple extends', done => {
var spy2 = jasmine.createSpy('A')
var spy3 = jasmine.createSpy('B')
var A = Vue.extend({
data: function () {
return {
a: 0,
b: 0
}
},
watch: {
b: spy
}
})
var B = Vue.extend({
extends: A,
watch: {
a: spy2
}
})
var C = Vue.extend({
extends: B,
watch: {
a: spy3
}
})
var vm = new C()
vm.a = 1
waitForUpdate(() => {
expect(spy).not.toHaveBeenCalled()
expect(spy2).toHaveBeenCalledWith(1, 0)
expect(spy3).toHaveBeenCalledWith(1, 0)
}).then(done)
})
})