Skip to content

Commit 3eca46c

Browse files
committed
test(pxtorem): update snapshots and tests to use 'pixel' terminology and add new test cases for custom configurations
1 parent 242dc71 commit 3eca46c

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`pxtorem plugin > should convert px to rem 1`] = `"div{margin:1rem}"`;
3+
exports[`pxtorem plugin > should convert pixel to rem 1`] = `"div{margin:1rem}"`;
44

5-
exports[`pxtorem plugin > should convert px to rem in custom properties 1`] = `":root{--margin:1rem}div{margin:var(--margin)}"`;
5+
exports[`pxtorem plugin > should convert pixel to rem in custom properties 1`] = `":root{--margin:1rem}div{margin:var(--margin)}"`;
66

7-
exports[`pxtorem plugin > should convert px to rem in keyframes 1`] = `"@keyframes fadeIn{0%{margin:1rem}to{margin:2rem}}"`;
7+
exports[`pxtorem plugin > should convert pixel to rem in keyframes 1`] = `"@keyframes fadeIn{0%{margin:1rem}to{margin:2rem}}"`;
88

9-
exports[`pxtorem plugin > should convert px to rem in media queries 1`] = `"@media (width>=48rem){div{margin:1rem}}"`;
9+
exports[`pxtorem plugin > should convert pixel to rem in media queries 1`] = `"@media (width>=48rem){div{margin:1rem}}"`;
10+
11+
exports[`pxtorem plugin > should handle custom configuration 1`] = `"div{margin:2rem}"`;
12+
13+
exports[`pxtorem plugin > should handle custom configuration with unitPrecision 1`] = `"div{margin:2.71rem}"`;
14+
15+
exports[`pxtorem plugin > should handle float pixel units 1`] = `"div{margin:2.7087rem}"`;
16+
17+
exports[`pxtorem plugin > should handle multiple pixel values 1`] = `"div{margin:1rem 2rem}"`;
18+
19+
exports[`pxtorem plugin > should handle nested selectors 1`] = `"div{&>span{margin:1rem}}"`;
1020

1121
exports[`pxtorem plugin > should not convert other units 1`] = `"div{margin:1em}"`;

packages/pxtorem/test/index.test.ts

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { composeVisitors, transform } from 'lightningcss'
33
import pxtorem from '../dist/index'
44

55
describe('pxtorem plugin', () => {
6-
it('should convert px to rem', () => {
6+
it('should convert pixel to rem', () => {
77
const css = 'div { margin: 16px; }'
88

99
const output = transform({
@@ -38,7 +38,7 @@ describe('pxtorem plugin', () => {
3838
expect(output).toMatchSnapshot()
3939
})
4040

41-
it('should convert px to rem in media queries', () => {
41+
it('should convert pixel to rem in media queries', () => {
4242
const css = '@media (min-width: 768px) { div { margin: 16px; } }'
4343

4444
const output = transform({
@@ -52,7 +52,7 @@ describe('pxtorem plugin', () => {
5252
expect(output).toMatchSnapshot()
5353
})
5454

55-
it('should convert px to rem in keyframes', () => {
55+
it('should convert pixel to rem in keyframes', () => {
5656
const css = '@keyframes fadeIn { from { margin: 16px; } to { margin: 32px; } }'
5757

5858
const output = transform({
@@ -66,7 +66,7 @@ describe('pxtorem plugin', () => {
6666
expect(output).toMatchSnapshot()
6767
})
6868

69-
it('should convert px to rem in custom properties', () => {
69+
it('should convert pixel to rem in custom properties', () => {
7070
const css = ':root { --margin: 16px; } div { margin: var(--margin); }'
7171

7272
const output = transform({
@@ -79,4 +79,74 @@ describe('pxtorem plugin', () => {
7979

8080
expect(output).toMatchSnapshot()
8181
})
82+
83+
it('should handle multiple pixel values', () => {
84+
const css = 'div { margin: 16px 32px; }'
85+
86+
const output = transform({
87+
filename: 'input.css',
88+
code: Buffer.from(css),
89+
minify: true,
90+
sourceMap: false,
91+
visitor: composeVisitors([pxtorem()])
92+
}).code.toString()
93+
94+
expect(output).toMatchSnapshot()
95+
})
96+
97+
it('should handle nested selectors', () => {
98+
const css = 'div { & > span { margin: 16px; } }'
99+
100+
const output = transform({
101+
filename: 'input.css',
102+
code: Buffer.from(css),
103+
minify: true,
104+
sourceMap: false,
105+
visitor: composeVisitors([pxtorem()])
106+
}).code.toString()
107+
108+
expect(output).toMatchSnapshot()
109+
})
110+
111+
it('should handle float pixel units', () => {
112+
const css = 'div { margin: 43.3398589px; }'
113+
114+
const output = transform({
115+
filename: 'input.css',
116+
code: Buffer.from(css),
117+
minify: true,
118+
sourceMap: false,
119+
visitor: composeVisitors([pxtorem()])
120+
}).code.toString()
121+
122+
expect(output).toMatchSnapshot()
123+
})
124+
125+
it('should handle custom configuration', () => {
126+
const css = 'div { margin: 16px; }'
127+
128+
const output = transform({
129+
filename: 'input.css',
130+
code: Buffer.from(css),
131+
minify: true,
132+
sourceMap: false,
133+
visitor: composeVisitors([pxtorem({ rootValue: 8, unitPrecision: 2 })])
134+
}).code.toString()
135+
136+
expect(output).toMatchSnapshot()
137+
})
138+
139+
it('should handle custom configuration with unitPrecision', () => {
140+
const css = 'div { margin: 43.3398589px; }'
141+
142+
const output = transform({
143+
filename: 'input.css',
144+
code: Buffer.from(css),
145+
minify: true,
146+
sourceMap: false,
147+
visitor: composeVisitors([pxtorem({ unitPrecision: 2 })])
148+
}).code.toString()
149+
150+
expect(output).toMatchSnapshot()
151+
})
82152
})

0 commit comments

Comments
 (0)