fix(compat): maintain compatConfig option in legacy functional comp (#4974)

This commit is contained in:
Illya Klymov 2021-12-06 06:20:27 +02:00 committed by GitHub
parent 595a93715b
commit ee97cf5a4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 39 deletions

View File

@ -55,6 +55,7 @@ export function convertLegacyFunctionalComponent(comp: ComponentOptions) {
} }
Func.props = comp.props Func.props = comp.props
Func.displayName = comp.name Func.displayName = comp.name
Func.compatConfig = comp.compatConfig
// v2 functional components do not inherit attrs // v2 functional components do not inherit attrs
Func.inheritAttrs = false Func.inheritAttrs = false

View File

@ -60,7 +60,11 @@ import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext' import { currentRenderingInstance } from './componentRenderContext'
import { startMeasure, endMeasure } from './profiling' import { startMeasure, endMeasure } from './profiling'
import { convertLegacyRenderFn } from './compat/renderFn' import { convertLegacyRenderFn } from './compat/renderFn'
import { globalCompatConfig, validateCompatConfig } from './compat/compatConfig' import {
CompatConfig,
globalCompatConfig,
validateCompatConfig
} from './compat/compatConfig'
import { SchedulerJob } from './scheduler' import { SchedulerJob } from './scheduler'
export type Data = Record<string, unknown> export type Data = Record<string, unknown>
@ -111,6 +115,7 @@ export interface FunctionalComponent<P = {}, E extends EmitsOptions = {}>
emits?: E | (keyof E)[] emits?: E | (keyof E)[]
inheritAttrs?: boolean inheritAttrs?: boolean
displayName?: string displayName?: string
compatConfig?: CompatConfig
} }
export interface ClassComponent { export interface ClassComponent {

View File

@ -18,45 +18,80 @@ afterEach(() => {
Vue.configureCompat({ MODE: 3 }) Vue.configureCompat({ MODE: 3 })
}) })
test('COMPONENT_FUNCTIONAL', async () => { describe('COMPONENT_FUNCTIONAL', () => {
const func = { test('basic usage', async () => {
name: 'Func', const func = {
functional: true, name: 'Func',
props: { functional: true,
x: String props: {
}, x: String
inject: ['foo'], },
render: (h: any, { data, props, injections, slots }: any) => { inject: ['foo'],
return h('div', { id: props.x, class: data.class }, [ render: (h: any, { data, props, injections, slots }: any) => {
h('div', { class: 'inject' }, injections.foo), return h('div', { id: props.x, class: data.class }, [
h('div', { class: 'slot' }, slots().default) h('div', { class: 'inject' }, injections.foo),
]) h('div', { class: 'slot' }, slots().default)
} ])
}
const vm = new Vue({
provide() {
return {
foo: 123
} }
}, }
components: {
func
},
template: `<func class="foo" x="foo">hello</func>`
}).$mount()
expect(vm.$el.id).toBe('foo') const vm = new Vue({
expect(vm.$el.className).toBe('foo') provide() {
expect(vm.$el.querySelector('.inject').textContent).toBe('123') return {
expect(vm.$el.querySelector('.slot').textContent).toBe('hello') foo: 123
expect(vm.$el.outerHTML).toMatchInlineSnapshot( }
`"<div id=\\"foo\\" class=\\"foo\\"><div class=\\"inject\\">123</div><div class=\\"slot\\">hello</div></div>"` },
) components: {
func
},
template: `<func class="foo" x="foo">hello</func>`
}).$mount()
expect( expect(vm.$el.id).toBe('foo')
( expect(vm.$el.className).toBe('foo')
deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL].message as Function expect(vm.$el.querySelector('.inject').textContent).toBe('123')
)(func) expect(vm.$el.querySelector('.slot').textContent).toBe('hello')
).toHaveBeenWarned() expect(vm.$el.outerHTML).toMatchInlineSnapshot(
`"<div id=\\"foo\\" class=\\"foo\\"><div class=\\"inject\\">123</div><div class=\\"slot\\">hello</div></div>"`
)
expect(
(
deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL]
.message as Function
)(func)
).toHaveBeenWarned()
})
test('copies compatConfig option', () => {
const func = {
name: 'Func',
functional: true,
compatConfig: {
ATTR_FALSE_VALUE: 'suppress-warning' as const
},
render: (h: any) => {
// should not render required: false due to compatConfig
return h('div', { 'data-some-attr': false })
}
}
const vm = new Vue({
components: { func },
template: `<func class="foo" x="foo">hello</func>`
}).$mount()
expect(vm.$el.outerHTML).toMatchInlineSnapshot(`"<div></div>"`)
expect(
(
deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL]
.message as Function
)(func)
).toHaveBeenWarned()
expect(
(deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
func
)
).not.toHaveBeenWarned()
})
}) })