|
| 1 | +import { globSync, writeFileSync } from 'node:fs' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { existsSync } from 'node:fs' |
| 4 | +import { join } from 'node:path' |
| 5 | + |
| 6 | +import { codeExtract, registerTheme } from '@devup-ui/wasm' |
| 7 | + |
| 8 | +import { findRoot } from '../find-root' |
| 9 | +import { preload } from '../preload' |
| 10 | + |
| 11 | +// Mock dependencies |
| 12 | +vi.mock('node:fs') |
| 13 | +vi.mock('@devup-ui/wasm') |
| 14 | +vi.mock('../find-root') |
| 15 | + |
| 16 | +// Mock globSync |
| 17 | +vi.mock('node:fs', () => ({ |
| 18 | + globSync: vi.fn(), |
| 19 | + readFileSync: vi.fn(), |
| 20 | + writeFileSync: vi.fn(), |
| 21 | + mkdirSync: vi.fn(), |
| 22 | + existsSync: vi.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +// Mock @devup-ui/wasm |
| 26 | +vi.mock('@devup-ui/wasm', () => ({ |
| 27 | + codeExtract: vi.fn(), |
| 28 | + registerTheme: vi.fn(), |
| 29 | +})) |
| 30 | + |
| 31 | +// Mock findRoot |
| 32 | +vi.mock('../find-root', () => ({ |
| 33 | + findRoot: vi.fn(), |
| 34 | +})) |
| 35 | + |
| 36 | +describe('preload', () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks() |
| 39 | + |
| 40 | + // Default mock implementations |
| 41 | + vi.mocked(findRoot).mockReturnValue('/project/root') |
| 42 | + vi.mocked(globSync).mockReturnValue([ |
| 43 | + 'src/App.tsx', |
| 44 | + 'src/components/Button.tsx', |
| 45 | + ]) |
| 46 | + vi.mocked(readFileSync).mockReturnValue( |
| 47 | + 'const Button = () => <div>Hello</div>', |
| 48 | + ) |
| 49 | + vi.mocked(codeExtract).mockReturnValue({ |
| 50 | + free: vi.fn(), |
| 51 | + cssFile: 'styles.css', |
| 52 | + css: '.button { color: red; }', |
| 53 | + code: '', |
| 54 | + map: '', |
| 55 | + updatedBaseStyle: false, |
| 56 | + [Symbol.dispose]: vi.fn(), |
| 57 | + }) |
| 58 | + vi.mocked(existsSync).mockReturnValue(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should find project root and collect files', () => { |
| 62 | + const excludeRegex = /node_modules/ |
| 63 | + const libPackage = '@devup-ui/react' |
| 64 | + const singleCss = false |
| 65 | + const theme = { colors: { primary: 'blue' } } |
| 66 | + const cssDir = '/output/css' |
| 67 | + |
| 68 | + preload(excludeRegex, libPackage, singleCss, theme, cssDir) |
| 69 | + |
| 70 | + expect(findRoot).toHaveBeenCalledWith(process.cwd()) |
| 71 | + expect(globSync).toHaveBeenCalledWith( |
| 72 | + ['**/*.tsx', '**/*.ts', '**/*.js', '**/*.mjs'], |
| 73 | + { |
| 74 | + cwd: '/project/root', |
| 75 | + exclude: expect.any(Function), |
| 76 | + }, |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should register theme before processing files', () => { |
| 81 | + const theme = { colors: { primary: 'blue' } } |
| 82 | + |
| 83 | + preload(/node_modules/, '@devup-ui/react', false, theme, '/output/css') |
| 84 | + |
| 85 | + expect(registerTheme).toHaveBeenCalledWith(theme) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should process each collected file', () => { |
| 89 | + const files = ['src/App.tsx', 'src/components/Button.tsx'] |
| 90 | + vi.mocked(globSync).mockReturnValue(files) |
| 91 | + |
| 92 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 93 | + |
| 94 | + expect(codeExtract).toHaveBeenCalledTimes(2) |
| 95 | + expect(codeExtract).toHaveBeenCalledWith( |
| 96 | + expect.stringMatching(/App\.tsx$/), |
| 97 | + 'const Button = () => <div>Hello</div>', |
| 98 | + '@devup-ui/react', |
| 99 | + '/output/css', |
| 100 | + false, |
| 101 | + false, |
| 102 | + true, |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should write CSS file when cssFile is returned', () => { |
| 107 | + vi.mocked(codeExtract).mockReturnValue({ |
| 108 | + cssFile: 'styles.css', |
| 109 | + css: '.button { color: red; }', |
| 110 | + free: vi.fn(), |
| 111 | + code: '', |
| 112 | + map: '', |
| 113 | + updatedBaseStyle: false, |
| 114 | + [Symbol.dispose]: vi.fn(), |
| 115 | + }) |
| 116 | + |
| 117 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 118 | + |
| 119 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 120 | + join('/output/css', 'styles.css'), |
| 121 | + '.button { color: red; }', |
| 122 | + 'utf-8', |
| 123 | + ) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should not write CSS file when cssFile is null', () => { |
| 127 | + vi.mocked(codeExtract).mockReturnValue({ |
| 128 | + cssFile: undefined, |
| 129 | + css: '.button { color: red; }', |
| 130 | + free: vi.fn(), |
| 131 | + code: '', |
| 132 | + map: '', |
| 133 | + updatedBaseStyle: false, |
| 134 | + [Symbol.dispose]: vi.fn(), |
| 135 | + }) |
| 136 | + |
| 137 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 138 | + |
| 139 | + expect(writeFileSync).not.toHaveBeenCalled() |
| 140 | + }) |
| 141 | + |
| 142 | + it('should handle empty CSS content', () => { |
| 143 | + vi.mocked(codeExtract).mockReturnValue({ |
| 144 | + cssFile: 'styles.css', |
| 145 | + css: '', |
| 146 | + free: vi.fn(), |
| 147 | + code: '', |
| 148 | + map: '', |
| 149 | + updatedBaseStyle: false, |
| 150 | + [Symbol.dispose]: vi.fn(), |
| 151 | + }) |
| 152 | + |
| 153 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 154 | + |
| 155 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 156 | + join('/output/css', 'styles.css'), |
| 157 | + '', |
| 158 | + 'utf-8', |
| 159 | + ) |
| 160 | + }) |
| 161 | + |
| 162 | + it('should handle undefined CSS content', () => { |
| 163 | + vi.mocked(codeExtract).mockReturnValue({ |
| 164 | + cssFile: 'styles.css', |
| 165 | + css: undefined, |
| 166 | + free: vi.fn(), |
| 167 | + code: '', |
| 168 | + map: '', |
| 169 | + updatedBaseStyle: false, |
| 170 | + [Symbol.dispose]: vi.fn(), |
| 171 | + }) |
| 172 | + |
| 173 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 174 | + |
| 175 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 176 | + join('/output/css', 'styles.css'), |
| 177 | + '', |
| 178 | + 'utf-8', |
| 179 | + ) |
| 180 | + }) |
| 181 | + |
| 182 | + it('should pass correct parameters to codeExtract', () => { |
| 183 | + const libPackage = '@devup-ui/react' |
| 184 | + const singleCss = true |
| 185 | + const cssDir = '/custom/css/dir' |
| 186 | + |
| 187 | + preload(/node_modules/, libPackage, singleCss, {}, cssDir) |
| 188 | + |
| 189 | + expect(codeExtract).toHaveBeenCalledWith( |
| 190 | + expect.stringMatching(/App\.tsx$/), |
| 191 | + 'const Button = () => <div>Hello</div>', |
| 192 | + libPackage, |
| 193 | + cssDir, |
| 194 | + singleCss, |
| 195 | + false, |
| 196 | + true, |
| 197 | + ) |
| 198 | + }) |
| 199 | + |
| 200 | + it('should handle multiple files with different CSS outputs', () => { |
| 201 | + const files = ['src/App.tsx', 'src/components/Button.tsx'] |
| 202 | + vi.mocked(globSync).mockReturnValue(files) |
| 203 | + |
| 204 | + vi.mocked(codeExtract) |
| 205 | + .mockReturnValueOnce({ |
| 206 | + cssFile: 'app.css', |
| 207 | + css: '.app { margin: 0; }', |
| 208 | + free: vi.fn(), |
| 209 | + code: '', |
| 210 | + map: '', |
| 211 | + updatedBaseStyle: false, |
| 212 | + [Symbol.dispose]: vi.fn(), |
| 213 | + }) |
| 214 | + .mockReturnValueOnce({ |
| 215 | + free: vi.fn(), |
| 216 | + cssFile: 'button.css', |
| 217 | + css: '.button { color: blue; }', |
| 218 | + code: '', |
| 219 | + map: '', |
| 220 | + updatedBaseStyle: false, |
| 221 | + [Symbol.dispose]: vi.fn(), |
| 222 | + }) |
| 223 | + |
| 224 | + preload(/node_modules/, '@devup-ui/react', false, {}, '/output/css') |
| 225 | + |
| 226 | + expect(writeFileSync).toHaveBeenCalledTimes(2) |
| 227 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 228 | + join('/output/css', 'app.css'), |
| 229 | + '.app { margin: 0; }', |
| 230 | + 'utf-8', |
| 231 | + ) |
| 232 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 233 | + join('/output/css', 'button.css'), |
| 234 | + '.button { color: blue; }', |
| 235 | + 'utf-8', |
| 236 | + ) |
| 237 | + }) |
| 238 | +}) |
0 commit comments