test: tests for script setup helpers
This commit is contained in:
parent
73cdb9d420
commit
426a6c996e
49
packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
Normal file
49
packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import {
|
||||||
|
defineComponent,
|
||||||
|
h,
|
||||||
|
nodeOps,
|
||||||
|
render,
|
||||||
|
SetupContext
|
||||||
|
} from '@vue/runtime-test'
|
||||||
|
import { defineEmit, defineProps, useContext } from '../src/apiSetupHelpers'
|
||||||
|
|
||||||
|
describe('SFC <script setup> helpers', () => {
|
||||||
|
test('should warn runtime usage', () => {
|
||||||
|
defineProps()
|
||||||
|
expect(`defineProps() is a compiler-hint`).toHaveBeenWarned()
|
||||||
|
|
||||||
|
defineEmit()
|
||||||
|
expect(`defineEmit() is a compiler-hint`).toHaveBeenWarned()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('useContext (no args)', () => {
|
||||||
|
let ctx: SetupContext | undefined
|
||||||
|
const Comp = {
|
||||||
|
setup() {
|
||||||
|
ctx = useContext()
|
||||||
|
return () => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
render(h(Comp), nodeOps.createElement('div'))
|
||||||
|
expect(ctx).toMatchObject({
|
||||||
|
attrs: {},
|
||||||
|
slots: {}
|
||||||
|
})
|
||||||
|
expect(typeof ctx!.emit).toBe('function')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('useContext (with args)', () => {
|
||||||
|
let ctx: SetupContext | undefined
|
||||||
|
let ctxArg: SetupContext | undefined
|
||||||
|
const Comp = defineComponent({
|
||||||
|
setup(_, _ctxArg) {
|
||||||
|
ctx = useContext()
|
||||||
|
ctxArg = _ctxArg
|
||||||
|
return () => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
render(h(Comp), nodeOps.createElement('div'))
|
||||||
|
expect(ctx).toBeDefined()
|
||||||
|
expect(ctx).toBe(ctxArg)
|
||||||
|
})
|
||||||
|
})
|
@ -27,8 +27,8 @@ export function defineProps<
|
|||||||
InferredProps = ExtractPropTypes<PP>
|
InferredProps = ExtractPropTypes<PP>
|
||||||
>(props?: PP): Readonly<TypeProps extends undefined ? InferredProps : TypeProps>
|
>(props?: PP): Readonly<TypeProps extends undefined ? InferredProps : TypeProps>
|
||||||
// implementation
|
// implementation
|
||||||
export function defineProps(props?: any) {
|
export function defineProps() {
|
||||||
if (__DEV__ && props) {
|
if (__DEV__) {
|
||||||
warn(
|
warn(
|
||||||
`defineProps() is a compiler-hint helper that is only usable inside ` +
|
`defineProps() is a compiler-hint helper that is only usable inside ` +
|
||||||
`<script setup> of a single file component. Its arguments should be ` +
|
`<script setup> of a single file component. Its arguments should be ` +
|
||||||
@ -45,8 +45,8 @@ export function defineEmit<
|
|||||||
InferredEmit = EmitFn<E>
|
InferredEmit = EmitFn<E>
|
||||||
>(emitOptions?: E | EE[]): TypeEmit extends undefined ? InferredEmit : TypeEmit
|
>(emitOptions?: E | EE[]): TypeEmit extends undefined ? InferredEmit : TypeEmit
|
||||||
// implementation
|
// implementation
|
||||||
export function defineEmit(emitOptions?: any) {
|
export function defineEmit() {
|
||||||
if (__DEV__ && emitOptions) {
|
if (__DEV__) {
|
||||||
warn(
|
warn(
|
||||||
`defineEmit() is a compiler-hint helper that is only usable inside ` +
|
`defineEmit() is a compiler-hint helper that is only usable inside ` +
|
||||||
`<script setup> of a single file component. Its arguments should be ` +
|
`<script setup> of a single file component. Its arguments should be ` +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user