feat(compiler-sfc): <script setup> defineProps destructure transform (#4690)

This commit is contained in:
Evan You
2021-09-27 14:24:21 -04:00
committed by GitHub
parent d84d5ecdbd
commit 467e113b95
14 changed files with 717 additions and 124 deletions

View File

@@ -11,7 +11,8 @@ import {
SetupContext,
Suspense,
computed,
ComputedRef
ComputedRef,
shallowReactive
} from '@vue/runtime-test'
import {
defineEmits,
@@ -21,7 +22,8 @@ import {
useAttrs,
useSlots,
mergeDefaults,
withAsyncContext
withAsyncContext,
createPropsRestProxy
} from '../src/apiSetupHelpers'
describe('SFC <script setup> helpers', () => {
@@ -77,26 +79,62 @@ describe('SFC <script setup> helpers', () => {
expect(attrs).toBe(ctx!.attrs)
})
test('mergeDefaults', () => {
const merged = mergeDefaults(
{
foo: null,
bar: { type: String, required: false }
},
{
foo: 1,
bar: 'baz'
}
)
expect(merged).toMatchObject({
foo: { default: 1 },
bar: { type: String, required: false, default: 'baz' }
describe('mergeDefaults', () => {
test('object syntax', () => {
const merged = mergeDefaults(
{
foo: null,
bar: { type: String, required: false },
baz: String
},
{
foo: 1,
bar: 'baz',
baz: 'qux'
}
)
expect(merged).toMatchObject({
foo: { default: 1 },
bar: { type: String, required: false, default: 'baz' },
baz: { type: String, default: 'qux' }
})
})
mergeDefaults({}, { foo: 1 })
expect(
`props default key "foo" has no corresponding declaration`
).toHaveBeenWarned()
test('array syntax', () => {
const merged = mergeDefaults(['foo', 'bar', 'baz'], {
foo: 1,
bar: 'baz',
baz: 'qux'
})
expect(merged).toMatchObject({
foo: { default: 1 },
bar: { default: 'baz' },
baz: { default: 'qux' }
})
})
test('should warn missing', () => {
mergeDefaults({}, { foo: 1 })
expect(
`props default key "foo" has no corresponding declaration`
).toHaveBeenWarned()
})
})
describe('createPropsRestProxy', () => {
const original = shallowReactive({
foo: 1,
bar: 2,
baz: 3
})
const rest = createPropsRestProxy(original, ['foo', 'bar'])
expect('foo' in rest).toBe(false)
expect('bar' in rest).toBe(false)
expect(rest.baz).toBe(3)
expect(Object.keys(rest)).toEqual(['baz'])
original.baz = 4
expect(rest.baz).toBe(4)
})
describe('withAsyncContext', () => {