11import { describe , expect , it } from 'vitest'
22import { composeVisitors , transform } from 'lightningcss'
3- import pxtorem from '../dist/index'
3+ import pxtorem from '../src/index'
4+ import { validatePositiveInteger } from '../src/utils/errorHandler'
45
56describe ( 'pxtorem plugin' , ( ) => {
6- it ( 'should convert pixel to rem' , ( ) => {
7+ it ( 'should convert pixel to rem using default configuration ' , ( ) => {
78 const css = 'div { margin: 16px; }'
89
910 const output = transform ( {
1011 filename : 'input.css' ,
1112 code : Buffer . from ( css ) ,
12- minify : true ,
13+ minify : false ,
1314 sourceMap : false ,
1415 visitor : composeVisitors ( [ pxtorem ( ) ] )
1516 } ) . code . toString ( )
@@ -23,15 +24,8 @@ describe('pxtorem plugin', () => {
2324 const output = transform ( {
2425 filename : 'input.css' ,
2526 code : Buffer . from ( css ) ,
26- minify : true ,
27+ minify : false ,
2728 sourceMap : false ,
28- drafts : {
29- customMedia : true
30- } ,
31- nonStandard : {
32- deepSelectorCombinator : true
33- } ,
34- errorRecovery : true ,
3529 visitor : composeVisitors ( [ pxtorem ( ) ] )
3630 } ) . code . toString ( )
3731
@@ -44,7 +38,7 @@ describe('pxtorem plugin', () => {
4438 const output = transform ( {
4539 filename : 'input.css' ,
4640 code : Buffer . from ( css ) ,
47- minify : true ,
41+ minify : false ,
4842 sourceMap : false ,
4943 visitor : composeVisitors ( [ pxtorem ( ) ] )
5044 } ) . code . toString ( )
@@ -58,7 +52,7 @@ describe('pxtorem plugin', () => {
5852 const output = transform ( {
5953 filename : 'input.css' ,
6054 code : Buffer . from ( css ) ,
61- minify : true ,
55+ minify : false ,
6256 sourceMap : false ,
6357 visitor : composeVisitors ( [ pxtorem ( ) ] )
6458 } ) . code . toString ( )
@@ -72,7 +66,7 @@ describe('pxtorem plugin', () => {
7266 const output = transform ( {
7367 filename : 'input.css' ,
7468 code : Buffer . from ( css ) ,
75- minify : true ,
69+ minify : false ,
7670 sourceMap : false ,
7771 visitor : composeVisitors ( [ pxtorem ( ) ] )
7872 } ) . code . toString ( )
@@ -86,7 +80,7 @@ describe('pxtorem plugin', () => {
8680 const output = transform ( {
8781 filename : 'input.css' ,
8882 code : Buffer . from ( css ) ,
89- minify : true ,
83+ minify : false ,
9084 sourceMap : false ,
9185 visitor : composeVisitors ( [ pxtorem ( ) ] )
9286 } ) . code . toString ( )
@@ -100,7 +94,7 @@ describe('pxtorem plugin', () => {
10094 const output = transform ( {
10195 filename : 'input.css' ,
10296 code : Buffer . from ( css ) ,
103- minify : true ,
97+ minify : false ,
10498 sourceMap : false ,
10599 visitor : composeVisitors ( [ pxtorem ( ) ] )
106100 } ) . code . toString ( )
@@ -114,7 +108,7 @@ describe('pxtorem plugin', () => {
114108 const output = transform ( {
115109 filename : 'input.css' ,
116110 code : Buffer . from ( css ) ,
117- minify : true ,
111+ minify : false ,
118112 sourceMap : false ,
119113 visitor : composeVisitors ( [ pxtorem ( ) ] )
120114 } ) . code . toString ( )
@@ -128,7 +122,7 @@ describe('pxtorem plugin', () => {
128122 const output = transform ( {
129123 filename : 'input.css' ,
130124 code : Buffer . from ( css ) ,
131- minify : true ,
125+ minify : false ,
132126 sourceMap : false ,
133127 visitor : composeVisitors ( [ pxtorem ( { rootValue : 8 , unitPrecision : 2 } ) ] )
134128 } ) . code . toString ( )
@@ -142,11 +136,83 @@ describe('pxtorem plugin', () => {
142136 const output = transform ( {
143137 filename : 'input.css' ,
144138 code : Buffer . from ( css ) ,
145- minify : true ,
139+ minify : false ,
146140 sourceMap : false ,
147141 visitor : composeVisitors ( [ pxtorem ( { unitPrecision : 2 } ) ] )
148142 } ) . code . toString ( )
149143
150144 expect ( output ) . toMatchSnapshot ( )
151145 } )
146+
147+ it ( 'should use custom rootValue' , ( ) => {
148+ const css = 'div { margin: 32px; }'
149+
150+ const output = transform ( {
151+ filename : 'input.css' ,
152+ code : Buffer . from ( css ) ,
153+ minify : false ,
154+ sourceMap : false ,
155+ visitor : composeVisitors ( [ pxtorem ( { rootValue : 32 } ) ] )
156+ } ) . code . toString ( )
157+
158+ expect ( output ) . toMatchSnapshot ( )
159+ } )
160+
161+ it ( 'should handle extreme pixel values' , ( ) => {
162+ const css = 'div { margin: 10000px; }'
163+
164+ const output = transform ( {
165+ filename : 'input.css' ,
166+ code : Buffer . from ( css ) ,
167+ minify : false ,
168+ sourceMap : false ,
169+ visitor : composeVisitors ( [ pxtorem ( ) ] )
170+ } ) . code . toString ( )
171+
172+ expect ( output ) . toMatchSnapshot ( )
173+ } )
174+
175+ it ( 'should handle very small pixel values' , ( ) => {
176+ const css = 'div { margin: 0.001px; }'
177+
178+ const output = transform ( {
179+ filename : 'input.css' ,
180+ code : Buffer . from ( css ) ,
181+ minify : false ,
182+ sourceMap : false ,
183+ visitor : composeVisitors ( [ pxtorem ( ) ] )
184+ } ) . code . toString ( )
185+
186+ expect ( output ) . toMatchSnapshot ( )
187+ } )
188+
189+ it ( 'should throw error for negative rootValue' , ( ) => {
190+ expect ( ( ) => pxtorem ( { rootValue : - 1 } ) ) . toThrowError ( 'Invalid rootValue: must not be negative.' )
191+ } )
192+
193+ it ( 'should throw error for negative unitPrecision' , ( ) => {
194+ expect ( ( ) => pxtorem ( { unitPrecision : - 1 } ) ) . toThrowError ( 'Invalid unitPrecision: must not be negative.' )
195+ } )
196+ } )
197+
198+ describe ( 'validatePositiveInteger' , ( ) => {
199+ it ( 'should throw error for undefined value' , ( ) => {
200+ expect ( ( ) => validatePositiveInteger ( undefined , 'rootValue' ) ) . toThrowError ( 'Invalid rootValue: must be a valid number.' )
201+ } )
202+
203+ it ( 'should throw error for NaN value' , ( ) => {
204+ expect ( ( ) => validatePositiveInteger ( NaN , 'rootValue' ) ) . toThrowError ( 'Invalid rootValue: must be a valid number.' )
205+ } )
206+
207+ it ( 'should throw error for negative value' , ( ) => {
208+ expect ( ( ) => validatePositiveInteger ( - 1 , 'rootValue' ) ) . toThrowError ( 'Invalid rootValue: must not be negative.' )
209+ } )
210+
211+ it ( 'should throw error for decimal value' , ( ) => {
212+ expect ( ( ) => validatePositiveInteger ( 1.5 , 'rootValue' ) ) . toThrowError ( 'Invalid rootValue: must not be a decimal.' )
213+ } )
214+
215+ it ( 'should pass for valid integer value' , ( ) => {
216+ expect ( ( ) => validatePositiveInteger ( 1 , 'rootValue' ) ) . not . toThrow ( )
217+ } )
152218} )
0 commit comments