Skip to content

Commit

Permalink
fix(compat): fix deep data merge with extended constructor
Browse files Browse the repository at this point in the history
fix #3852
  • Loading branch information
yyx990803 committed May 31, 2021
1 parent ecd97ee commit c7efb96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/runtime-core/src/compat/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function deepMergeData(
to[key] = fromVal
}
}
return to
}

export function mergeDataOption(to: any, from: any) {
Expand Down
29 changes: 26 additions & 3 deletions packages/vue-compat/__tests__/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,42 @@ test('data deep merge', () => {
data: () => ({
foo: {
bar: 1
}
},
selfData: 3
}),
template: `{{ foo }}`
template: `{{ { selfData, foo } }}`
}).$mount()

expect(vm.$el.textContent).toBe(JSON.stringify({ baz: 2, bar: 1 }, null, 2))
expect(vm.$el.textContent).toBe(
JSON.stringify({ selfData: 3, foo: { baz: 2, bar: 1 } }, null, 2)
)
expect(
(deprecationData[DeprecationTypes.OPTIONS_DATA_MERGE].message as Function)(
'foo'
)
).toHaveBeenWarned()
})

// #3852
test('data deep merge w/ extended constructor', () => {
const App = Vue.extend({
template: `<pre>{{ { mixinData, selfData } }}</pre>`,
mixins: [{ data: () => ({ mixinData: 'mixinData' }) }],
data: () => ({ selfData: 'selfData' })
})
const vm = new App().$mount()
expect(vm.$el.textContent).toBe(
JSON.stringify(
{
mixinData: 'mixinData',
selfData: 'selfData'
},
null,
2
)
)
})

test('beforeDestroy/destroyed', async () => {
const beforeDestroy = jest.fn()
const destroyed = jest.fn()
Expand Down

0 comments on commit c7efb96

Please sign in to comment.