shadcn-vue/packages/cli/test/utils/transform-sfc.test.ts
zernonia f8f3fc754f
feat: support tailwind prefix (#619)
* chore: enable tw prefix

* chore: enable tw  prefix during init

* fix: cater for cn function

* fix: prevent transforming importDeclaration

* chore: update registry to make sure tailwind prefix parse correctly

* chore: fix wrong import

* chore: checkpoint

* refactor: goodbye ts-morph

* chore: remove ts-morpg

* chore: update test

* chore: cleanup

* chore: fix test

* fix: move vue-metamorph to dep

* refactor: transform tw prefix by specific case

* fix: transform-sfc not parsing .ts file

* fix: prefix double quote

* chore: patch vue-eslint-parser

* refactor: transform to cater only for class in sfc

* refactor: replace detypes with @unovue/detypes

* chore: update test snapshot

* chore: update pnpm-lock, fix import

* chore: bump detypes version

* chore: update deps
2024-10-14 19:39:58 +08:00

147 lines
4.1 KiB
TypeScript

import { resolve } from 'pathe'
import { describe, expect, it } from 'vitest'
import { transform } from '../../src/utils/transformers'
describe('transformSFC', () => {
it('basic', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const array: (number | string)[] = [1, 2, 3]
</script>
<template>
<div v-bind="{ array }">
template
</div>
</template>
<style scoped>
</style>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('remove all type reference', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const array: (number | string)[] = [1, 2, 3]
</script>
<template>
<div v-bind="{ array }" :prop="(a: number) => a" :prop2="(a: number) => {
let b: number = a
return b
}">
{{ true ? 123 as number : 0 }}
</div>
</template>
<style scoped>
</style>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('remove all type reference, keeping similar template', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const array: (number | string)[] = [1, 2, 3]
</script>
<template>
<div :class="cn(
'relative z-50 max-h-96 min-w-32 overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
position === 'popper'
&& 'data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1',
props.class,
)
">
{{ true ? 123 as number : 0 }}
</div>
</template>
<style scoped>
</style>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('defineProps', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const props = defineProps<{ foo: string }>()
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('defineProps with withDefaults', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const props = withDefaults(defineProps<{ foo: string }>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('defineProps with external props', async () => {
const result = await transform({
filename: resolve(__dirname, './test.vue'),
raw: `<script lang="ts" setup>
import { type Props } from './__fixtures__/props'
const props = withDefaults(defineProps<{ foo?: string } & Props>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
it('defineProps with package props', async () => {
const result = await transform({
filename: resolve(__dirname, './test.vue'),
raw: `<script lang="ts" setup>
import { type LabelProps } from 'radix-vue'
const props = withDefaults(defineProps<{ foo?: string } & LabelProps>(), {
foo: 'bar'
})
</script>
`,
config: {},
})
// TODO: We need to improve this. https://github.com/radix-vue/shadcn-vue/issues/187
expect(result).toMatchSnapshot()
})
it('defineEmits', async () => {
const result = await transform({
filename: 'app.vue',
raw: `<script lang="ts" setup>
const emit = defineEmits<{ foo: string }>()
</script>
`,
config: {},
})
expect(result).toMatchSnapshot()
})
})