-
Notifications
You must be signed in to change notification settings - Fork 4k
/
typography-utils.js
214 lines (209 loc) · 4.47 KB
/
typography-utils.js
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
/**
* Internal dependencies
*/
import { getTypographyFontSizeValue } from '../typography-utils';
describe( 'typography utils', () => {
describe( 'getTypographyFontSizeValue', () => {
it( 'should return the expected values', () => {
[
// Default return non-fluid value.
{
preset: {
size: '28px',
},
typographySettings: undefined,
expected: '28px',
},
// Default return value where font size is 0.
{
preset: {
size: 0,
},
typographySettings: undefined,
expected: 0,
},
// Default return value where font size is '0'.
{
preset: {
size: '0',
},
typographySettings: undefined,
expected: '0',
},
// Default return non-fluid value where `size` is undefined.
{
preset: {
size: undefined,
},
typographySettings: undefined,
expected: undefined,
},
// Should return non-fluid value when fluid is `false`.
{
preset: {
size: '28px',
fluid: false,
},
typographySettings: {
fluid: true,
},
expected: '28px',
},
// Should coerce integer to `px` and return fluid value.
{
preset: {
size: 33,
fluid: true,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(24.75px, 1.546875rem + ((1vw - 7.68px) * 2.975), 49.5px)',
},
// Should coerce float to `px` and return fluid value.
{
preset: {
size: 100.23,
fluid: true,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(75.1725px, 4.69828125rem + ((1vw - 7.68px) * 9.035), 150.345px)',
},
// Should return incoming value when already clamped.
{
preset: {
size: 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
fluid: false,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
},
// Should return incoming value with unsupported unit.
{
preset: {
size: '1000%',
fluid: false,
},
typographySettings: {
fluid: true,
},
expected: '1000%',
},
// Should return fluid value.
{
preset: {
size: '1.75rem',
},
typographySettings: {
fluid: true,
},
expected:
'clamp(1.3125rem, 1.3125rem + ((1vw - 0.48rem) * 2.524), 2.625rem)',
},
// Should return fluid value for floats with units.
{
preset: {
size: '100.175px',
},
typographySettings: {
fluid: true,
},
expected:
'clamp(75.13125px, 4.695703125rem + ((1vw - 7.68px) * 9.03), 150.2625px)',
},
// Should return default fluid values with empty fluid array.
{
preset: {
size: '28px',
fluid: [],
},
typographySettings: {
fluid: true,
},
expected:
'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
},
// Should return default fluid values with null values.
{
preset: {
size: '28px',
fluid: null,
},
typographySettings: {
fluid: true,
},
expected:
'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)',
},
// Should return size with invalid fluid units.
{
preset: {
size: '10em',
fluid: {
min: '20vw',
max: '50%',
},
},
typographySettings: {
fluid: true,
},
expected: '10em',
},
// Should return fluid clamp value.
{
preset: {
size: '28px',
fluid: {
min: '20px',
max: '50rem',
},
},
typographySettings: {
fluid: true,
},
expected:
'clamp(20px, 1.25rem + ((1vw - 7.68px) * 93.75), 50rem)',
},
// Should return clamp value with default fluid max value.
{
preset: {
size: '28px',
fluid: {
min: '2.6rem',
},
},
typographySettings: {
fluid: true,
},
expected:
'clamp(2.6rem, 2.6rem + ((1vw - 0.48rem) * 0.048), 42px)',
},
// Should return clamp value with default fluid min value.
{
preset: {
size: '28px',
fluid: {
max: '80px',
},
},
typographySettings: {
fluid: true,
},
expected:
'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 7.091), 80px)',
},
].forEach( ( { preset, typographySettings, expected } ) => {
expect(
getTypographyFontSizeValue( preset, typographySettings )
).toBe( expected );
} );
} );
} );
} );