Skip to content

Commit 5345983

Browse files
committed
test(pxtorem): update snapshots and tests to improve coverage for custom configurations and validation
1 parent 28f3139 commit 5345983

File tree

2 files changed

+180
-29
lines changed

2 files changed

+180
-29
lines changed
Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,106 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`pxtorem plugin > should convert pixel to rem 1`] = `"div{margin:1rem}"`;
3+
exports[`pxtorem plugin > should convert pixel to rem in custom properties 1`] = `
4+
":root {
5+
--margin: 1rem;
6+
}
47
5-
exports[`pxtorem plugin > should convert pixel to rem in custom properties 1`] = `":root{--margin:1rem}div{margin:var(--margin)}"`;
8+
div {
9+
margin: var(--margin);
10+
}
11+
"
12+
`;
613

7-
exports[`pxtorem plugin > should convert pixel to rem in keyframes 1`] = `"@keyframes fadeIn{0%{margin:1rem}to{margin:2rem}}"`;
14+
exports[`pxtorem plugin > should convert pixel to rem in keyframes 1`] = `
15+
"@keyframes fadeIn {
16+
from {
17+
margin: 1rem;
18+
}
819
9-
exports[`pxtorem plugin > should convert pixel to rem in media queries 1`] = `"@media (width>=48rem){div{margin:1rem}}"`;
20+
to {
21+
margin: 2rem;
22+
}
23+
}
24+
"
25+
`;
1026

11-
exports[`pxtorem plugin > should handle custom configuration 1`] = `"div{margin:2rem}"`;
27+
exports[`pxtorem plugin > should convert pixel to rem in media queries 1`] = `
28+
"@media (width >= 48rem) {
29+
div {
30+
margin: 1rem;
31+
}
32+
}
33+
"
34+
`;
1235

13-
exports[`pxtorem plugin > should handle custom configuration with unitPrecision 1`] = `"div{margin:2.71rem}"`;
36+
exports[`pxtorem plugin > should convert pixel to rem using default configuration 1`] = `
37+
"div {
38+
margin: 1rem;
39+
}
40+
"
41+
`;
1442

15-
exports[`pxtorem plugin > should handle float pixel units 1`] = `"div{margin:2.7087rem}"`;
43+
exports[`pxtorem plugin > should handle custom configuration 1`] = `
44+
"div {
45+
margin: 2rem;
46+
}
47+
"
48+
`;
1649

17-
exports[`pxtorem plugin > should handle multiple pixel values 1`] = `"div{margin:1rem 2rem}"`;
50+
exports[`pxtorem plugin > should handle custom configuration with unitPrecision 1`] = `
51+
"div {
52+
margin: 2.71rem;
53+
}
54+
"
55+
`;
1856

19-
exports[`pxtorem plugin > should handle nested selectors 1`] = `"div{&>span{margin:1rem}}"`;
57+
exports[`pxtorem plugin > should handle extreme pixel values 1`] = `
58+
"div {
59+
margin: 625rem;
60+
}
61+
"
62+
`;
2063

21-
exports[`pxtorem plugin > should not convert other units 1`] = `"div{margin:1em}"`;
64+
exports[`pxtorem plugin > should handle float pixel units 1`] = `
65+
"div {
66+
margin: 2.7087rem;
67+
}
68+
"
69+
`;
70+
71+
exports[`pxtorem plugin > should handle multiple pixel values 1`] = `
72+
"div {
73+
margin: 1rem 2rem;
74+
}
75+
"
76+
`;
77+
78+
exports[`pxtorem plugin > should handle nested selectors 1`] = `
79+
"div {
80+
& > span {
81+
margin: 1rem;
82+
}
83+
}
84+
"
85+
`;
86+
87+
exports[`pxtorem plugin > should handle very small pixel values 1`] = `
88+
"div {
89+
margin: .0001rem;
90+
}
91+
"
92+
`;
93+
94+
exports[`pxtorem plugin > should not convert other units 1`] = `
95+
"div {
96+
margin: 1em;
97+
}
98+
"
99+
`;
100+
101+
exports[`pxtorem plugin > should use custom rootValue 1`] = `
102+
"div {
103+
margin: 1rem;
104+
}
105+
"
106+
`;

packages/pxtorem/test/index.test.ts

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { describe, expect, it } from 'vitest'
22
import { composeVisitors, transform } from 'lightningcss'
3-
import pxtorem from '../dist/index'
3+
import pxtorem from '../src/index'
4+
import { validatePositiveInteger } from '../src/utils/errorHandler'
45

56
describe('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

Comments
 (0)