1
1
import { describe , expect , it } from 'vitest'
2
2
import { composeVisitors , transform } from 'lightningcss'
3
- import pxtorem from '../dist/index'
3
+ import pxtorem from '../src/index'
4
+ import { validatePositiveInteger } from '../src/utils/errorHandler'
4
5
5
6
describe ( 'pxtorem plugin' , ( ) => {
6
- it ( 'should convert pixel to rem' , ( ) => {
7
+ it ( 'should convert pixel to rem using default configuration ' , ( ) => {
7
8
const css = 'div { margin: 16px; }'
8
9
9
10
const output = transform ( {
10
11
filename : 'input.css' ,
11
12
code : Buffer . from ( css ) ,
12
- minify : true ,
13
+ minify : false ,
13
14
sourceMap : false ,
14
15
visitor : composeVisitors ( [ pxtorem ( ) ] )
15
16
} ) . code . toString ( )
@@ -23,15 +24,8 @@ describe('pxtorem plugin', () => {
23
24
const output = transform ( {
24
25
filename : 'input.css' ,
25
26
code : Buffer . from ( css ) ,
26
- minify : true ,
27
+ minify : false ,
27
28
sourceMap : false ,
28
- drafts : {
29
- customMedia : true
30
- } ,
31
- nonStandard : {
32
- deepSelectorCombinator : true
33
- } ,
34
- errorRecovery : true ,
35
29
visitor : composeVisitors ( [ pxtorem ( ) ] )
36
30
} ) . code . toString ( )
37
31
@@ -44,7 +38,7 @@ describe('pxtorem plugin', () => {
44
38
const output = transform ( {
45
39
filename : 'input.css' ,
46
40
code : Buffer . from ( css ) ,
47
- minify : true ,
41
+ minify : false ,
48
42
sourceMap : false ,
49
43
visitor : composeVisitors ( [ pxtorem ( ) ] )
50
44
} ) . code . toString ( )
@@ -58,7 +52,7 @@ describe('pxtorem plugin', () => {
58
52
const output = transform ( {
59
53
filename : 'input.css' ,
60
54
code : Buffer . from ( css ) ,
61
- minify : true ,
55
+ minify : false ,
62
56
sourceMap : false ,
63
57
visitor : composeVisitors ( [ pxtorem ( ) ] )
64
58
} ) . code . toString ( )
@@ -72,7 +66,7 @@ describe('pxtorem plugin', () => {
72
66
const output = transform ( {
73
67
filename : 'input.css' ,
74
68
code : Buffer . from ( css ) ,
75
- minify : true ,
69
+ minify : false ,
76
70
sourceMap : false ,
77
71
visitor : composeVisitors ( [ pxtorem ( ) ] )
78
72
} ) . code . toString ( )
@@ -86,7 +80,7 @@ describe('pxtorem plugin', () => {
86
80
const output = transform ( {
87
81
filename : 'input.css' ,
88
82
code : Buffer . from ( css ) ,
89
- minify : true ,
83
+ minify : false ,
90
84
sourceMap : false ,
91
85
visitor : composeVisitors ( [ pxtorem ( ) ] )
92
86
} ) . code . toString ( )
@@ -100,7 +94,7 @@ describe('pxtorem plugin', () => {
100
94
const output = transform ( {
101
95
filename : 'input.css' ,
102
96
code : Buffer . from ( css ) ,
103
- minify : true ,
97
+ minify : false ,
104
98
sourceMap : false ,
105
99
visitor : composeVisitors ( [ pxtorem ( ) ] )
106
100
} ) . code . toString ( )
@@ -114,7 +108,7 @@ describe('pxtorem plugin', () => {
114
108
const output = transform ( {
115
109
filename : 'input.css' ,
116
110
code : Buffer . from ( css ) ,
117
- minify : true ,
111
+ minify : false ,
118
112
sourceMap : false ,
119
113
visitor : composeVisitors ( [ pxtorem ( ) ] )
120
114
} ) . code . toString ( )
@@ -128,7 +122,7 @@ describe('pxtorem plugin', () => {
128
122
const output = transform ( {
129
123
filename : 'input.css' ,
130
124
code : Buffer . from ( css ) ,
131
- minify : true ,
125
+ minify : false ,
132
126
sourceMap : false ,
133
127
visitor : composeVisitors ( [ pxtorem ( { rootValue : 8 , unitPrecision : 2 } ) ] )
134
128
} ) . code . toString ( )
@@ -142,11 +136,83 @@ describe('pxtorem plugin', () => {
142
136
const output = transform ( {
143
137
filename : 'input.css' ,
144
138
code : Buffer . from ( css ) ,
145
- minify : true ,
139
+ minify : false ,
146
140
sourceMap : false ,
147
141
visitor : composeVisitors ( [ pxtorem ( { unitPrecision : 2 } ) ] )
148
142
} ) . code . toString ( )
149
143
150
144
expect ( output ) . toMatchSnapshot ( )
151
145
} )
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
+ } )
152
218
} )
0 commit comments