-
Notifications
You must be signed in to change notification settings - Fork 4k
/
utils.ts
288 lines (249 loc) · 7.83 KB
/
utils.ts
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Internal dependencies
*/
import {
filterUnitsWithSettings,
useCustomUnits,
getValidParsedQuantityAndUnit,
getUnitsWithCurrentUnit,
parseQuantityAndUnitFromRawValue,
} from '../utils';
import type { WPUnitControlUnit } from '../types';
describe( 'UnitControl utils', () => {
describe( 'useCustomUnits', () => {
it( 'should return filtered css units', () => {
const cssUnits = [
{ value: 'px', label: 'pixel' },
{ value: '%', label: 'percent' },
];
const units = useCustomUnits( {
availableUnits: [ 'em', 'px' ],
units: cssUnits,
} );
expect( units ).toEqual( [ { value: 'px', label: 'pixel' } ] );
} );
it( 'should add default values to available units', () => {
const cssUnits = [
{ value: 'px', label: 'pixel' },
{ value: '%', label: 'percent' },
];
const units = useCustomUnits( {
availableUnits: [ '%', 'px' ],
defaultValues: { '%': 10, px: 10 },
units: cssUnits,
} );
expect( units ).toEqual( [
{ value: 'px', label: 'pixel', default: 10 },
{ value: '%', label: 'percent', default: 10 },
] );
} );
it( 'should add default values to available units even if the default values are strings', () => {
// Although the public APIs of the component expect a `number` as the type of the
// default values, it's still good to test for strings (as it can happen in un-typed
// environments)
const cssUnits = [
{ value: 'px', label: 'pixel' },
{ value: '%', label: 'percent' },
];
const units = useCustomUnits( {
availableUnits: [ '%', 'px' ],
defaultValues: {
// @ts-expect-error (passing a string instead of a number is the point of the test)
'%': '14',
// @ts-expect-error (passing a string instead of a number is the point of the test)
px: 'not a valid numeric quantity',
},
units: cssUnits,
} );
expect( units ).toEqual( [
{ value: 'px', label: 'pixel', default: undefined },
{ value: '%', label: 'percent', default: 14 },
] );
} );
it( 'should return an empty array where availableUnits match no preferred css units', () => {
const cssUnits = [
{ value: 'em', label: 'em' },
{ value: 'vh', label: 'vh' },
];
const units = useCustomUnits( {
availableUnits: [ '%', 'px' ],
defaultValues: { '%': 10, px: 10 },
units: cssUnits,
} );
expect( units ).toHaveLength( 0 );
} );
} );
describe( 'filterUnitsWithSettings', () => {
it( 'should return filtered units array', () => {
const preferredUnits = [ '%', 'px' ];
const availableUnits = [
{ value: 'px', label: 'pixel' },
{ value: 'em', label: 'em' },
];
expect(
filterUnitsWithSettings( preferredUnits, availableUnits )
).toEqual( [ { value: 'px', label: 'pixel' } ] );
} );
it( 'should return empty array where preferred units match no available css unit', () => {
const preferredUnits = [ '%', 'px' ];
const availableUnits = [ { value: 'em', label: 'em' } ];
expect(
filterUnitsWithSettings( preferredUnits, availableUnits )
).toEqual( [] );
} );
// Although the component's APIs and types don't allow for `false` as a value
// unit lists, it's good to keep this test around for backwards compat.
it( 'should return empty array where available units is set to false', () => {
const preferredUnits = [ '%', 'px' ];
const availableUnits = false;
expect(
// @ts-expect-error (passing `false` instead of a valid array of units is the point of the test)
filterUnitsWithSettings( preferredUnits, availableUnits )
).toEqual( [] );
} );
it( 'should return empty array where available units is set to an empty array', () => {
const preferredUnits = [ '%', 'px' ];
const availableUnits: WPUnitControlUnit[] = [];
expect(
filterUnitsWithSettings( preferredUnits, availableUnits )
).toEqual( [] );
} );
} );
describe( 'getValidParsedQuantityAndUnit', () => {
it( 'should parse valid number and unit', () => {
const nextValue = '42px';
expect( getValidParsedQuantityAndUnit( nextValue ) ).toEqual( [
42,
'px',
] );
} );
it( 'should return next value only where no known unit parsed', () => {
const nextValue = '365zz';
expect( getValidParsedQuantityAndUnit( nextValue ) ).toEqual( [
365,
undefined,
] );
} );
it( 'should return fallback value', () => {
const nextValue = 'thirteen';
const preferredUnits = [ { value: 'em', label: 'em' } ];
const fallbackValue = 13;
expect(
getValidParsedQuantityAndUnit(
nextValue,
preferredUnits,
fallbackValue
)
).toEqual( [ 13, 'em' ] );
} );
it( 'should return fallback unit', () => {
const nextValue = '911';
const fallbackUnit = '%';
expect(
getValidParsedQuantityAndUnit(
nextValue,
undefined,
undefined,
fallbackUnit
)
).toEqual( [ 911, '%' ] );
} );
it( 'should return first unit in preferred units collection as second fallback unit', () => {
const nextValue = 101;
const preferredUnits = [ { value: 'px', label: 'pixel' } ];
expect(
getValidParsedQuantityAndUnit( nextValue, preferredUnits )
).toEqual( [ 101, 'px' ] );
} );
} );
describe( 'getUnitsWithCurrentUnit', () => {
const limitedUnits = [
{
value: 'px',
label: 'px',
},
{
value: 'em',
label: 'em',
},
];
it( 'should return units list with valid current unit prepended', () => {
const result = getUnitsWithCurrentUnit(
'20%',
undefined,
limitedUnits
);
expect( result ).toHaveLength( 3 );
const currentUnit = result.shift();
expect( currentUnit?.value ).toBe( '%' );
expect( currentUnit?.label ).toBe( '%' );
expect( result ).toEqual( limitedUnits );
} );
it( 'should return units list with valid current unit prepended using legacy values', () => {
const result = getUnitsWithCurrentUnit( 20, '%', limitedUnits );
expect( result ).toHaveLength( 3 );
const currentUnit = result.shift();
expect( currentUnit?.value ).toBe( '%' );
expect( currentUnit?.label ).toBe( '%' );
expect( result ).toEqual( limitedUnits );
} );
it( 'should return units list without invalid current unit prepended', () => {
const result = getUnitsWithCurrentUnit(
'20null',
undefined,
limitedUnits
);
expect( result ).toHaveLength( 2 );
expect( result ).toEqual( limitedUnits );
} );
it( 'should return units list without an existing current unit prepended', () => {
const result = getUnitsWithCurrentUnit(
'20em',
undefined,
limitedUnits
);
expect( result ).toHaveLength( 2 );
expect( result ).toEqual( limitedUnits );
} );
} );
describe( 'parseQuantityAndUnitFromRawValue', () => {
const cases: [
number | string | undefined,
number | undefined,
string | undefined,
][] = [
// Test undefined.
[ undefined, undefined, undefined ],
// Test integers and non-integers.
[ 1, 1, undefined ],
[ 1.25, 1.25, undefined ],
[ '123', 123, undefined ],
[ '1.5', 1.5, undefined ],
[ '0.75', 0.75, undefined ],
// Valid simple CSS values.
[ '20px', 20, 'px' ],
[ '0.8em', 0.8, 'em' ],
[ '2rem', 2, 'rem' ],
[ '1.4vw', 1.4, 'vw' ],
[ '0.4vh', 0.4, 'vh' ],
[ '-5px', -5, 'px' ],
// Complex CSS values that shouldn't parse.
[ 'abs(-15px)', undefined, undefined ],
[ 'calc(10px + 1)', undefined, undefined ],
[ 'clamp(2.5rem, 4vw, 3rem)', undefined, undefined ],
[ 'max(4.5em, 3vh)', undefined, undefined ],
[ 'min(10px, 1rem)', undefined, undefined ],
[ 'minmax(30px, auto)', undefined, undefined ],
[ 'var(--wp--font-size)', undefined, undefined ],
];
test.each( cases )(
'given %p as argument, returns value = %p and unit = %p',
( rawValue, expectedQuantity, expectedUnit ) => {
const [ quantity, unit ] =
parseQuantityAndUnitFromRawValue( rawValue );
expect( quantity ).toBe( expectedQuantity );
expect( unit ).toBe( expectedUnit );
}
);
} );
} );