|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { |
| 3 | + DEVUP_COMPONENTS, |
| 4 | + extractCustomComponentImports, |
| 5 | + extractImports, |
| 6 | + generateImportStatements, |
| 7 | +} from '../exportPagesAndComponents' |
| 8 | + |
| 9 | +describe('DEVUP_COMPONENTS', () => { |
| 10 | + test('should contain expected devup-ui components', () => { |
| 11 | + expect(DEVUP_COMPONENTS).toContain('Box') |
| 12 | + expect(DEVUP_COMPONENTS).toContain('Flex') |
| 13 | + expect(DEVUP_COMPONENTS).toContain('Text') |
| 14 | + expect(DEVUP_COMPONENTS).toContain('Image') |
| 15 | + expect(DEVUP_COMPONENTS).toContain('Grid') |
| 16 | + expect(DEVUP_COMPONENTS).toContain('VStack') |
| 17 | + expect(DEVUP_COMPONENTS).toContain('Center') |
| 18 | + }) |
| 19 | +}) |
| 20 | + |
| 21 | +describe('extractImports', () => { |
| 22 | + test('should extract Box import', () => { |
| 23 | + const result = extractImports([['Test', '<Box>Hello</Box>']]) |
| 24 | + expect(result).toContain('Box') |
| 25 | + }) |
| 26 | + |
| 27 | + test('should extract multiple devup-ui components', () => { |
| 28 | + const result = extractImports([ |
| 29 | + ['Test', '<Box><Flex><Text>Hello</Text></Flex></Box>'], |
| 30 | + ]) |
| 31 | + expect(result).toContain('Box') |
| 32 | + expect(result).toContain('Flex') |
| 33 | + expect(result).toContain('Text') |
| 34 | + }) |
| 35 | + |
| 36 | + test('should extract keyframes with parenthesis', () => { |
| 37 | + const result = extractImports([ |
| 38 | + ['Test', '<Box animationName={keyframes({ "0%": { opacity: 0 } })} />'], |
| 39 | + ]) |
| 40 | + expect(result).toContain('keyframes') |
| 41 | + expect(result).toContain('Box') |
| 42 | + }) |
| 43 | + |
| 44 | + test('should extract keyframes with template literal', () => { |
| 45 | + const result = extractImports([ |
| 46 | + ['Test', '<Box animationName={keyframes`from { opacity: 0 }`} />'], |
| 47 | + ]) |
| 48 | + expect(result).toContain('keyframes') |
| 49 | + }) |
| 50 | + |
| 51 | + test('should not extract keyframes when not present', () => { |
| 52 | + const result = extractImports([['Test', '<Box w="100px" />']]) |
| 53 | + expect(result).not.toContain('keyframes') |
| 54 | + }) |
| 55 | + |
| 56 | + test('should return sorted imports', () => { |
| 57 | + const result = extractImports([ |
| 58 | + ['Test', '<VStack><Box><Center /></Box></VStack>'], |
| 59 | + ]) |
| 60 | + expect(result).toEqual(['Box', 'Center', 'VStack']) |
| 61 | + }) |
| 62 | + |
| 63 | + test('should not include duplicates', () => { |
| 64 | + const result = extractImports([ |
| 65 | + ['Test1', '<Box>A</Box>'], |
| 66 | + ['Test2', '<Box>B</Box>'], |
| 67 | + ]) |
| 68 | + expect(result.filter((x) => x === 'Box').length).toBe(1) |
| 69 | + }) |
| 70 | + |
| 71 | + test('should handle self-closing tags', () => { |
| 72 | + const result = extractImports([['Test', '<Image />']]) |
| 73 | + expect(result).toContain('Image') |
| 74 | + }) |
| 75 | + |
| 76 | + test('should handle tags with spaces', () => { |
| 77 | + const result = extractImports([['Test', '<Grid rows={2}>']]) |
| 78 | + expect(result).toContain('Grid') |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +describe('extractCustomComponentImports', () => { |
| 83 | + test('should extract custom component', () => { |
| 84 | + const result = extractCustomComponentImports([ |
| 85 | + ['Test', '<Box><CustomButton /></Box>'], |
| 86 | + ]) |
| 87 | + expect(result).toContain('CustomButton') |
| 88 | + }) |
| 89 | + |
| 90 | + test('should extract multiple custom components', () => { |
| 91 | + const result = extractCustomComponentImports([ |
| 92 | + ['Test', '<CustomA><CustomB /><CustomC /></CustomA>'], |
| 93 | + ]) |
| 94 | + expect(result).toContain('CustomA') |
| 95 | + expect(result).toContain('CustomB') |
| 96 | + expect(result).toContain('CustomC') |
| 97 | + }) |
| 98 | + |
| 99 | + test('should not include devup-ui components', () => { |
| 100 | + const result = extractCustomComponentImports([ |
| 101 | + ['Test', '<Box><Flex><CustomCard /></Flex></Box>'], |
| 102 | + ]) |
| 103 | + expect(result).toContain('CustomCard') |
| 104 | + expect(result).not.toContain('Box') |
| 105 | + expect(result).not.toContain('Flex') |
| 106 | + }) |
| 107 | + |
| 108 | + test('should return sorted imports', () => { |
| 109 | + const result = extractCustomComponentImports([ |
| 110 | + ['Test', '<Zebra /><Apple /><Mango />'], |
| 111 | + ]) |
| 112 | + expect(result).toEqual(['Apple', 'Mango', 'Zebra']) |
| 113 | + }) |
| 114 | + |
| 115 | + test('should not include duplicates', () => { |
| 116 | + const result = extractCustomComponentImports([ |
| 117 | + ['Test1', '<SharedButton />'], |
| 118 | + ['Test2', '<SharedButton />'], |
| 119 | + ]) |
| 120 | + expect(result.filter((x) => x === 'SharedButton').length).toBe(1) |
| 121 | + }) |
| 122 | + |
| 123 | + test('should return empty array when no custom components', () => { |
| 124 | + const result = extractCustomComponentImports([ |
| 125 | + ['Test', '<Box><Flex>Hello</Flex></Box>'], |
| 126 | + ]) |
| 127 | + expect(result).toEqual([]) |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +describe('generateImportStatements', () => { |
| 132 | + test('should generate devup-ui import statement', () => { |
| 133 | + const result = generateImportStatements([['Test', '<Box><Flex /></Box>']]) |
| 134 | + expect(result).toContain("import { Box, Flex } from '@devup-ui/react'") |
| 135 | + }) |
| 136 | + |
| 137 | + test('should generate custom component import statements', () => { |
| 138 | + const result = generateImportStatements([ |
| 139 | + ['Test', '<Box><CustomButton /></Box>'], |
| 140 | + ]) |
| 141 | + expect(result).toContain("import { Box } from '@devup-ui/react'") |
| 142 | + expect(result).toContain( |
| 143 | + "import { CustomButton } from '@/components/CustomButton'", |
| 144 | + ) |
| 145 | + }) |
| 146 | + |
| 147 | + test('should generate multiple custom component imports on separate lines', () => { |
| 148 | + const result = generateImportStatements([ |
| 149 | + ['Test', '<Box><ButtonA /><ButtonB /></Box>'], |
| 150 | + ]) |
| 151 | + expect(result).toContain("import { ButtonA } from '@/components/ButtonA'") |
| 152 | + expect(result).toContain("import { ButtonB } from '@/components/ButtonB'") |
| 153 | + }) |
| 154 | + |
| 155 | + test('should return empty string when no imports', () => { |
| 156 | + const result = generateImportStatements([['Test', 'just text']]) |
| 157 | + expect(result).toBe('') |
| 158 | + }) |
| 159 | + |
| 160 | + test('should include keyframes in devup-ui import', () => { |
| 161 | + const result = generateImportStatements([ |
| 162 | + ['Test', '<Box animation={keyframes({})} />'], |
| 163 | + ]) |
| 164 | + expect(result).toContain('keyframes') |
| 165 | + expect(result).toContain("from '@devup-ui/react'") |
| 166 | + }) |
| 167 | + |
| 168 | + test('should end with double newline when has imports', () => { |
| 169 | + const result = generateImportStatements([['Test', '<Box />']]) |
| 170 | + expect(result.endsWith('\n\n')).toBe(true) |
| 171 | + }) |
| 172 | +}) |
0 commit comments