generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy paththeme-json-schema.test.ts
More file actions
311 lines (292 loc) · 8.38 KB
/
theme-json-schema.test.ts
File metadata and controls
311 lines (292 loc) · 8.38 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, test, expect } from 'vitest';
import { presetWithValidSchema } from '../../../__fixtures__/common';
import { ThemeJson, TokenJson } from '../theme-json';
import { validateJson, getThemeJSONSchema } from '../theme-json-schema';
const schema = getThemeJSONSchema(presetWithValidSchema.theme);
const getJson = (tokens: Record<string, TokenJson>): ThemeJson => ({
tokens,
contexts: {
navigation: {
tokens,
},
},
});
const validateTokens = (tokens: Record<string, TokenJson>): boolean => {
return validateJson(getJson(tokens), schema);
};
describe('validateJson', () => {
test('accepts only predefined token names', () => {
expect(() => {
validateTokens({
'invalid-token': {
$value: '#ffff',
},
});
}).toThrowError(
'Tokens validation error: instance.tokens is not allowed to have the additional property "invalid-token"',
);
});
test('accepts tokens descriptions', () => {
expect(
validateTokens({
'color-button': {
$value: {
light: '#ffffff',
dark: 'rgba(0, 7, 22, 0.2)',
},
$description: 'token description',
},
}),
).toBe(true);
});
test('accepts font family tokens', () => {
expect(
validateTokens({
'font-family-main': {
$value: 'any string',
},
}),
).toBe(true);
});
test('accepts border radius tokens in certain formats', () => {
expect(
validateTokens({
'border-radius-one': {
$value: '100px',
},
'border-radius-two': {
$value: '0px',
},
'border-radius-three': {
$value: '20rem',
},
'border-radius-four': {
$value: '20%',
},
}),
).toBe(true);
expect(() =>
validateTokens({
'border-radius-one': {
$value: '100ps',
},
}),
).toThrowError(
'Tokens validation error: instance.tokens.border-radius-one.$value does not match pattern "\\\\d+(px|rem|%)"',
);
});
describe('colors', () => {
test('should have light and dark values defined', () => {
expect(() => {
validateTokens({
'color-button': {
$value: '#ffff',
},
});
}).toThrowError('Tokens validation error: instance.tokens.color-button.$value is not of a type(s) object');
});
test('accepts certain formats', () => {
expect(
validateTokens({
'color-button': {
$value: {
light: '#ffffff',
dark: 'rgba(0, 7, 22, 0.2)',
},
},
'color-container': {
$value: {
light: '#ffffff',
dark: 'transparent',
},
},
}),
).toBe(true);
['#fff', 'magenta', 'hsl(0, 100%, 50%)'].forEach((invalidValue) => {
expect(() =>
validateTokens({
'color-button': {
$value: {
light: invalidValue,
dark: invalidValue,
},
},
}),
).toThrowError(
'Tokens validation error: instance.tokens.color-button.$value.light does not match pattern "#[0-9a-f]{6}|rgba\\\\(\\\\d{1,3}%?(,\\\\s?\\\\d{1,3}%?){2},\\\\s?(1|0|0?\\\\.\\\\d+)\\\\)|transparent"',
);
});
});
});
describe('space', () => {
test('should have comfortable and compact values defined', () => {
expect(() => {
validateTokens({
'space-button': {
$value: '10px',
},
});
}).toThrowError('Tokens validation error: instance.tokens.space-button.$value is not of a type(s) object');
});
test('accepts certain formats', () => {
expect(
validateTokens({
'space-button': {
$value: {
comfortable: '100px',
compact: '0rem',
},
},
'space-alert': {
$value: {
comfortable: '100%',
compact: '50%',
},
},
}),
).toBe(true);
['100 px', '20ps', '100 %'].forEach((invalidValue) => {
expect(() =>
validateTokens({
'space-button': {
$value: {
comfortable: invalidValue,
compact: invalidValue,
},
},
}),
).toThrowError(
'Tokens validation error: instance.tokens.space-button.$value.comfortable does not match pattern "\\\\d+(px|rem|%)"',
);
});
});
});
describe('shadow', () => {
test('should have light and dark values defined', () => {
expect(() => {
validateTokens({
'shadow-button': {
$value: '#ffffff',
},
});
}).toThrowError('Tokens validation error: instance.tokens.shadow-button.$value is not of a type(s) object');
});
test('accepts shadow tokens', () => {
expect(
validateTokens({
'shadow-button': {
$value: {
light: 'any string',
dark: 'any string',
},
},
}),
).toBe(true);
});
});
describe('motion duration', () => {
test('should have default and disabled values defined', () => {
expect(() => {
validateTokens({
'motion-duration-button': {
$value: '10ms',
},
});
}).toThrowError(
'Tokens validation error: instance.tokens.motion-duration-button.$value is not of a type(s) object',
);
});
test('accepts certain formats', () => {
expect(
validateTokens({
'motion-duration-button': {
$value: {
default: '100ms',
disabled: '0s',
},
},
}),
).toBe(true);
['100 ms', '20ps'].forEach((invalidValue) => {
expect(() =>
validateTokens({
'motion-duration-button': {
$value: {
default: invalidValue,
disabled: invalidValue,
},
},
}),
).toThrowError(
'Tokens validation error: instance.tokens.motion-duration-button.$value.default does not match pattern "\\\\d+m?s"',
);
});
});
});
['motion-easing', 'motion-keyframes'].forEach((tokenType) => {
describe(tokenType, () => {
test('should have default and disabled values defined', () => {
expect(() => {
validateTokens({
[`${tokenType}-button`]: {
$value: 'any string',
},
});
}).toThrowError(
`Tokens validation error: instance.tokens.${tokenType}-button.$value is not of a type(s) object`,
);
});
test(`accepts ${tokenType} tokens`, () => {
expect(
validateTokens({
[`${tokenType}-button`]: {
$value: {
default: 'any string',
disabled: 'any string',
},
},
}),
).toBe(true);
});
});
});
describe('letter spacing', () => {
test('accepts positive, negative, and zero values with valid units', () => {
['1px', '-0.5rem', '0em', '0.25px', '-1.5em', '.5px', '-.5rem'].forEach((validValue) => {
expect(
validateTokens({
'letter-spacing-button': {
$value: validValue,
},
}),
).toBe(true);
});
});
test('accepts keyword values', () => {
['normal', 'inherit', 'initial', 'revert', 'revert-layer', 'unset'].forEach((validValue) => {
expect(
validateTokens({
'letter-spacing-button': {
$value: validValue,
},
}),
).toBe(true);
});
});
test('rejects invalid formats', () => {
['100', '-1', '1.5', '100 px', '20ps', '.px', '-.rem'].forEach((invalidValue) => {
expect(() =>
validateTokens({
'letter-spacing-button': {
$value: invalidValue,
},
}),
).toThrowError(
'Tokens validation error: instance.tokens.letter-spacing-button.$value does not match pattern "normal|inherit|initial|revert|revert-layer|unset|-?\\\\d*\\\\.?\\\\d+(px|rem|em)"',
);
});
});
});
});