-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCTABanner.test.tsx
More file actions
577 lines (482 loc) · 19.2 KB
/
Copy pathCTABanner.test.tsx
File metadata and controls
577 lines (482 loc) · 19.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import React, {render, cleanup} from '@testing-library/react'
import {createRef} from 'react'
import '@testing-library/jest-dom'
import {CTABanner} from './CTABanner'
import {axe, toHaveNoViolations} from 'jest-axe'
import {Button} from '../Button'
expect.extend(toHaveNoViolations)
describe('CTABanner', () => {
afterEach(cleanup)
it('no a11y violations', async () => {
const {container} = render(
<CTABanner>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Description>This is your description</CTABanner.Description>
</CTABanner>,
)
const results = await axe(container)
expect(results).toHaveNoViolations()
})
it('renders correctly into the document', () => {
const expectedClass = 'CTABanner'
const expectedCustomClass = 'custom-class'
const expectedTag = 'section'
const mockTestId = 'test'
const {getByTestId} = render(
<CTABanner data-testid={mockTestId} className={expectedCustomClass}>
<CTABanner.Heading as="h1">Where the most ambitious teams build great things</CTABanner.Heading>
<CTABanner.Description>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sapien sit ullamcorper id. Aliquam luctus sed
turpis felis nam pulvinar risus elementum.
</CTABanner.Description>
<CTABanner.ButtonGroup>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</CTABanner.ButtonGroup>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId)
expect(ctaBannerEl.tagName).toBe(expectedTag.toUpperCase())
expect(ctaBannerEl.classList).toContain(expectedClass)
expect(ctaBannerEl.classList).toContain(expectedCustomClass)
})
it('renders the correct default heading type', () => {
const expectedTag = 'h3'
const headingText = 'This is your heading'
const {getByText} = render(
<CTABanner>
<CTABanner.Heading>{headingText}</CTABanner.Heading>
</CTABanner>,
)
const ctaHeaderEl = getByText(headingText)
expect(ctaHeaderEl.tagName).toBe(expectedTag.toUpperCase())
})
it('renders the correct heading type when an override is used', () => {
const expectedTag = 'h1'
const headingText = 'This is your heading'
const {getByText} = render(
<CTABanner>
<CTABanner.Heading as={'h1'}>{headingText}</CTABanner.Heading>
</CTABanner>,
)
const ctaHeaderEl = getByText(headingText)
expect(ctaHeaderEl.tagName).toBe(expectedTag.toUpperCase())
})
it('renders the CTABanner default without border', () => {
const mockTestId = 'test'
const classToCheck = 'CTABanner--border'
const {getByTestId} = render(
<CTABanner data-testid={mockTestId}>
<CTABanner.Heading>{'This is your heading'}</CTABanner.Heading>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId).lastChild
expect(ctaBannerEl).not.toHaveClass(classToCheck)
})
it('renders the CTABanner with a border', () => {
const mockTestId = 'test'
const classToCheck = 'CTABanner-container--border'
const {getByTestId} = render(
<CTABanner hasBorder data-testid={mockTestId}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId).lastChild
expect(ctaBannerEl).toHaveClass(classToCheck)
})
it('renders the CTABanner with shadows', () => {
const mockTestId = 'test'
const classToCheck = 'CTABanner--shadow'
const {getByTestId} = render(
<CTABanner data-testid={mockTestId} hasShadow>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId)
expect(ctaBannerEl).toHaveClass(classToCheck)
})
it('renders the CTABanner with no shadows', () => {
const mockTestId = 'test'
const classToCheck = 'CTABanner--Shadow'
const {getByTestId} = render(
<CTABanner hasShadow={false} data-testid={mockTestId}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId).firstChild
expect(ctaBannerEl).not.toHaveClass(classToCheck)
})
it('provides an escape hatch to render a custom trailing component', () => {
const trailingText = 'Custom trailing'
const MockTrailingComponent = () => <div>{trailingText}</div>
const {getByText} = render(
<CTABanner trailingComponent={MockTrailingComponent}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Description>This is your description</CTABanner.Description>
</CTABanner>,
)
const elTrailing = getByText(trailingText)
expect(elTrailing).toBeInTheDocument()
})
it('provides a way to pass a background image', () => {
const mockImage = 'image.jpg'
const {getByTestId} = render(
<CTABanner backgroundImageSrc={mockImage} data-testid="root">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const rootElement = getByTestId('root')
const styles = rootElement.getAttribute('style') as string
const styleObject = styles
.split(';')
.map(style => style.split(':'))
.reduce((acc, [key, value]) => {
if (key && value) {
acc[key.trim()] = value.trim()
}
return acc
}, {})
expect(styleObject['--brand-CTABanner-background-image-src']).toBe(`url(${mockImage})`)
})
it('provides a way to pass responsive background images', () => {
const mockImages = {
narrow: 'narrow-image.jpg',
regular: 'regular-image.jpg',
wide: 'wide-image.jpg',
}
const {getByTestId} = render(
<CTABanner backgroundImageSrc={mockImages} data-testid="root">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const rootElement = getByTestId('root')
const styles = rootElement.getAttribute('style') as string
const styleObject = styles
.split(';')
.map(style => style.split(':'))
.reduce((acc, [key, value]) => {
if (key && value) {
acc[key.trim()] = value.trim()
}
return acc
}, {})
for (const [key, value] of Object.entries(mockImages)) {
expect(styleObject[`--brand-CTABanner-${key}-background-image-src`]).toBe(`url(${value})`)
}
})
it('provides a way to pass an optional background color', () => {
const mockColor = 'red'
const {getByTestId} = render(
<CTABanner backgroundColor={mockColor} data-testid="root">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const rootElement = getByTestId('root')
const styles = rootElement.getAttribute('style') as string
const styleObject = styles
.split(';')
.map(style => style.split(':'))
.reduce((acc, [key, value]) => {
if (key && value) {
acc[key.trim()] = value.trim()
}
return acc
}, {})
expect(styleObject['--brand-CTABanner-background-color']).toBe(mockColor)
})
it('provides a way to pass responsive background colors', () => {
const mockColors = {
narrow: 'red',
regular: 'yellow',
wide: 'blue',
}
const {getByTestId} = render(
<CTABanner backgroundColor={mockColors} data-testid="root">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const rootElement = getByTestId('root')
const styles = rootElement.getAttribute('style') as string
const styleObject = styles
.split(';')
.map(style => style.split(':'))
.reduce((acc, [key, value]) => {
if (key && value) {
acc[key.trim()] = value.trim()
}
return acc
}, {})
for (const [key, value] of Object.entries(mockColors)) {
expect(styleObject[`--brand-CTABanner-${key}-background-color`]).toBe(value)
}
})
it('applies the default variant by default', () => {
const mockTestId = 'test'
const {getByTestId} = render(
<CTABanner data-testid={mockTestId}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId)
expect(ctaBannerEl).toHaveClass('CTABanner--variant-default')
})
it('can optionally apply the balanced variant', () => {
const mockTestId = 'test'
const {getByTestId} = render(
<CTABanner variant="balanced" data-testid={mockTestId}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Image src="image.png" alt="test" />
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId)
expect(ctaBannerEl).toHaveClass('CTABanner--variant-balanced')
})
it('can optionally apply the minimal variant', () => {
const mockTestId = 'test'
const {getByTestId} = render(
<CTABanner variant="minimal" data-testid={mockTestId}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.ButtonGroup>
<Button>Action</Button>
</CTABanner.ButtonGroup>
</CTABanner>,
)
const ctaBannerEl = getByTestId(mockTestId)
expect(ctaBannerEl).toHaveClass('CTABanner--variant-minimal')
})
it.each(['default', 'balanced', 'minimal'] as const)(
'applies rounded corners to the %s variant by default',
variant => {
const {getByTestId} = render(
<CTABanner variant={variant} data-testid="test">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
{variant === 'balanced' && <CTABanner.Image src="image.png" alt="test" />}
{variant === 'minimal' && (
<CTABanner.ButtonGroup>
<Button>Action</Button>
</CTABanner.ButtonGroup>
)}
</CTABanner>,
)
expect(getByTestId('test').firstChild).toHaveClass('CTABanner-container--rounded')
},
)
it('can render CTABanner.Image in the default variant alongside other children', () => {
const {getByAltText} = render(
<CTABanner>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Image src="image.png" alt="test image" />
</CTABanner>,
)
const imageEl = getByAltText('test image')
expect(imageEl).toBeInTheDocument()
})
it('renders a two-column Grid layout in the balanced variant with an image', () => {
const {getByAltText, getByText} = render(
<CTABanner variant="balanced">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Image src="image.png" alt="test image" />
</CTABanner>,
)
const imageEl = getByAltText('test image')
const headingEl = getByText('This is your heading')
expect(imageEl).toBeInTheDocument()
expect(headingEl).toBeInTheDocument()
expect(imageEl.closest('[class*="CTABanner-grid-column--secondary"]')).toBeInTheDocument()
expect(headingEl.closest('[class*="CTABanner-grid-column--primary"]')).toBeInTheDocument()
})
it('renders a two-column Grid layout in the minimal variant with buttons in the side column', () => {
const {getByText} = render(
<CTABanner variant="minimal">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.ButtonGroup>
<Button>Action</Button>
</CTABanner.ButtonGroup>
</CTABanner>,
)
const headingEl = getByText('This is your heading')
const buttonEl = getByText('Action')
expect(headingEl.closest('[class*="CTABanner-grid-column--primary"]')).toBeInTheDocument()
expect(buttonEl.closest('[class*="CTABanner-grid-column--secondary"]')).toBeInTheDocument()
})
it('does not render CTABanner.Image in the minimal variant and logs a warning', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const {queryByAltText} = render(
<CTABanner variant="minimal">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Image src="image.png" alt="test image" />
</CTABanner>,
)
expect(queryByAltText('test image')).not.toBeInTheDocument()
expect(consoleWarnSpy).toHaveBeenCalledWith(
'CTABanner: Image child is not supported in minimal variant and will not be rendered.',
)
consoleWarnSpy.mockRestore()
})
it('logs a warning when the balanced variant is used without CTABanner.Image', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
render(
<CTABanner variant="balanced">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
expect(consoleWarnSpy).toHaveBeenCalledWith(
'CTABanner: The "balanced" variant requires a CTABanner.Image child to display the two-column layout correctly.',
)
consoleWarnSpy.mockRestore()
})
it('renders the hasGridLines outer border wrapper and container class', () => {
const {getByTestId} = render(
<CTABanner hasGridLines data-testid="test">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const sectionEl = getByTestId('test')
expect(sectionEl.parentElement).toHaveClass('CTABanner-outer-container--border')
expect(sectionEl.firstChild).toHaveClass('CTABanner-container--border-gridlines')
expect(sectionEl.firstChild).not.toHaveClass('CTABanner-container--rounded')
})
it('does not render the hasGridLines outer border wrapper by default', () => {
const {getByTestId} = render(
<CTABanner data-testid="test">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const sectionEl = getByTestId('test')
const outerWrapper = sectionEl.parentElement
expect(outerWrapper).not.toHaveClass('CTABanner-outer-container--border')
})
it('provides an escape hatch to render a custom leading component', () => {
const leadingText = 'Custom leading'
const MockLeadingComponent = () => <div>{leadingText}</div>
const {getByText} = render(
<CTABanner leadingComponent={MockLeadingComponent}>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Description>This is your description</CTABanner.Description>
</CTABanner>,
)
const elLeading = getByText(leadingText)
expect(elLeading).toBeInTheDocument()
})
it('renders leading component before heading and trailing component after children', () => {
const MockLeading = () => <div data-testid="leading">Leading</div>
const MockTrailing = () => <div data-testid="trailing">Trailing</div>
const {getByTestId} = render(
<CTABanner leadingComponent={MockLeading} trailingComponent={MockTrailing} data-testid="test">
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const contentEl = getByTestId('test').querySelector('[class*="CTABanner-content"]')
const children = Array.from(contentEl!.children)
const leadingIndex = children.findIndex(el => el.getAttribute('data-testid') === 'leading')
const trailingIndex = children.findIndex(el => el.getAttribute('data-testid') === 'trailing')
expect(leadingIndex).toBeLessThan(trailingIndex)
expect(leadingIndex).toBe(0)
})
it('renders CTABanner.Logo children inside a wrapper with the CTABanner-logo class', () => {
const {getByTestId} = render(
<CTABanner>
<CTABanner.Logo data-testid="logo">
<svg data-testid="logo-svg" />
</CTABanner.Logo>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const logoEl = getByTestId('logo')
expect(logoEl).toBeInTheDocument()
expect(logoEl.className).toMatch(/CTABanner-logo/)
expect(getByTestId('logo-svg')).toBeInTheDocument()
})
it('forwards a custom className on CTABanner.Logo alongside the default class', () => {
const {getByTestId} = render(
<CTABanner>
<CTABanner.Logo data-testid="logo" className="custom-logo">
<span>Logo</span>
</CTABanner.Logo>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
const logoEl = getByTestId('logo')
expect(logoEl).toHaveClass('custom-logo')
expect(logoEl.className).toMatch(/CTABanner-logo/)
})
it('forwards a ref on CTABanner.Logo to the underlying div element', () => {
const ref = createRef<HTMLDivElement>()
render(
<CTABanner>
<CTABanner.Logo ref={ref}>
<span>Logo</span>
</CTABanner.Logo>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
</CTABanner>,
)
expect(ref.current).toBeInstanceOf(HTMLDivElement)
expect(ref.current?.className).toMatch(/CTABanner-logo/)
})
it('renders CTABanner.Link as an anchor with the CTABanner-link class and forwards href', () => {
const {getByRole} = render(
<CTABanner>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Link href="https://example.com">Read more</CTABanner.Link>
</CTABanner>,
)
const linkEl = getByRole('link', {name: 'Read more'})
expect(linkEl).toBeInTheDocument()
expect(linkEl).toHaveAttribute('href', 'https://example.com')
expect(linkEl.className).toMatch(/CTABanner-link/)
})
it('forwards a custom className on CTABanner.Link alongside the default class', () => {
const {getByRole} = render(
<CTABanner>
<CTABanner.Heading>This is your heading</CTABanner.Heading>
<CTABanner.Link href="#" className="custom-link">
Read more
</CTABanner.Link>
</CTABanner>,
)
const linkEl = getByRole('link', {name: 'Read more'})
expect(linkEl).toHaveClass('custom-link')
expect(linkEl.className).toMatch(/CTABanner-link/)
})
it('renders CTABanner.Heading with size "3" in the default variant when no size prop is provided', () => {
const {getByText} = render(
<CTABanner>
<CTABanner.Heading>Default heading</CTABanner.Heading>
</CTABanner>,
)
const headingEl = getByText('Default heading')
expect(headingEl.className).toMatch(/Heading--3/)
})
it('auto-applies size "6" to CTABanner.Heading in the minimal variant when no size prop is provided', () => {
const {getByText} = render(
<CTABanner variant="minimal">
<CTABanner.Heading>Minimal heading</CTABanner.Heading>
</CTABanner>,
)
const headingEl = getByText('Minimal heading')
expect(headingEl.className).toMatch(/Heading--6/)
expect(headingEl.className).not.toMatch(/Heading--3/)
})
it('respects an explicit size prop on CTABanner.Heading in the minimal variant', () => {
const {getByText} = render(
<CTABanner variant="minimal">
<CTABanner.Heading size="2">Minimal heading</CTABanner.Heading>
</CTABanner>,
)
const headingEl = getByText('Minimal heading')
expect(headingEl.className).toMatch(/Heading--2/)
expect(headingEl.className).not.toMatch(/Heading--6/)
})
it('renders <b> children inside CTABanner.Heading for duotone emphasis', () => {
const {getByText} = render(
<CTABanner>
<CTABanner.Heading>
Where the most ambitious teams <b>build great things</b>
</CTABanner.Heading>
</CTABanner>,
)
const emphasizedText = getByText('build great things')
expect(emphasizedText).toBeInTheDocument()
expect(emphasizedText.tagName).toBe('B')
})
})