| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- import {
- type VaporElementConstructor,
- defineVaporComponent,
- defineVaporCustomElement,
- } from 'vue'
- import { describe, expectType, test } from '../utils'
- describe('defineVaporCustomElement using defineVaporComponent return type', () => {
- test('with object emits', () => {
- const Comp1Vapor = defineVaporComponent({
- props: {
- a: String,
- },
- emits: {
- click: () => true,
- },
- })
- const Comp = defineVaporCustomElement(Comp1Vapor)
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<string | undefined>(instance.a)
- instance.a = ''
- })
- test('with array emits', () => {
- const Comp1Vapor = defineVaporComponent({
- props: {
- a: Number,
- },
- emits: ['click'],
- })
- const Comp = defineVaporCustomElement(Comp1Vapor)
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<number | undefined>(instance.a)
- instance.a = 42
- })
- test('with required props', () => {
- const Comp1Vapor = defineVaporComponent({
- props: {
- a: { type: Number, required: true },
- },
- })
- const Comp = defineVaporCustomElement(Comp1Vapor)
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<number>(instance.a)
- instance.a = 42
- })
- test('with default props', () => {
- const Comp1Vapor = defineVaporComponent({
- props: {
- a: {
- type: Number,
- default: 1,
- validator: () => true,
- },
- },
- emits: ['click'],
- })
- const Comp = defineVaporCustomElement(Comp1Vapor)
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<number>(instance.a)
- instance.a = 42
- })
- test('with extra options', () => {
- const Comp1Vapor = defineVaporComponent({
- props: {
- a: {
- type: Number,
- default: 1,
- validator: () => true,
- },
- },
- emits: ['click'],
- })
- const Comp = defineVaporCustomElement(Comp1Vapor, {
- shadowRoot: false,
- styles: [`div { color: red; }`],
- nonce: 'xxx',
- shadowRootOptions: {
- delegatesFocus: false,
- },
- configureApp: app => {
- app.provide('a', 1)
- },
- })
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<number>(instance.a)
- instance.a = 42
- })
- })
- describe('defineVaporCustomElement with direct setup function', () => {
- test('basic setup function', () => {
- const Comp = defineVaporCustomElement((props: { msg: string }) => {
- expectType<string>(props.msg)
- return []
- })
- expectType<VaporElementConstructor<{ msg: string }>>(Comp)
- const instance = new Comp()
- expectType<string>(instance.msg)
- })
- test('setup function with emits', () => {
- const Comp = defineVaporCustomElement(
- (props: { msg: string }, ctx) => {
- ctx.emit('foo')
- return []
- },
- {
- emits: ['foo'],
- },
- )
- expectType<VaporElementConstructor<{ msg: string }>>(Comp)
- const instance = new Comp()
- expectType<string>(instance.msg)
- })
- test('setup function with extra options', () => {
- const Comp = defineVaporCustomElement(
- (props: { msg: string }, ctx) => {
- ctx.emit('foo')
- return []
- },
- {
- name: 'Foo',
- emits: ['foo'],
- inheritAttrs: false,
- shadowRoot: false,
- styles: [`div { color: red; }`],
- nonce: 'xxx',
- shadowRootOptions: {
- delegatesFocus: false,
- },
- configureApp: app => {
- app.provide('a', 1)
- },
- },
- )
- expectType<VaporElementConstructor<{ msg: string }>>(Comp)
- const instance = new Comp()
- expectType<string>(instance.msg)
- })
- })
- describe('defineVaporCustomElement with options object', () => {
- test('with object props', () => {
- const Comp = defineVaporCustomElement({
- props: {
- foo: String,
- bar: {
- type: Number,
- required: true,
- },
- },
- setup(props) {
- expectType<string | undefined>(props.foo)
- expectType<number>(props.bar)
- },
- })
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<string | undefined>(instance.foo)
- expectType<number>(instance.bar)
- })
- test('with array props', () => {
- const Comp = defineVaporCustomElement({
- props: ['foo', 'bar'],
- setup(props) {
- expectType<any>(props.foo)
- expectType<any>(props.bar)
- },
- })
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<any>(instance.foo)
- expectType<any>(instance.bar)
- })
- test('with emits', () => {
- const Comp = defineVaporCustomElement({
- props: {
- value: String,
- },
- emits: {
- change: (val: string) => true,
- },
- setup(props, { emit }) {
- emit('change', 'test')
- // @ts-expect-error
- emit('change', 123)
- // @ts-expect-error
- emit('unknown')
- },
- })
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<string | undefined>(instance.value)
- })
- test('with extra options', () => {
- const Comp = defineVaporCustomElement(
- {
- props: {
- value: String,
- },
- emits: {
- change: (val: string) => true,
- },
- setup(props, { emit }) {
- emit('change', 'test')
- // @ts-expect-error
- emit('change', 123)
- // @ts-expect-error
- emit('unknown')
- },
- },
- {
- shadowRoot: false,
- configureApp: app => {
- app.provide('a', 1)
- },
- },
- )
- expectType<VaporElementConstructor>(Comp)
- const instance = new Comp()
- expectType<string | undefined>(instance.value)
- })
- })
|