-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtext.test.js
More file actions
341 lines (251 loc) · 10.9 KB
/
text.test.js
File metadata and controls
341 lines (251 loc) · 10.9 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { mount } from '@vue/test-utils';
import { vi } from 'vitest';
import DtText, { resetHeadlineSemanticInfoFlag } from './text.vue';
import {
TEXT_LINE_CLAMP_CLASS,
TEXT_NUMERIC_CLASS,
TEXT_TRUNCATE_CLASS,
TEXT_TONE_MODIFIERS,
TEXT_WRAP_MODIFIERS,
TEXT_BOX_TRIM_MODIFIERS,
TEXT_STRENGTH_MODIFIERS,
TEXT_DENSITY_MODIFIERS,
} from './text_constants';
describe('DtText', () => {
const slotContent = 'Sample text';
const mountComponent = (props = {}, slots = {}) => {
return mount(DtText, {
props,
slots: {
default: slotContent,
...slots,
},
});
};
afterEach(() => {
vi.restoreAllMocks();
});
it('renders with d-text base class', () => {
const wrapper = mountComponent();
expect(wrapper.classes()).toContain('d-text');
});
it('renders slot content', () => {
const wrapper = mountComponent();
expect(wrapper.text()).toBe(slotContent);
});
it('applies typography modifier class for kind and size', () => {
const wrapper = mountComponent({ kind: 'headline', size: 'lg' });
expect(wrapper.classes()).toContain('d-text-headline--lg');
});
it('falls back to default size when invalid universal size provided', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ kind: 'headline', size: 'unknown' });
expect(wrapper.classes()).toContain('d-text-headline--md');
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('size="unknown"'));
});
it('warns when size is set without kind', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
mountComponent({ size: 'lg' });
expect(warnSpy).toHaveBeenCalledWith(
'[DtText] size prop has no effect without kind. Set kind="headline|body|label|code".',
);
});
it.each([
['body', '3xl'],
['label', '2xl'],
['code', 'xl'],
])('throws error for kind="%s" with headline-only size="%s"', (kind, size) => {
expect(() => mountComponent({ kind, size }))
.toThrow(`[DtText] size="${size}" is only valid for kind="headline"`);
});
it('allows headline-only sizes with headline kind', () => {
const wrapper = mountComponent({ kind: 'headline', size: '3xl' });
expect(wrapper.classes()).toContain('d-text-headline--3xl');
});
describe('When size is numeric', () => {
it('should apply the correct typography class for numeric size 200 with body kind', () => {
const wrapper = mountComponent({ kind: 'body', size: 200 });
expect(wrapper.classes()).toContain('d-text-body--sm');
});
});
it('applies truncate class when truncate prop is true', () => {
const wrapper = mountComponent({ truncate: true });
expect(wrapper.classes()).toContain(TEXT_TRUNCATE_CLASS);
});
it('applies numeric class when numeric prop is true', () => {
const wrapper = mountComponent({ numeric: true });
expect(wrapper.classes()).toContain(TEXT_NUMERIC_CLASS);
});
it('applies line clamp class and style when maxLines is provided', () => {
const wrapper = mountComponent({ maxLines: 3 });
expect(wrapper.classes()).toContain(TEXT_LINE_CLAMP_CLASS);
expect(wrapper.attributes('style')).toContain('--dt-text-line-clamp: 3');
});
it('applies tone modifier class', () => {
const wrapper = mountComponent({ tone: 'primary' });
expect(wrapper.classes()).toContain(TEXT_TONE_MODIFIERS.primary);
});
it('applies positive tone modifier class', () => {
const wrapper = mountComponent({ tone: 'positive' });
expect(wrapper.classes()).toContain(TEXT_TONE_MODIFIERS.positive);
});
it('warns and does not apply class for unrecognized tone', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ tone: 'not-real' });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported tone'));
expect(wrapper.classes()).not.toContain('d-text--tone-not-real');
});
it('applies align modifier class when align prop is valid', () => {
const wrapper = mountComponent({ align: 'center' });
expect(wrapper.classes()).toContain('d-text--align-center');
});
it('warns and does not apply class for unrecognized align', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ align: 'diagonal' });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported align "diagonal"'));
expect(wrapper.classes()).not.toContain('d-text--align-diagonal');
});
it('composes all modifier classes additively', () => {
const wrapper = mountComponent({
kind: 'headline',
size: 'lg',
strength: 'semibold',
density: 300,
tone: 'primary',
align: 'center',
wrap: 'balance',
textBoxTrim: 'both',
truncate: true,
numeric: true,
maxLines: 3,
});
expect(wrapper.classes()).toEqual(expect.arrayContaining([
'd-text',
'd-text-headline--lg',
TEXT_STRENGTH_MODIFIERS.semibold,
TEXT_DENSITY_MODIFIERS[300],
TEXT_TONE_MODIFIERS.primary,
'd-text--align-center',
TEXT_WRAP_MODIFIERS.balance,
TEXT_BOX_TRIM_MODIFIERS.both,
TEXT_TRUNCATE_CLASS,
TEXT_NUMERIC_CLASS,
TEXT_LINE_CLAMP_CLASS,
]));
expect(wrapper.attributes('style')).toContain('--dt-text-line-clamp: 3');
});
it('removes line clamp class and style when maxLines is cleared', async () => {
const wrapper = mountComponent({ maxLines: 2 });
await wrapper.setProps({ maxLines: null });
expect(wrapper.classes()).not.toContain(TEXT_LINE_CLAMP_CLASS);
expect(wrapper.attributes('style')).toBeUndefined();
});
it('maintains expected classes when mounted onto existing DOM (hydration-style)', () => {
const mountTarget = document.createElement('div');
mountTarget.innerHTML = '<span class="d-text"></span>';
document.body.appendChild(mountTarget);
const wrapper = mount(DtText, {
props: { kind: 'headline', size: 'md' },
slots: { default: slotContent },
attachTo: mountTarget,
});
expect(wrapper.classes()).toEqual(expect.arrayContaining(['d-text', 'd-text-headline--md']));
wrapper.unmount();
mountTarget.remove();
});
it('applies wrap modifier class', () => {
const wrapper = mountComponent({ wrap: 'balance' });
expect(wrapper.classes()).toContain(TEXT_WRAP_MODIFIERS.balance);
});
it('warns and does not apply class for unrecognized wrap', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ wrap: 'invalid-wrap' });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported wrap "invalid-wrap"'));
expect(wrapper.classes()).not.toContain('d-text--wrap-invalid-wrap');
});
it('applies textBoxTrim modifier class', () => {
const wrapper = mountComponent({ textBoxTrim: 'both' });
expect(wrapper.classes()).toContain(TEXT_BOX_TRIM_MODIFIERS.both);
});
it('warns and does not apply class for unrecognized textBoxTrim', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ textBoxTrim: 'invalid' });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported textBoxTrim "invalid"'));
expect(wrapper.classes()).not.toContain('d-text--trim-invalid');
});
it('has data-qa attribute', () => {
const wrapper = mountComponent();
expect(wrapper.attributes('data-qa')).toBe('dt-text');
});
describe('semantic heading info', () => {
beforeEach(() => {
resetHeadlineSemanticInfoFlag();
});
it('emits info when headline kind used without semantic heading element', () => {
const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
mountComponent({ kind: 'headline', as: 'span' });
expect(infoSpy).toHaveBeenCalledWith(
expect.stringContaining('Consider using as="h1|h2|h3|h4|h5|h6"'),
);
});
it('does not emit info when headline kind used with semantic heading element', () => {
const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
mountComponent({ kind: 'headline', as: 'h2' });
expect(infoSpy).not.toHaveBeenCalled();
});
it('emits info only once per session', () => {
const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
mountComponent({ kind: 'headline', as: 'div' });
mountComponent({ kind: 'headline', as: 'span' });
mountComponent({ kind: 'headline', as: 'p' });
expect(infoSpy).toHaveBeenCalledTimes(1);
});
it('does not emit info for non-headline kinds', () => {
const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
mountComponent({ kind: 'body' });
mountComponent({ kind: 'label' });
mountComponent({ kind: 'code' });
expect(infoSpy).not.toHaveBeenCalled();
});
});
describe('strength prop', () => {
it('applies strength modifier class', () => {
const wrapper = mountComponent({ strength: 'bold' });
expect(wrapper.classes()).toContain(TEXT_STRENGTH_MODIFIERS.bold);
});
it('warns and does not apply class for unrecognized strength', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ strength: 'invalid-strength' });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported strength "invalid-strength"'));
expect(wrapper.classes()).not.toContain('d-text--fw-invalid-strength');
});
it('does not add strength class when prop is omitted', () => {
const wrapper = mountComponent({});
const hasStrengthClass = wrapper.classes().some(c => c.includes('--fw-'));
expect(hasStrengthClass).toBe(false);
});
});
describe('density prop', () => {
it('applies density modifier class', () => {
const wrapper = mountComponent({ density: 300 });
expect(wrapper.classes()).toContain(TEXT_DENSITY_MODIFIERS[300]);
});
it('accepts density as string or number', () => {
const wrapperNumber = mountComponent({ density: 400 });
const wrapperString = mountComponent({ density: '400' });
expect(wrapperNumber.classes()).toContain(TEXT_DENSITY_MODIFIERS[400]);
expect(wrapperString.classes()).toContain(TEXT_DENSITY_MODIFIERS[400]);
});
it('warns and does not apply class for unrecognized density', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const wrapper = mountComponent({ density: 999 });
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unsupported density "999"'));
expect(wrapper.classes()).not.toContain('d-text--lh-999');
});
it('does not add density class when prop is omitted', () => {
const wrapper = mountComponent({});
const hasDensityClass = wrapper.classes().some(c => c.includes('--lh-'));
expect(hasDensityClass).toBe(false);
});
});
});