-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathButtonGroup.test.tsx
More file actions
114 lines (98 loc) · 3.56 KB
/
Copy pathButtonGroup.test.tsx
File metadata and controls
114 lines (98 loc) · 3.56 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
import {render, cleanup} from '@testing-library/react'
import '@testing-library/jest-dom'
import {ButtonGroup} from './ButtonGroup'
import {Button} from '../Button'
import {axe, toHaveNoViolations} from 'jest-axe'
expect.extend(toHaveNoViolations)
describe('ButtonGroup', () => {
afterEach(cleanup)
it('renders correctly into the document', () => {
const expectedClass = 'ButtonGroup'
const expectedTag = 'section'
const mockTestId = 'test'
const {getByTestId} = render(
<ButtonGroup data-testid={mockTestId}>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const buttonGroupEl = getByTestId(mockTestId)
expect(buttonGroupEl.tagName).toBe(expectedTag.toUpperCase())
expect(buttonGroupEl.classList).toContain(expectedClass)
})
it('forwards a custom className alongside the default class', () => {
const {getByTestId} = render(
<ButtonGroup data-testid="test" className="custom-button-group">
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const buttonGroupEl = getByTestId('test')
expect(buttonGroupEl).toHaveClass('ButtonGroup')
expect(buttonGroupEl).toHaveClass('custom-button-group')
})
it('renders buttons with the correct element type when buttonAs is set', () => {
const expectedTag = 'a'
const {getAllByRole} = render(
<ButtonGroup buttonsAs={expectedTag}>
<Button href="#">Primary Action</Button>
<Button href="#">Secondary Action</Button>
</ButtonGroup>,
)
const buttonEl = getAllByRole('link')[0]
expect(buttonEl.tagName).toBe(expectedTag.toUpperCase())
})
it('renders buttons with the correct size class when buttonSize is set', () => {
const expectedClass = 'Button--size-large'
const {getAllByRole} = render(
<ButtonGroup buttonSize={'large'}>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const buttonEl = getAllByRole('button')[0]
expect(buttonEl.classList).toContain(expectedClass)
})
it('applies primary variant automatically to the first button and subtle variant to second', () => {
const {getAllByRole} = render(
<ButtonGroup>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const buttons = getAllByRole('button')
expect(buttons[0].classList).toContain('Button--primary')
expect(buttons[1].classList).toContain('Button--subtle')
})
it('does not render arrows on buttons by default', () => {
const {container} = render(
<ButtonGroup>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const arrows = container.querySelectorAll('svg')
expect(arrows).toHaveLength(0)
})
it('allows variant to be overridden via child props', () => {
const {getAllByRole} = render(
<ButtonGroup>
<Button variant="primary">Primary Action</Button>
<Button variant="secondary">Secondary Action</Button>
</ButtonGroup>,
)
const buttons = getAllByRole('button')
expect(buttons[0].classList).toContain('Button--primary')
expect(buttons[1].classList).toContain('Button--secondary')
})
it('has no axe violations', async () => {
const {container} = render(
<ButtonGroup>
<Button>Primary Action</Button>
<Button>Secondary Action</Button>
</ButtonGroup>,
)
const results = await axe(container)
expect(results).toHaveNoViolations()
})
})