index.test.js
6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
let postcss = require('postcss')
let clamp = require('./')
async function run (input, output, opts) {
let result = await postcss([clamp(opts)]).process(input, {
from: '/test.css'
})
expect(result.css).toEqual(output)
expect(result.warnings()).toHaveLength(0)
return result
}
it('handle simple transformation (only values)', async () => {
await run(
'a{ width: clamp(10px, 64px, 80px); }',
'a{ width: max(10px, min(64px, 80px)); }'
)
})
it('handle simple transformation (only values) with preserve', async () => {
await run(
'a{ width: clamp(10px, 64px, 80px); }',
'a{ width: max(10px, min(64px, 80px)); width: clamp(10px, 64px, 80px); }',
{ preserve: true }
)
})
it('handle transformation with functions', async () => {
await run(
'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); }'
)
})
it('handle transformation with functions with preserve', async () => {
await run(
'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); ' +
'width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
{ preserve: true }
)
})
it('handle transformation with different units', async () => {
await run(
'a{ width: clamp(10%, 2px, 4rem); }',
'a{ width: max(10%, min(2px, 4rem)); }'
)
})
it('handle transformation with different units and preserve', async () => {
await run(
'a{ width: clamp(10%, 2px, 4rem); }',
'a{ width: max(10%, min(2px, 4rem)); width: clamp(10%, 2px, 4rem); }',
{ preserve: true }
)
})
it('transform only function with 3 parameters', async () => {
await run(
'a{ width: clamp(10%, 2px, 4rem);' +
'\nheight: clamp(10px, 20px, 30px, 40px); }',
'a{ width: max(10%, min(2px, 4rem));' +
'\nheight: clamp(10px, 20px, 30px, 40px); }'
)
})
it('transform only clamp function', async () => {
await run(
'a{ width: clamp(10%, 2px, 4rem);\nheight: calc(10px + 100%); }',
'a{ width: max(10%, min(2px, 4rem));\nheight: calc(10px + 100%); }'
)
})
it('precalculate second and third with the same unit (int values)', async () => {
await run('a{ width: clamp(10%, 2px, 5px); }', 'a{ width: max(10%, 7px); }', {
precalculate: true
})
})
it('precalculate second and third with the same unit (float values)', async () => {
await run(
'a{ width: clamp(10%, 2.5px, 5.1px); }',
'a{ width: max(10%, 7.6px); }',
{ precalculate: true }
)
})
it('precalculate second and third with the same unit (float and int values)', async () => {
await run(
'a{ width: clamp(10%, 2.5px, 5px); }',
'a{ width: max(10%, 7.5px); }',
{ precalculate: true }
)
})
it('precalculate 2nd & 3rd with the same unit (float and int vals) & preserve', async () => {
await run(
'a{ width: clamp(10%, 2.5px, 5px); }',
'a{ width: max(10%, 7.5px); width: clamp(10%, 2.5px, 5px); }',
{ precalculate: true, preserve: true }
)
})
it('precalculate all values with the same unit (int values)', async () => {
await run('a{ width: clamp(10px, 2px, 5px); }', 'a{ width: 17px; }', {
precalculate: true
})
})
it('precalculate all values with the same unit (float values)', async () => {
await run(
'a{ width: clamp(10.4px, 2.11px, 5.9px); }',
'a{ width: 18.41px; }',
{ precalculate: true }
)
})
it('precalculate all values with the same unit (int and float values)', async () => {
await run('a{ width: clamp(10.4px, 2px, 5.9px); }', 'a{ width: 18.3px; }', {
precalculate: true
})
})
it('handle function with enable precalculation as third', async () => {
await run(
'a{ width: clamp(10px, 2px, calc(10px + 100%)); }',
'a{ width: max(10px, min(2px, calc(10px + 100%))); }',
{ precalculate: true }
)
})
it('handle function with enable precalculation as second', async () => {
await run(
'a{ width: clamp(10px, calc(10px + 100%), 2px); }',
'a{ width: max(10px, min(calc(10px + 100%), 2px)); }',
{ precalculate: true }
)
})
it('handle function with enable precalculation as first', async () => {
await run(
'a{ width: clamp(calc(10px + 100%), 10px, 2px); }',
'a{ width: max(calc(10px + 100%), 12px); }',
{ precalculate: true }
)
})
it('handle function with enable precalculation as all', async () => {
await run(
'a{ width: clamp(calc(10px + 100%), calc(10rem + 200%), 10px); }',
'a{ width: max(calc(10px + 100%), min(calc(10rem + 200%), 10px)); }',
{ precalculate: true }
)
})
it('handle not valid values', async () => {
await run('a{ width: clamp(a, b, c); }', 'a{ width: max(a, min(b, c)); }', {
precalculate: true
})
})
it('handle not valid values with preserve', async () => {
await run(
'a{ width: clamp(a, b, c); }',
'a{ width: max(a, min(b, c)); width: clamp(a, b, c); }',
{ precalculate: true, preserve: true }
)
})
it('handle not valid values mixed with valid', async () => {
await run(
'a{ width: clamp(a, 1px, 2em); }',
'a{ width: max(a, min(1px, 2em)); }',
{ precalculate: true }
)
})
it('handle not valid values mixed with valid and preserve', async () => {
await run(
'a{ width: clamp(a, 1px, 2em); }',
'a{ width: max(a, min(1px, 2em)); width: clamp(a, 1px, 2em); }',
{ precalculate: true, preserve: true }
)
})
it('handle complex values', async () => {
await run(
'a{ grid-template-columns: clamp(22rem, 40%, 32rem) minmax(0, 1fr); }',
'a{ grid-template-columns: max(22rem, min(40%, 32rem)) minmax(0, 1fr); }'
)
})
it('handle multiple complex values', async () => {
await run(
'a{ margin: clamp(1rem, 2%, 3rem) 4px clamp(5rem, 6%, 7rem) 8rem; }',
'a{ margin: max(1rem, min(2%, 3rem)) 4px max(5rem, min(6%, 7rem)) 8rem; }'
)
})
it('handle calc', async () => {
await run(
'a{ margin: 0 40px 0 calc(-1 * clamp(32px, 16vw, 64px)); }',
'a{ margin: 0 40px 0 calc(-1 * max(32px, min(16vw, 64px))); }'
)
})
it('handle multiple calc', async () => {
await run(
'a{ margin: calc(-1 * clamp(1px, 2vw, 3px)) calc(-1 * clamp(4px, 5vw, 6px)); }',
'a{ margin: calc(-1 * max(1px, min(2vw, 3px))) calc(-1 * max(4px, min(5vw, 6px))); }'
)
})
it('handle nested clamp', async () => {
await run(
'a{ font-size: clamp(clamp(1rem, 2vw, 3rem), 4vw, 5rem); }',
'a{ font-size: max(max(1rem, min(2vw, 3rem)), min(4vw, 5rem)); }'
)
})