1- import { DevupUIRsbuildPlugin } from "../plugin"
2- import { vi } from 'vitest'
31import { mkdir , writeFile } from 'node:fs/promises'
42import { resolve } from 'node:path'
5- import { codeExtract } from "@devup-ui/wasm"
6- import { write } from "node:fs"
3+
4+ import { codeExtract } from '@devup-ui/wasm'
5+ import { vi } from 'vitest'
6+
7+ import { DevupUIRsbuildPlugin } from '../plugin'
78
89// Mock dependencies
910vi . mock ( 'node:fs/promises' )
1011vi . mock ( '@devup-ui/wasm' , ( ) => ( {
11- codeExtract : vi . fn ( ) . mockReturnValue ( {
12- code : '' ,
13- css : ''
14- } )
12+ codeExtract : vi . fn ( ) . mockReturnValue ( {
13+ code : '' ,
14+ css : '' ,
15+ } ) ,
1516} ) )
1617
1718describe ( 'DevupUIRsbuildPlugin' , ( ) => {
18- beforeEach ( ( ) => {
19- vi . resetAllMocks ( )
20- vi . mocked ( mkdir ) . mockResolvedValue ( undefined )
21- vi . mocked ( writeFile ) . mockResolvedValue ( undefined )
22- } )
19+ beforeEach ( ( ) => {
20+ vi . resetAllMocks ( )
21+ vi . mocked ( mkdir ) . mockResolvedValue ( undefined )
22+ vi . mocked ( writeFile ) . mockResolvedValue ( undefined )
23+ } )
2324
24- it ( 'should export DevupUIRsbuildPlugin' , ( ) => {
25- expect ( DevupUIRsbuildPlugin ) . toBeDefined ( )
26- } )
25+ it ( 'should export DevupUIRsbuildPlugin' , ( ) => {
26+ expect ( DevupUIRsbuildPlugin ) . toBeDefined ( )
27+ } )
2728
28- it ( 'should be a function' , ( ) => {
29- expect ( DevupUIRsbuildPlugin ) . toBeInstanceOf ( Function )
30- } )
29+ it ( 'should be a function' , ( ) => {
30+ expect ( DevupUIRsbuildPlugin ) . toBeInstanceOf ( Function )
31+ } )
3132
32- it ( 'should return a plugin object with correct name' , async ( ) => {
33- const plugin = DevupUIRsbuildPlugin ( )
34- expect ( plugin ) . toBeDefined ( )
35- expect ( plugin . name ) . toBe ( 'devup-ui-rsbuild-plugin' )
36- expect ( typeof plugin . setup ) . toBe ( 'function' )
33+ it ( 'should return a plugin object with correct name' , async ( ) => {
34+ const plugin = DevupUIRsbuildPlugin ( )
35+ expect ( plugin ) . toBeDefined ( )
36+ expect ( plugin . name ) . toBe ( 'devup-ui-rsbuild-plugin' )
37+ expect ( typeof plugin . setup ) . toBe ( 'function' )
3738
38- const transform = vi . fn ( )
39- await plugin . setup ( {
40- transform,
41- } as any )
42- expect ( transform ) . toHaveBeenCalled ( )
43- } )
39+ const transform = vi . fn ( )
40+ await plugin . setup ( {
41+ transform,
42+ } as any )
43+ expect ( transform ) . toHaveBeenCalled ( )
44+ } )
4445
45- it ( 'should not register css transform' , async ( ) => {
46- const plugin = DevupUIRsbuildPlugin ( {
47- extractCss : false
48- } )
49- expect ( plugin ) . toBeDefined ( )
50- expect ( plugin . setup ) . toBeDefined ( )
51- const transform = vi . fn ( )
52- await plugin . setup ( {
53- transform,
54- } as any )
55- expect ( transform ) . not . toHaveBeenCalled ( )
46+ it ( 'should not register css transform' , async ( ) => {
47+ const plugin = DevupUIRsbuildPlugin ( {
48+ extractCss : false ,
5649 } )
50+ expect ( plugin ) . toBeDefined ( )
51+ expect ( plugin . setup ) . toBeDefined ( )
52+ const transform = vi . fn ( )
53+ await plugin . setup ( {
54+ transform,
55+ } as any )
56+ expect ( transform ) . not . toHaveBeenCalled ( )
57+ } )
5758
58- it ( 'should accept custom options' , ( ) => {
59- const customOptions = {
60- package : '@custom/devup-ui' ,
61- cssFile : './custom.css' ,
62- devupPath : './custom-df' ,
63- interfacePath : './custom-interface' ,
64- extractCss : false ,
65- debug : true ,
66- include : [ 'src/**/*' ]
67- }
59+ it ( 'should accept custom options' , ( ) => {
60+ const customOptions = {
61+ package : '@custom/devup-ui' ,
62+ cssFile : './custom.css' ,
63+ devupPath : './custom-df' ,
64+ interfacePath : './custom-interface' ,
65+ extractCss : false ,
66+ debug : true ,
67+ include : [ 'src/**/*' ] ,
68+ }
6869
69- const plugin = DevupUIRsbuildPlugin ( customOptions )
70- expect ( plugin ) . toBeDefined ( )
71- expect ( plugin . name ) . toBe ( 'devup-ui-rsbuild-plugin' )
72- } )
73- it ( 'should transform css' , async ( ) => {
74- const plugin = DevupUIRsbuildPlugin ( )
75- expect ( plugin ) . toBeDefined ( )
76- expect ( plugin . setup ) . toBeDefined ( )
77- const transform = vi . fn ( )
78- await plugin . setup ( {
79- transform,
80- } as any )
81- expect ( transform ) . toHaveBeenCalled ( )
82- expect ( transform ) . toHaveBeenCalledWith ( {
83- test : resolve ( '.df' , 'devup-ui.css' ) ,
84- } , expect . any ( Function ) )
70+ const plugin = DevupUIRsbuildPlugin ( customOptions )
71+ expect ( plugin ) . toBeDefined ( )
72+ expect ( plugin . name ) . toBe ( 'devup-ui-rsbuild-plugin' )
73+ } )
74+ it ( 'should transform css' , async ( ) => {
75+ const plugin = DevupUIRsbuildPlugin ( )
76+ expect ( plugin ) . toBeDefined ( )
77+ expect ( plugin . setup ) . toBeDefined ( )
78+ const transform = vi . fn ( )
79+ await plugin . setup ( {
80+ transform,
81+ } as any )
82+ expect ( transform ) . toHaveBeenCalled ( )
83+ expect ( transform ) . toHaveBeenCalledWith (
84+ {
85+ test : resolve ( '.df' , 'devup-ui.css' ) ,
86+ } ,
87+ expect . any ( Function ) ,
88+ )
8589
86- expect ( transform . mock . calls [ 0 ] [ 1 ] ( {
87- code : `
90+ expect (
91+ transform . mock . calls [ 0 ] [ 1 ] ( {
92+ code : `
8893 .devup-ui-1 {
8994 color: red;
9095 }
9196 ` ,
92- } ) ) . toBe ( '' )
93- } )
94- it ( 'should transform code' , async ( ) => {
95- const plugin = DevupUIRsbuildPlugin ( )
96- expect ( plugin ) . toBeDefined ( )
97- expect ( plugin . setup ) . toBeDefined ( )
98- const transform = vi . fn ( )
99- await plugin . setup ( {
100- transform,
101- } as any )
102- expect ( transform ) . toHaveBeenCalled ( )
103- expect ( transform ) . toHaveBeenCalledWith ( {
104- test : / \. ( t s x | t s | j s | m j s | j s x ) $ / ,
105- } , expect . any ( Function ) )
97+ } ) ,
98+ ) . toBe ( '' )
99+ } )
100+ it ( 'should transform code' , async ( ) => {
101+ const plugin = DevupUIRsbuildPlugin ( )
102+ expect ( plugin ) . toBeDefined ( )
103+ expect ( plugin . setup ) . toBeDefined ( )
104+ const transform = vi . fn ( )
105+ await plugin . setup ( {
106+ transform,
107+ } as any )
108+ expect ( transform ) . toHaveBeenCalled ( )
109+ expect ( transform ) . toHaveBeenCalledWith (
110+ {
111+ test : / \. ( t s x | t s | j s | m j s | j s x ) $ / ,
112+ } ,
113+ expect . any ( Function ) ,
114+ )
106115
107- expect ( transform . mock . calls [ 0 ] [ 1 ] ( {
108- code : ``
109- } ) ) . toBe ( '' )
116+ expect (
117+ transform . mock . calls [ 0 ] [ 1 ] ( {
118+ code : `` ,
119+ } ) ,
120+ ) . toBe ( '' )
110121
111- vi . mocked ( codeExtract ) . mockReturnValue ( {
112- code : '<div></div>' ,
113- css : ''
114- } as any )
115- await expect ( transform . mock . calls [ 1 ] [ 1 ] ( {
116- code : `import { Box } from '@devup-ui/react'
122+ vi . mocked ( codeExtract ) . mockReturnValue ( {
123+ code : '<div></div>' ,
124+ css : '' ,
125+ } as any )
126+ await expect (
127+ transform . mock . calls [ 1 ] [ 1 ] ( {
128+ code : `import { Box } from '@devup-ui/react'
117129const App = () => <Box></Box>` ,
118- resourcePath : 'src/App.tsx'
119- } ) ) . resolves . toBe ( '<div></div>' )
130+ resourcePath : 'src/App.tsx' ,
131+ } ) ,
132+ ) . resolves . toBe ( '<div></div>' )
133+ } )
134+ it ( 'should transform with include' , async ( ) => {
135+ const plugin = DevupUIRsbuildPlugin ( {
136+ include : [ 'lib' ] ,
120137 } )
121- it ( 'should transform with include' , async ( ) => {
122- const plugin = DevupUIRsbuildPlugin ( {
123- include : [ 'lib' ]
124- } )
125- expect ( plugin ) . toBeDefined ( )
126- expect ( plugin . setup ) . toBeDefined ( )
127- const transform = vi . fn ( )
128- await plugin . setup ( {
129- transform,
130- } as any )
131- expect ( transform ) . toHaveBeenCalled ( )
132- expect ( transform ) . toHaveBeenCalledWith ( {
133- test : / \. ( t s x | t s | j s | m j s | j s x ) $ / ,
134- } , expect . any ( Function ) )
135- vi . mocked ( codeExtract ) . mockReturnValue ( {
136- code : '<div></div>' ,
137- css : '.devup-ui-1 { color: red; }'
138- } as any )
139- let ret = await transform . mock . calls [ 1 ] [ 1 ] ( {
140- code : `import { Box } from '@devup-ui/react'
138+ expect ( plugin ) . toBeDefined ( )
139+ expect ( plugin . setup ) . toBeDefined ( )
140+ const transform = vi . fn ( )
141+ await plugin . setup ( {
142+ transform,
143+ } as any )
144+ expect ( transform ) . toHaveBeenCalled ( )
145+ expect ( transform ) . toHaveBeenCalledWith (
146+ {
147+ test : / \. ( t s x | t s | j s | m j s | j s x ) $ / ,
148+ } ,
149+ expect . any ( Function ) ,
150+ )
151+ vi . mocked ( codeExtract ) . mockReturnValue ( {
152+ code : '<div></div>' ,
153+ css : '.devup-ui-1 { color: red; }' ,
154+ } as any )
155+ const ret = await transform . mock . calls [ 1 ] [ 1 ] ( {
156+ code : `import { Box } from '@devup-ui/react'
141157const App = () => <Box></Box>` ,
142- resourcePath : 'src/App.tsx'
143- } )
144- expect ( ret ) . toBe ( '<div></div>' )
145- expect ( writeFile ) . toHaveBeenCalledWith ( resolve ( '.df' , 'devup-ui.css' ) , expect . stringMatching ( / \/ \* s r c \/ A p p \. t s x \d + \* \/ / ) , {
146- encoding : 'utf-8'
147- } )
158+ resourcePath : 'src/App.tsx' ,
159+ } )
160+ expect ( ret ) . toBe ( '<div></div>' )
161+ expect ( writeFile ) . toHaveBeenCalledWith (
162+ resolve ( '.df' , 'devup-ui.css' ) ,
163+ expect . stringMatching ( / \/ \* s r c \/ A p p \. t s x \d + \* \/ / ) ,
164+ {
165+ encoding : 'utf-8' ,
166+ } ,
167+ )
148168
149- let ret1 = await transform . mock . calls [ 1 ] [ 1 ] ( {
150- code : `import { Box } from '@devup-ui/react'
169+ const ret1 = await transform . mock . calls [ 1 ] [ 1 ] ( {
170+ code : `import { Box } from '@devup-ui/react'
151171const App = () => <Box></Box>` ,
152- resourcePath : 'node_modules/@devup-ui/react/index.tsx'
153- } )
154- expect ( ret1 ) . toBe ( `import { Box } from '@devup-ui/react'
155- const App = () => <Box></Box>` )
172+ resourcePath : 'node_modules/@devup-ui/react/index.tsx' ,
156173 } )
157- } )
174+ expect ( ret1 ) . toBe ( `import { Box } from '@devup-ui/react'
175+ const App = () => <Box></Box>` )
176+ } )
177+ } )
0 commit comments