vue3-yuanma/packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
2021-06-23 10:31:32 -04:00

62 lines
1.5 KiB
TypeScript

import {
defineComponent,
h,
nodeOps,
render,
SetupContext
} from '@vue/runtime-test'
import {
defineEmits,
defineProps,
useAttrs,
useSlots
} from '../src/apiSetupHelpers'
describe('SFC <script setup> helpers', () => {
test('should warn runtime usage', () => {
defineProps()
expect(`defineProps() is a compiler-hint`).toHaveBeenWarned()
defineEmits()
expect(`defineEmits() is a compiler-hint`).toHaveBeenWarned()
})
test('useSlots / useAttrs (no args)', () => {
let slots: SetupContext['slots'] | undefined
let attrs: SetupContext['attrs'] | undefined
const Comp = {
setup() {
slots = useSlots()
attrs = useAttrs()
return () => {}
}
}
const passedAttrs = { id: 'foo' }
const passedSlots = {
default: () => {},
x: () => {}
}
render(h(Comp, passedAttrs, passedSlots), nodeOps.createElement('div'))
expect(typeof slots!.default).toBe('function')
expect(typeof slots!.x).toBe('function')
expect(attrs).toMatchObject(passedAttrs)
})
test('useSlots / useAttrs (with args)', () => {
let slots: SetupContext['slots'] | undefined
let attrs: SetupContext['attrs'] | undefined
let ctx: SetupContext | undefined
const Comp = defineComponent({
setup(_, _ctx) {
slots = useSlots()
attrs = useAttrs()
ctx = _ctx
return () => {}
}
})
render(h(Comp), nodeOps.createElement('div'))
expect(slots).toBe(ctx!.slots)
expect(attrs).toBe(ctx!.attrs)
})
})