Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): align the merge strategies of component options with the behavior of vue2 #3600

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,204 @@ describe('api: options', () => {
).toHaveBeenWarned()
})
})

describe('options merge strategies', () => {
test('this.$options.data', () => {
const mixin = {
data() {
return { foo: 1, bar: 2 }
}
}
createApp({
mixins: [mixin],
data() {
return {
foo: 3,
baz: 4
}
},
created() {
expect(this.$options.data).toBeInstanceOf(Function)
expect(this.$options.data()).toEqual({
foo: 3,
bar: 2,
baz: 4
})
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options.watch', () => {
const mixin = {
watch: {
a() {},
b() {}
}
}
createApp({
mixins: [mixin],
data() {
return {
a: 1,
b: 2
}
},
watch: {
a() {}
},
created() {
expect(this.$options.watch.a).toBeInstanceOf(Array)
expect(this.$options.watch.a.length).toBe(2)
expect(this.$options.watch.b).toBeInstanceOf(Function)
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options.inject', () => {
const mixin = {
inject: ['a']
}
createApp({
mixins: [mixin],
inject: ['b'],
created() {
expect(this.$options.inject.a).toEqual({ from: 'a' })
expect(this.$options.inject.b).toEqual({ from: 'b' })
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options.provide', () => {
const mixin = {
provide: {
a: 1
}
}
createApp({
mixins: [mixin],
provide() {
return {
b: 2
}
},
created() {
expect(this.$options.provide).toBeInstanceOf(Function)
expect(this.$options.provide()).toEqual({ a: 1, b: 2 })
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options[lifecycle-name]', () => {
const mixin = {
mounted() {}
}
createApp({
mixins: [mixin],
mounted() {},
created() {
expect(this.$options.mounted).toBeInstanceOf(Array)
expect(this.$options.mounted.length).toBe(2)
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options[asset-name]', () => {
const mixin = {
components: {
a: {}
},
directives: {
d1: {}
}
}
createApp({
mixins: [mixin],
components: {
b: {}
},
directives: {
d2: {}
},
created() {
expect('a' in this.$options.components).toBe(true)
expect('b' in this.$options.components).toBe(true)
expect('d1' in this.$options.directives).toBe(true)
expect('d2' in this.$options.directives).toBe(true)
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options.methods', () => {
const mixin = {
methods: {
fn1() {}
}
}
createApp({
mixins: [mixin],
methods: {
fn2() {}
},
created() {
expect(this.$options.methods.fn1).toBeInstanceOf(Function)
expect(this.$options.methods.fn2).toBeInstanceOf(Function)
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

test('this.$options.computed', () => {
const mixin = {
computed: {
c1() {}
}
}
createApp({
mixins: [mixin],
computed: {
c2() {}
},
created() {
expect(this.$options.computed.c1).toBeInstanceOf(Function)
expect(this.$options.computed.c2).toBeInstanceOf(Function)
},
render: () => null
}).mount(nodeOps.createElement('div'))
})

// #2791
test('modify $options in the beforeCreate hook', async () => {
const count = ref(0)
const mixin = {
data() {
return { foo: 1 }
},
beforeCreate(this: any) {
if (!this.$options.computed) {
this.$options.computed = {}
}
this.$options.computed.value = () => count.value
}
}
const root = nodeOps.createElement('div')
createApp({
mixins: [mixin],
render(this: any) {
return this.value
}
}).mount(root)

expect(serializeInner(root)).toBe('0')

count.value++
await nextTick()
expect(serializeInner(root)).toBe('1')
})
})
})
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export type OptionMergeFunction = (
to: unknown,
from: unknown,
instance: any,
key: string
key: string,
asMixin: boolean
) => any

export interface AppConfig {
Expand Down
Loading