2021-04-29 05:21:02 +08:00
|
|
|
import Vue from '@vue/compat'
|
2021-04-30 04:58:14 +08:00
|
|
|
import { nextTick } from '../../runtime-core/src/scheduler'
|
2021-04-29 05:21:02 +08:00
|
|
|
import {
|
|
|
|
DeprecationTypes,
|
|
|
|
deprecationData,
|
|
|
|
toggleDeprecationWarning
|
2021-04-30 04:58:14 +08:00
|
|
|
} from '../../runtime-core/src/compat/compatConfig'
|
2021-04-29 05:21:02 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
toggleDeprecationWarning(true)
|
|
|
|
Vue.configureCompat({
|
|
|
|
MODE: 2,
|
2021-05-13 05:13:44 +08:00
|
|
|
GLOBAL_MOUNT: 'suppress-warning',
|
|
|
|
GLOBAL_EXTEND: 'suppress-warning'
|
2021-04-29 05:21:02 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
toggleDeprecationWarning(false)
|
|
|
|
Vue.configureCompat({ MODE: 3 })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('root data plain object', () => {
|
|
|
|
const vm = new Vue({
|
|
|
|
data: { foo: 1 } as any,
|
|
|
|
template: `{{ foo }}`
|
|
|
|
}).$mount()
|
|
|
|
expect(vm.$el.textContent).toBe('1')
|
|
|
|
expect(
|
|
|
|
deprecationData[DeprecationTypes.OPTIONS_DATA_FN].message
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('data deep merge', () => {
|
|
|
|
const mixin = {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
foo: {
|
|
|
|
baz: 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const vm = new Vue({
|
|
|
|
mixins: [mixin],
|
|
|
|
data: () => ({
|
|
|
|
foo: {
|
|
|
|
bar: 1
|
2021-06-01 04:48:18 +08:00
|
|
|
},
|
|
|
|
selfData: 3
|
2021-04-29 05:21:02 +08:00
|
|
|
}),
|
2021-06-01 04:48:18 +08:00
|
|
|
template: `{{ { selfData, foo } }}`
|
2021-04-29 05:21:02 +08:00
|
|
|
}).$mount()
|
|
|
|
|
2021-06-01 04:48:18 +08:00
|
|
|
expect(vm.$el.textContent).toBe(
|
|
|
|
JSON.stringify({ selfData: 3, foo: { baz: 2, bar: 1 } }, null, 2)
|
|
|
|
)
|
2021-04-29 05:21:02 +08:00
|
|
|
expect(
|
|
|
|
(deprecationData[DeprecationTypes.OPTIONS_DATA_MERGE].message as Function)(
|
|
|
|
'foo'
|
|
|
|
)
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2021-06-01 04:48:18 +08:00
|
|
|
// #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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-29 05:21:02 +08:00
|
|
|
test('beforeDestroy/destroyed', async () => {
|
|
|
|
const beforeDestroy = jest.fn()
|
|
|
|
const destroyed = jest.fn()
|
|
|
|
|
|
|
|
const child = {
|
|
|
|
template: `foo`,
|
|
|
|
beforeDestroy,
|
|
|
|
destroyed
|
|
|
|
}
|
|
|
|
|
|
|
|
const vm = new Vue({
|
|
|
|
template: `<child v-if="ok"/>`,
|
|
|
|
data() {
|
|
|
|
return { ok: true }
|
|
|
|
},
|
|
|
|
components: { child }
|
|
|
|
}).$mount() as any
|
|
|
|
|
|
|
|
vm.ok = false
|
|
|
|
await nextTick()
|
|
|
|
expect(beforeDestroy).toHaveBeenCalled()
|
|
|
|
expect(destroyed).toHaveBeenCalled()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
deprecationData[DeprecationTypes.OPTIONS_BEFORE_DESTROY].message
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
deprecationData[DeprecationTypes.OPTIONS_DESTROYED].message
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
2021-05-13 05:13:44 +08:00
|
|
|
|
|
|
|
test('beforeDestroy/destroyed in Vue.extend components', async () => {
|
|
|
|
const beforeDestroy = jest.fn()
|
|
|
|
const destroyed = jest.fn()
|
|
|
|
|
|
|
|
const child = Vue.extend({
|
|
|
|
template: `foo`,
|
|
|
|
beforeDestroy,
|
|
|
|
destroyed
|
|
|
|
})
|
|
|
|
|
|
|
|
const vm = new Vue({
|
|
|
|
template: `<child v-if="ok"/>`,
|
|
|
|
data() {
|
|
|
|
return { ok: true }
|
|
|
|
},
|
|
|
|
components: { child }
|
|
|
|
}).$mount() as any
|
|
|
|
|
|
|
|
vm.ok = false
|
|
|
|
await nextTick()
|
|
|
|
expect(beforeDestroy).toHaveBeenCalled()
|
|
|
|
expect(destroyed).toHaveBeenCalled()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
deprecationData[DeprecationTypes.OPTIONS_BEFORE_DESTROY].message
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
|
|
|
|
expect(
|
|
|
|
deprecationData[DeprecationTypes.OPTIONS_DESTROYED].message
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|