11import { describe , expect , it } from 'vitest'
22import { transformStyledSyntax } from '../src/ts-transformer'
3- import { normalizeString } from './normalize'
3+ import { extractPropsFromCode } from './normalize'
44
55describe ( '复杂泛型语法转换' , ( ) => {
66 it ( '应该转换嵌套泛型' , ( ) => {
@@ -24,7 +24,15 @@ describe('复杂泛型语法转换', () => {
2424 const result = transformStyledSyntax ( code , 'test.tsx' )
2525
2626 expect ( result ) . not . toBeNull ( )
27- expect ( normalizeString ( result ?. code ) ) . toContain ( normalizeString ( `styled('input', { value: { type: String, required: false }, onChange: { type: Function, required: false }, placeholder: { type: String, required: false } })` ) )
27+
28+ const props = extractPropsFromCode ( result ?. props ?. [ 0 ] , 'Input' )
29+ expect ( props ) . toHaveProperty ( 'value' )
30+ expect ( props ) . toHaveProperty ( 'onChange' )
31+ expect ( props ) . toHaveProperty ( 'placeholder' )
32+ expect ( props . value . type ) . toBe ( 'String' )
33+ expect ( props . value . required ) . toBe ( false )
34+ expect ( props . onChange . type ) . toBe ( 'Function' )
35+ expect ( props . placeholder . type ) . toBe ( 'String' )
2836 } )
2937
3038 it ( '应该转换多行泛型定义' , ( ) => {
@@ -44,11 +52,14 @@ describe('复杂泛型语法转换', () => {
4452
4553 expect ( result ) . not . toBeNull ( )
4654
47- expect ( normalizeString ( result ?. code ) ) . toContain ( normalizeString ( `styled('button', {
48- primary: { type: Boolean, required: false },
49- size: { type: String, required: false },
50- disabled: { type: Boolean, required: false }
51- })` ) )
55+ const props = extractPropsFromCode ( result ?. props ?. [ 0 ] , 'Button' )
56+ expect ( props ) . toHaveProperty ( 'primary' )
57+ expect ( props ) . toHaveProperty ( 'size' )
58+ expect ( props ) . toHaveProperty ( 'disabled' )
59+ expect ( props . primary . type ) . toBe ( 'Boolean' )
60+ expect ( props . primary . required ) . toBe ( false )
61+ expect ( props . size . type ) . toBe ( 'String' )
62+ expect ( props . disabled . type ) . toBe ( 'Boolean' )
5263 } )
5364
5465 it ( '应该转换带条件类型的复杂泛型' , ( ) => {
@@ -74,16 +85,12 @@ describe('复杂泛型语法转换', () => {
7485 const result = transformStyledSyntax ( code , 'test.tsx' )
7586
7687 expect ( result ) . not . toBeNull ( )
77- expect ( normalizeString ( result ?. code ) ) . toContain ( normalizeString ( `styled('button', {
78- variant: {
79- type: String,
80- required: false
81- },
82- style: {
83- type: String,
84- required: false
85- }
86- })` ) )
88+
89+ const props = extractPropsFromCode ( result ?. props ?. [ 0 ] , 'Button' )
90+ expect ( props ) . toHaveProperty ( 'variant' )
91+ expect ( props ) . toHaveProperty ( 'style' )
92+ expect ( props . variant . required ) . toBe ( false )
93+ expect ( props . style . required ) . toBe ( false )
8794 } )
8895
8996 it ( '应该转换组合多个泛型和接口的类型' , ( ) => {
@@ -113,25 +120,14 @@ describe('复杂泛型语法转换', () => {
113120 const result = transformStyledSyntax ( code , 'test.tsx' )
114121
115122 expect ( result ) . not . toBeNull ( )
116- expect ( normalizeString ( result ?. code ) ) . toContain ( normalizeString ( `styled('button',
117- {
118- fullWidth: {
119- type: Boolean,
120- required: false
121- },
122- outlined: {
123- type: Boolean,
124- required: false
125- },
126- theme: {
127- type: String,
128- required: false
129- },
130- size: {
131- type: String,
132- required: false
133- }
134- }
135- )` ) )
123+
124+ const props = extractPropsFromCode ( result ?. props ?. [ 0 ] , 'ComplexButton' )
125+ expect ( props ) . toHaveProperty ( 'theme' )
126+ expect ( props ) . toHaveProperty ( 'size' )
127+ expect ( props ) . toHaveProperty ( 'fullWidth' )
128+ expect ( props ) . toHaveProperty ( 'outlined' )
129+ expect ( props . theme . type ) . toBe ( 'String' )
130+ expect ( props . fullWidth . type ) . toBe ( 'Boolean' )
131+ expect ( props . fullWidth . required ) . toBe ( false )
136132 } )
137133} )
0 commit comments