|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { transformStyledSyntax } from '../src/ts-transformer' |
| 3 | +import { extractPropsFromCode } from './normalize' |
| 4 | + |
| 5 | +describe('基本语法转换', () => { |
| 6 | + it('应该转换简单的 styled.tag<Props> 为 styled("tag", { primary: { type: Boolean, required: false } })', () => { |
| 7 | + const code = ` |
| 8 | + import styled from '@vue-styled-components/core' |
| 9 | + |
| 10 | + interface ButtonProps { |
| 11 | + primary?: boolean |
| 12 | + } |
| 13 | + |
| 14 | + const Button = styled.button<ButtonProps>\` |
| 15 | + background-color: \${props => props.primary ? 'blue' : 'white'}; |
| 16 | + \` |
| 17 | + ` |
| 18 | + |
| 19 | + const result = transformStyledSyntax(code, 'test.tsx') |
| 20 | + |
| 21 | + expect(result).not.toBeNull() |
| 22 | + |
| 23 | + const props = extractPropsFromCode(result?.props?.[0], 'Button') |
| 24 | + expect(props).toHaveProperty('primary') |
| 25 | + expect(props.primary.type).toBe('Boolean') |
| 26 | + expect(props.primary.required).toBe(false) |
| 27 | + }) |
| 28 | + |
| 29 | + it('应该转换行内泛型定义', () => { |
| 30 | + const code = ` |
| 31 | + import styled from '@vue-styled-components/core' |
| 32 | + |
| 33 | + const Link = styled.a<{ active: boolean; disabled?: boolean }>\` |
| 34 | + color: \${props => props.active ? 'red' : 'blue'}; |
| 35 | + opacity: \${props => props.disabled ? 0.5 : 1}; |
| 36 | + \` |
| 37 | + ` |
| 38 | + |
| 39 | + const result = transformStyledSyntax(code, 'test.tsx') |
| 40 | + |
| 41 | + expect(result).not.toBeNull() |
| 42 | + |
| 43 | + const props = extractPropsFromCode(result?.props?.[0], 'Link') |
| 44 | + expect(props).toHaveProperty('active') |
| 45 | + expect(props).toHaveProperty('disabled') |
| 46 | + expect(props.active.type).toBe('Boolean') |
| 47 | + expect(props.active.required).toBe(true) |
| 48 | + expect(props.disabled.type).toBe('Boolean') |
| 49 | + expect(props.disabled.required).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + it('不应该转换没有泛型的样式组件', () => { |
| 53 | + const code = ` |
| 54 | + import styled from '@vue-styled-components/core' |
| 55 | + |
| 56 | + const Button = styled.button\` |
| 57 | + background-color: blue; |
| 58 | + \` |
| 59 | + ` |
| 60 | + |
| 61 | + const result = transformStyledSyntax(code, 'test.tsx') |
| 62 | + |
| 63 | + expect(result).toBeNull() |
| 64 | + }) |
| 65 | + |
| 66 | + it('不应该转换原始的函数调用语法', () => { |
| 67 | + const code = ` |
| 68 | + import styled from '@vue-styled-components/core' |
| 69 | + |
| 70 | + const Button = styled('button', { |
| 71 | + primary: Boolean, |
| 72 | + })\` |
| 73 | + background-color: \${props => props.primary ? 'blue' : 'white'}; |
| 74 | + \` |
| 75 | + ` |
| 76 | + |
| 77 | + const result = transformStyledSyntax(code, 'test.tsx') |
| 78 | + |
| 79 | + expect(result).toBeNull() |
| 80 | + }) |
| 81 | + |
| 82 | + it('应该处理同一文件中的多个样式组件', () => { |
| 83 | + const code = ` |
| 84 | + import styled from '@vue-styled-components/core' |
| 85 | + |
| 86 | + interface ButtonProps { |
| 87 | + primary?: boolean |
| 88 | + } |
| 89 | + |
| 90 | + interface IconProps { |
| 91 | + size?: number |
| 92 | + } |
| 93 | + |
| 94 | + const Button = styled.button<ButtonProps>\` |
| 95 | + background-color: \${props => props.primary ? 'blue' : 'white'}; |
| 96 | + \` |
| 97 | + |
| 98 | + const Icon = styled.span<IconProps>\` |
| 99 | + font-size: \${props => \`\${props.size || 16}px\`}; |
| 100 | + \` |
| 101 | + ` |
| 102 | + |
| 103 | + const result = transformStyledSyntax(code, 'test.tsx') |
| 104 | + |
| 105 | + expect(result).not.toBeNull() |
| 106 | + |
| 107 | + // 检查第一个组件 |
| 108 | + const buttonProps = extractPropsFromCode(result?.props?.[0], 'Button') |
| 109 | + expect(buttonProps).toHaveProperty('primary') |
| 110 | + expect(buttonProps.primary.type).toBe('Boolean') |
| 111 | + expect(buttonProps.primary.required).toBe(false) |
| 112 | + |
| 113 | + // 检查第二个组件 |
| 114 | + const iconProps = extractPropsFromCode(result?.props?.[1], 'Icon') |
| 115 | + expect(iconProps).toHaveProperty('size') |
| 116 | + expect(iconProps.size.type).toBe('Number') |
| 117 | + expect(iconProps.size.required).toBe(false) |
| 118 | + }) |
| 119 | +}) |
0 commit comments