11import { existsSync } from 'node:fs'
22import { mkdir , readFile , writeFile } from 'node:fs/promises'
3- import { resolve } from 'node:path'
3+ import { join , resolve } from 'node:path'
44
5- import { codeExtract , getThemeInterface } from '@devup-ui/wasm'
5+ import {
6+ codeExtract ,
7+ getDefaultTheme ,
8+ getThemeInterface ,
9+ registerTheme ,
10+ } from '@devup-ui/wasm'
611import { vi } from 'vitest'
712
813import { DevupUI } from '../plugin'
@@ -34,8 +39,10 @@ describe('DevupUIRsbuildPlugin', () => {
3439 expect ( typeof plugin . setup ) . toBe ( 'function' )
3540
3641 const transform = vi . fn ( )
42+ const modifyRsbuildConfig = vi . fn ( )
3743 await plugin . setup ( {
3844 transform,
45+ modifyRsbuildConfig,
3946 } as any )
4047 expect ( transform ) . toHaveBeenCalled ( )
4148 } )
@@ -51,8 +58,10 @@ describe('DevupUIRsbuildPlugin', () => {
5158 expect ( plugin ) . toBeDefined ( )
5259 expect ( plugin . setup ) . toBeDefined ( )
5360 const transform = vi . fn ( )
61+ const modifyRsbuildConfig = vi . fn ( )
5462 await plugin . setup ( {
5563 transform,
64+ modifyRsbuildConfig,
5665 } as any )
5766 } )
5867
@@ -66,8 +75,10 @@ describe('DevupUIRsbuildPlugin', () => {
6675 expect ( plugin ) . toBeDefined ( )
6776 expect ( plugin . setup ) . toBeDefined ( )
6877 const transform = vi . fn ( )
78+ const modifyRsbuildConfig = vi . fn ( )
6979 await plugin . setup ( {
7080 transform,
81+ modifyRsbuildConfig,
7182 } as any )
7283 } )
7384
@@ -104,8 +115,10 @@ describe('DevupUIRsbuildPlugin', () => {
104115 expect ( plugin ) . toBeDefined ( )
105116 expect ( plugin . setup ) . toBeDefined ( )
106117 const transform = vi . fn ( )
118+ const modifyRsbuildConfig = vi . fn ( )
107119 await plugin . setup ( {
108120 transform,
121+ modifyRsbuildConfig,
109122 } as any )
110123 expect ( transform ) . toHaveBeenCalled ( )
111124 expect ( transform ) . toHaveBeenCalledWith (
@@ -130,8 +143,10 @@ describe('DevupUIRsbuildPlugin', () => {
130143 expect ( plugin ) . toBeDefined ( )
131144 expect ( plugin . setup ) . toBeDefined ( )
132145 const transform = vi . fn ( )
146+ const modifyRsbuildConfig = vi . fn ( )
133147 await plugin . setup ( {
134148 transform,
149+ modifyRsbuildConfig,
135150 } as any )
136151 expect ( transform ) . toHaveBeenCalled ( )
137152 expect ( transform ) . toHaveBeenCalledWith (
@@ -182,6 +197,7 @@ const App = () => <Box></Box>`,
182197 const transform = vi . fn ( )
183198 await plugin . setup ( {
184199 transform,
200+ modifyRsbuildConfig : vi . fn ( ) ,
185201 } as any )
186202 expect ( transform ) . toHaveBeenCalled ( )
187203 expect ( transform ) . toHaveBeenCalledWith (
@@ -220,4 +236,105 @@ const App = () => <Box></Box>`,
220236 map : undefined ,
221237 } )
222238 } )
239+ it . each (
240+ createTestMatrix ( {
241+ watch : [ true , false ] ,
242+ existsDevupFile : [ true , false ] ,
243+ existsDistDir : [ true , false ] ,
244+ existsSheetFile : [ true , false ] ,
245+ existsClassMapFile : [ true , false ] ,
246+ existsFileMapFile : [ true , false ] ,
247+ existsCssDir : [ true , false ] ,
248+ getDefaultTheme : [ 'theme' , '' ] ,
249+ singleCss : [ true , false ] ,
250+ } ) ,
251+ ) ( 'should write data files' , async ( options ) => {
252+ vi . mocked ( writeFile ) . mockResolvedValueOnce ( undefined )
253+ vi . mocked ( readFile ) . mockResolvedValueOnce ( JSON . stringify ( { } ) )
254+ vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
255+ vi . mocked ( getDefaultTheme ) . mockReturnValue ( options . getDefaultTheme )
256+ vi . mocked ( existsSync ) . mockImplementation ( ( path ) => {
257+ if ( path === 'devup.json' ) return options . existsDevupFile
258+ if ( path === 'df' ) return options . existsDistDir
259+ if ( path === resolve ( 'df' , 'devup-ui' ) ) return options . existsCssDir
260+ if ( path === join ( 'df' , 'sheet.json' ) ) return options . existsSheetFile
261+ if ( path === join ( 'df' , 'classMap.json' ) )
262+ return options . existsClassMapFile
263+ if ( path === join ( 'df' , 'fileMap.json' ) ) return options . existsFileMapFile
264+ return false
265+ } )
266+ const plugin = DevupUI ( { singleCss : options . singleCss } )
267+ await ( plugin as any ) . setup ( {
268+ transform : vi . fn ( ) ,
269+ renderChunk : vi . fn ( ) ,
270+ generateBundle : vi . fn ( ) ,
271+ closeBundle : vi . fn ( ) ,
272+ resolve : vi . fn ( ) ,
273+ load : vi . fn ( ) ,
274+ modifyRsbuildConfig : vi . fn ( ) ,
275+ watchChange : vi . fn ( ) ,
276+ resolveId : vi . fn ( ) ,
277+ } as any )
278+ if ( options . existsDevupFile ) {
279+ expect ( readFile ) . toHaveBeenCalledWith ( 'devup.json' , 'utf-8' )
280+ expect ( registerTheme ) . toHaveBeenCalledWith ( { } )
281+ expect ( getThemeInterface ) . toHaveBeenCalledWith (
282+ '@devup-ui/react' ,
283+ 'DevupThemeColors' ,
284+ 'DevupThemeTypography' ,
285+ 'DevupTheme' ,
286+ )
287+ expect ( writeFile ) . toHaveBeenCalledWith (
288+ join ( 'df' , 'theme.d.ts' ) ,
289+ 'interface code' ,
290+ 'utf-8' ,
291+ )
292+ } else {
293+ expect ( registerTheme ) . toHaveBeenCalledWith ( { } )
294+ }
295+
296+ const modifyRsbuildConfig = vi . fn ( )
297+ await ( plugin as any ) . setup ( {
298+ transform : vi . fn ( ) ,
299+ renderChunk : vi . fn ( ) ,
300+ generateBundle : vi . fn ( ) ,
301+ closeBundle : vi . fn ( ) ,
302+ resolve : vi . fn ( ) ,
303+ modifyRsbuildConfig,
304+ load : vi . fn ( ) ,
305+ watchChange : vi . fn ( ) ,
306+ resolveId : vi . fn ( ) ,
307+ } as any )
308+ if ( options . getDefaultTheme ) {
309+ expect ( modifyRsbuildConfig ) . toHaveBeenCalledWith ( expect . any ( Function ) )
310+ const config = {
311+ source : {
312+ define : { } ,
313+ } ,
314+ }
315+ modifyRsbuildConfig . mock . calls [ 0 ] [ 0 ] ( config )
316+ expect ( config ) . toEqual ( {
317+ source : {
318+ define : {
319+ 'process.env.DEVUP_UI_DEFAULT_THEME' : JSON . stringify (
320+ options . getDefaultTheme ,
321+ ) ,
322+ } ,
323+ } ,
324+ } )
325+ } else {
326+ expect ( modifyRsbuildConfig ) . toHaveBeenCalledWith ( expect . any ( Function ) )
327+ const config = {
328+ source : {
329+ define : { } ,
330+ } ,
331+ }
332+ modifyRsbuildConfig . mock . calls [ 0 ] [ 0 ] ( config )
333+ expect ( config ) . toEqual ( {
334+ source : {
335+ define : { } ,
336+ } ,
337+ } )
338+ }
339+ } )
223340} )
0 commit comments