Skip to content

Commit 10d90ed

Browse files
joshblackCopilot
authored andcommitted
refactor(test): update BranchName, Breadcrumbs, ButtonGroup, CheckboxGroup, CircleBadge, CircleOcticon tests to vitest (#6044)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 02fd737 commit 10d90ed

File tree

11 files changed

+75
-247
lines changed

11 files changed

+75
-247
lines changed

packages/react/jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ module.exports = {
3333
'<rootDir>/src/AnchoredOverlay/',
3434
'<rootDir>/src/Banner/',
3535
'<rootDir>/src/Blankslate/',
36+
'<rootDir>/src/BranchName/',
37+
'<rootDir>/src/Breadcrumbs/',
38+
'<rootDir>/src/ButtonGroup/',
39+
'<rootDir>/src/CheckboxGroup/',
40+
'<rootDir>/src/CircleBadge/',
41+
'<rootDir>/src/CircleOcticon/',
3642
'<rootDir>/src/DataTable/',
3743
'<rootDir>/src/FeatureFlags/',
3844
'<rootDir>/src/Select/',

packages/react/src/BranchName/__tests__/BranchName.test.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
import React from 'react'
22
import BranchName from '../BranchName'
3-
import {render, behavesAsComponent, checkExports} from '../../utils/testing'
43
import {render as HTMLRender} from '@testing-library/react'
5-
import axe from 'axe-core'
4+
import {describe, expect, it} from 'vitest'
65

76
describe('BranchName', () => {
8-
behavesAsComponent({
9-
Component: BranchName,
10-
options: {
11-
skipDisplayName: true,
12-
},
13-
})
14-
15-
checkExports('BranchName', {
16-
default: BranchName,
17-
})
18-
19-
it('should have no axe violations', async () => {
20-
const {container} = HTMLRender(<BranchName />)
21-
const results = await axe.run(container)
22-
expect(results).toHaveNoViolations()
23-
})
24-
257
it('renders an <a> by default', () => {
26-
expect(render(<BranchName />).type).toEqual('a')
8+
const {container} = HTMLRender(<BranchName />)
9+
expect(container.firstChild?.nodeName).toEqual('A')
2710
})
2811

2912
it('should support `className` on the outermost element', () => {
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
import React from 'react'
2-
import Breadcrumbs, {Breadcrumb} from '..'
3-
import {render, behavesAsComponent, checkExports} from '../../utils/testing'
2+
import Breadcrumbs from '..'
43
import {render as HTMLRender} from '@testing-library/react'
5-
import axe from 'axe-core'
4+
import {describe, expect, it} from 'vitest'
65

76
describe('Breadcrumbs', () => {
8-
behavesAsComponent({Component: Breadcrumbs, options: {skipAs: true}})
9-
10-
checkExports('Breadcrumbs', {
11-
default: Breadcrumbs,
12-
Breadcrumb,
13-
})
14-
157
it('should support `className` on the outermost element', () => {
168
expect(HTMLRender(<Breadcrumbs className={'test-class-name'} />).container.firstChild).toHaveClass(
179
'test-class-name',
1810
)
1911
})
2012

21-
it('should have no axe violations', async () => {
22-
const {container} = HTMLRender(<Breadcrumbs />)
23-
const results = await axe.run(container)
24-
expect(results).toHaveNoViolations()
25-
})
26-
2713
it('renders a <nav>', () => {
28-
expect(render(<Breadcrumbs />).type).toEqual('nav')
14+
const {container} = HTMLRender(<Breadcrumbs />)
15+
expect(container.firstChild?.nodeName).toEqual('NAV')
2916
})
3017
})
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import {render as HTMLRender} from '@testing-library/react'
2-
import axe from 'axe-core'
32
import React from 'react'
43
import Breadcrumbs from '..'
5-
import {behavesAsComponent, render} from '../../utils/testing'
4+
import {describe, expect, it} from 'vitest'
65

76
describe('Breadcrumbs.Item', () => {
8-
behavesAsComponent({Component: Breadcrumbs.Item})
9-
107
it('renders an <a> by default', () => {
11-
expect(render(<Breadcrumbs.Item />).type).toEqual('a')
12-
})
13-
14-
it('should have no axe violations', async () => {
158
const {container} = HTMLRender(<Breadcrumbs.Item />)
16-
const results = await axe.run(container)
17-
expect(results).toHaveNoViolations()
9+
expect(container.firstChild?.nodeName).toEqual('A')
1810
})
1911

2012
it('respects the "selected" prop', () => {
21-
expect(render(<Breadcrumbs.Item selected />)).toMatchSnapshot()
13+
const {container} = HTMLRender(<Breadcrumbs.Item selected />)
14+
expect(container.firstChild).toMatchSnapshot()
2215
})
2316
})
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`Breadcrumbs.Item respects the "selected" prop 1`] = `
3+
exports[`Breadcrumbs.Item > respects the "selected" prop 1`] = `
44
<a
55
aria-current="page"
6-
className="Item selected ItemSelected"
6+
class="_Item_tg8iv_12 selected _ItemSelected_tg8iv_53"
77
/>
88
`;

packages/react/src/ButtonGroup/ButtonGroup.test.tsx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
1-
import {Button} from '../Button'
21
import {render, screen} from '@testing-library/react'
3-
import axe from 'axe-core'
4-
import {behavesAsComponent} from '../utils/testing'
5-
import type {ButtonGroupProps} from './ButtonGroup'
62
import ButtonGroup from './ButtonGroup'
73
import React from 'react'
8-
9-
const TestButtonGroup = (props: ButtonGroupProps) => (
10-
<ButtonGroup {...props}>
11-
<Button>Button 1</Button>
12-
<Button>Button 2</Button>
13-
<Button>Button 3</Button>
14-
</ButtonGroup>
15-
)
4+
import {describe, expect, it} from 'vitest'
165

176
describe('ButtonGroup', () => {
18-
behavesAsComponent({
19-
Component: TestButtonGroup,
20-
options: {skipSx: true, skipAs: true},
21-
})
22-
237
it('should support `className` on the outermost element', () => {
248
const Element = () => <ButtonGroup className={'test-class-name'} />
259
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name')
@@ -30,12 +14,6 @@ describe('ButtonGroup', () => {
3014
expect(container.getByTestId('button-group').tagName).toBe('DIV')
3115
})
3216

33-
it('should have no axe violations', async () => {
34-
const {container} = render(<TestButtonGroup />)
35-
const results = await axe.run(container)
36-
expect(results).toHaveNoViolations()
37-
})
38-
3917
it('should respect role prop', () => {
4018
render(<ButtonGroup role="toolbar" />)
4119
expect(screen.getByRole('toolbar')).toBeInTheDocument()

packages/react/src/CheckboxGroup/CheckboxGroup.test.tsx

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,18 @@
11
import React from 'react'
22
import {render} from '@testing-library/react'
33
import {Checkbox, CheckboxGroup, FormControl} from '..'
4-
import {behavesAsComponent, checkExports} from '../utils/testing'
54
import userEvent from '@testing-library/user-event'
6-
import {CheckboxGroupContext} from '../CheckboxGroup'
5+
import {describe, expect, it, vi, beforeAll, afterAll} from 'vitest'
76

87
describe('CheckboxGroup', () => {
9-
const mockWarningFn = jest.fn()
8+
const mockWarningFn = vi.fn()
109

1110
beforeAll(() => {
12-
jest.spyOn(global.console, 'warn').mockImplementation(mockWarningFn)
11+
vi.spyOn(console, 'warn').mockImplementation(mockWarningFn)
1312
})
1413

1514
afterAll(() => {
16-
jest.clearAllMocks()
17-
})
18-
19-
behavesAsComponent({
20-
Component: CheckboxGroup,
21-
options: {skipAs: true, skipSx: true},
22-
toRender: () => (
23-
<CheckboxGroup>
24-
<CheckboxGroup.Label>Choices</CheckboxGroup.Label>
25-
<FormControl>
26-
<Checkbox value="one" />
27-
<FormControl.Label>Choice one</FormControl.Label>
28-
</FormControl>
29-
<FormControl>
30-
<Checkbox value="two" />
31-
<FormControl.Label>Choice two</FormControl.Label>
32-
</FormControl>
33-
<FormControl>
34-
<Checkbox value="three" />
35-
<FormControl.Label>Choice three</FormControl.Label>
36-
</FormControl>
37-
</CheckboxGroup>
38-
),
39-
})
40-
41-
checkExports('CheckboxGroup', {
42-
default: CheckboxGroup,
43-
CheckboxGroupContext,
15+
vi.restoreAllMocks()
4416
})
4517

4618
it('renders a disabled group of inputs', () => {
@@ -96,8 +68,8 @@ describe('CheckboxGroup', () => {
9668

9769
it('calls onChange handlers passed to CheckboxGroup and Checkbox', async () => {
9870
const user = userEvent.setup()
99-
const handleParentChange = jest.fn()
100-
const handleCheckboxChange = jest.fn()
71+
const handleParentChange = vi.fn()
72+
const handleCheckboxChange = vi.fn()
10173
const {getByLabelText} = render(
10274
<CheckboxGroup onChange={handleParentChange}>
10375
<CheckboxGroup.Label>Choices</CheckboxGroup.Label>
@@ -126,7 +98,7 @@ describe('CheckboxGroup', () => {
12698

12799
it('calls onChange handler on CheckboxGroup with selected values', async () => {
128100
const user = userEvent.setup()
129-
const handleParentChange = jest.fn()
101+
const handleParentChange = vi.fn()
130102
const {getByLabelText} = render(
131103
<CheckboxGroup onChange={handleParentChange}>
132104
<CheckboxGroup.Label>Choices</CheckboxGroup.Label>
Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,34 @@
11
import React from 'react'
22
import {CircleBadge} from '..'
33
import {CheckIcon} from '@primer/octicons-react'
4-
import {render, behavesAsComponent, checkExports} from '../utils/testing'
54
import {render as HTMLRender} from '@testing-library/react'
6-
import axe from 'axe-core'
5+
import {describe, expect, it} from 'vitest'
76

87
const imgInput = <img alt="Example" src="primer.jpg" />
98

109
describe('CircleBadge', () => {
11-
behavesAsComponent({
12-
Component: CircleBadge,
13-
toRender: () => <CircleBadge>{imgInput}</CircleBadge>,
14-
})
15-
16-
checkExports('CircleBadge', {
17-
default: CircleBadge,
18-
})
19-
20-
describe('CircleBadge.Icon', () => {
21-
behavesAsComponent({
22-
Component: CircleBadge.Icon,
23-
toRender: () => <CircleBadge.Icon icon={CheckIcon} />,
24-
})
25-
})
26-
27-
it('should have no axe violations', async () => {
28-
const {container} = HTMLRender(<CircleBadge variant="large" size={20} />)
29-
const results = await axe.run(container)
30-
expect(results).toHaveNoViolations()
31-
})
32-
3310
it('respects the inline prop', () => {
34-
expect(render(<CircleBadge inline />)).toMatchSnapshot()
11+
const {container} = HTMLRender(<CircleBadge inline />)
12+
expect(container.firstChild).toMatchSnapshot()
3513
})
3614

3715
it('respects the variant prop', () => {
38-
expect(render(<CircleBadge variant="large" />)).toMatchSnapshot()
16+
const {container} = HTMLRender(<CircleBadge variant="large" />)
17+
expect(container.firstChild).toMatchSnapshot()
3918
})
4019

4120
it('uses the size prop to override the variant prop', () => {
42-
expect(render(<CircleBadge variant="large" size={20} />)).toMatchSnapshot()
21+
const {container} = HTMLRender(<CircleBadge variant="large" size={20} />)
22+
expect(container.firstChild).toMatchSnapshot()
4323
})
4424

4525
it('applies title', () => {
46-
expect(
47-
render(
48-
<CircleBadge as="a" title="primer logo">
49-
{imgInput}
50-
</CircleBadge>,
51-
).props['title'],
52-
).toEqual('primer logo')
26+
const {container} = HTMLRender(
27+
<CircleBadge as="a" title="primer logo">
28+
{imgInput}
29+
</CircleBadge>,
30+
)
31+
expect(container.firstChild).toHaveAttribute('title', 'primer logo')
5332
})
5433

5534
it('preserves child class names', () => {
@@ -60,4 +39,11 @@ describe('CircleBadge', () => {
6039
)
6140
expect(getByRole('img')).toHaveClass('primer')
6241
})
42+
43+
describe('CircleBadge.Icon', () => {
44+
it('renders an icon', () => {
45+
const {container} = HTMLRender(<CircleBadge.Icon icon={CheckIcon} />)
46+
expect(container.firstChild).toBeInTheDocument()
47+
})
48+
})
6349
})

0 commit comments

Comments
 (0)