Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 99f38b2

Browse files
committed
test(switch): refactor use screen
1 parent 97a1349 commit 99f38b2

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed
Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,51 @@
11
import CSwitch from '..'
2-
import { render, userEvent } from '@/tests/test-utils'
2+
import { render, userEvent, screen } from '@/tests/test-utils'
33

4-
const renderComponent = (props) => {
4+
const renderComponent = ({ inlineAttrs = '', ...props } = { }) => {
55
const base = {
6+
template: `<CSwitch data-testid="switch" ${inlineAttrs} />`,
67
components: { CSwitch },
78
...props
89
}
910
return render(base)
1011
}
1112
it('should render correctly', () => {
12-
const { asFragment } = renderComponent(
13-
{
14-
template: `
15-
<CSwitch />
16-
`
17-
}
18-
)
13+
const { asFragment } = renderComponent()
1914
expect(asFragment()).toMatchSnapshot()
2015
})
2116

2217
it('should switch', async () => {
23-
const { getByTestId, getByRole } = renderComponent(
24-
{
25-
template: '<CSwitch data-testid="label" />'
26-
}
27-
)
28-
const label = getByTestId('label')
29-
const input = getByRole('checkbox')
30-
await userEvent.click(label)
31-
expect(input).toBeChecked()
18+
renderComponent()
19+
20+
await userEvent.click(screen.getByTestId('switch'))
21+
expect(screen.getByRole('checkbox')).toBeChecked()
3222
})
3323

3424
it('should emit a change event', async () => {
35-
const spy = jest.fn()
36-
const { getByTestId } = renderComponent(
37-
{
38-
methods: {
39-
handleChange: spy
40-
},
41-
template: '<CSwitch data-testid="label" @change="handleChange" /> '
42-
}
43-
)
25+
const onChange = jest.fn()
26+
const inlineAttrs = '@change="handleChange"'
27+
renderComponent({ inlineAttrs, methods: { handleChange: onChange } })
4428

45-
const label = getByTestId('label')
46-
await userEvent.click(label)
29+
await userEvent.click(screen.getByTestId('switch'))
4730

48-
expect(spy).toHaveBeenCalledTimes(1)
31+
expect(onChange).toHaveBeenCalledTimes(1)
4932
})
5033

5134
test('properly handles v-model', async () => {
52-
const { getByTestId, getByText } = renderComponent(
53-
{
54-
data: () => ({
55-
enable: false
56-
}),
57-
template: `
35+
renderComponent({
36+
data: () => ({
37+
enable: false
38+
}),
39+
template: `
5840
<div>
5941
<span>{{enable ? 'enabled' : 'disabled'}}</span>
60-
<CSwitch v-model="enable" data-testid="label" />
42+
<CSwitch v-model="enable" data-testid="switch" />
6143
</div>`
62-
}
63-
)
64-
const label = getByTestId('label')
44+
})
6545

66-
expect(getByText('disabled')).toBeInTheDocument()
46+
expect(screen.getByText('disabled')).toBeInTheDocument()
6747

68-
await userEvent.click(label)
48+
await userEvent.click(screen.getByTestId('switch'))
6949

70-
expect(getByText('enabled')).toBeInTheDocument()
50+
expect(screen.getByText('enabled')).toBeInTheDocument()
7151
})

packages/chakra-ui-core/src/CSwitch/tests/__snapshots__/CSwitch.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ exports[`should render correctly 1`] = `
55
<label
66
class="css-v7ibj3"
77
data-chakra-component="CSwitch"
8+
data-testid="switch"
89
>
910
<input
1011
class="css-f8n5zr"

0 commit comments

Comments
 (0)