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

Commit 068420c

Browse files
committed
test(checkbox): refactor checkbox tests
1 parent e689731 commit 068420c

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed
Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import CCheckbox from '..'
2-
import { render, userEvent } from '@/tests/test-utils'
2+
import { render, userEvent, screen } from '@/tests/test-utils'
33

44
jest.mock('@/packages/chakra-ui-core/src/utils/generators.js', () => {
55
return {
@@ -13,7 +13,7 @@ const renderComponent = (props) => {
1313
const inlineAttrs = (props && props.inlineAttrs) || ''
1414
const base = {
1515
components: { CCheckbox },
16-
template: `<CCheckbox ${inlineAttrs}>checkbox</CCheckbox>`,
16+
template: `<CCheckbox ${inlineAttrs}>check</CCheckbox>`,
1717
...props
1818
}
1919
return render(base)
@@ -26,42 +26,42 @@ it('should render correctly', () => {
2626

2727
it('should display a disabled checkbox', () => {
2828
const inlineAttrs = 'isDisabled'
29-
const { getByText, container } = renderComponent({ inlineAttrs })
30-
const input = container.querySelector('input')
29+
renderComponent({ inlineAttrs })
30+
const input = screen.getByLabelText('check')
3131

3232
expect(input).toHaveAttribute('disabled')
33-
expect(getByText('checkbox').parentNode).toHaveStyle('cursor: not-allowed;')
33+
expect(screen.getByText('check').parentNode).toHaveStyle('cursor: not-allowed;')
3434
})
3535

3636
it('should display a checkbox with a checked state', () => {
3737
const inlineAttrs = 'isChecked'
38-
const { container } = renderComponent({ inlineAttrs })
39-
const input = container.querySelector('input')
38+
renderComponent({ inlineAttrs })
39+
const input = screen.getByLabelText('check')
4040

4141
expect(input).toBeChecked()
4242
})
4343

4444
it('should display a checkbox with an unchecked state', () => {
4545
const inlineAttrs = ':isChecked="false"'
46-
const { container } = renderComponent({ inlineAttrs })
47-
const input = container.querySelector('input')
46+
renderComponent({ inlineAttrs })
47+
const input = screen.getByLabelText('check')
4848

4949
expect(input).not.toBeChecked()
5050
})
5151

5252
it('should have a checked state when setting defaultIsChecked', () => {
5353
const inlineAttrs = 'defaultIsChecked'
54-
const { container } = renderComponent({ inlineAttrs })
55-
const input = container.querySelector('input')
54+
renderComponent({ inlineAttrs })
55+
const input = screen.getByLabelText('check')
5656

5757
expect(input).toBeChecked()
5858
})
5959

6060
test('Uncontrolled - should not check if disabled', async () => {
6161
const inlineAttrs = 'isDisabled'
62-
const { container, getByText } = renderComponent({ inlineAttrs })
63-
const input = container.querySelector('input')
64-
const checkbox = getByText('checkbox')
62+
renderComponent({ inlineAttrs })
63+
const input = screen.getByLabelText('check') // input
64+
const checkbox = screen.getByText('check') // div with text
6565

6666
expect(input).toBeDisabled()
6767

@@ -71,9 +71,9 @@ test('Uncontrolled - should not check if disabled', async () => {
7171
})
7272

7373
test('Uncontrolled - should check and uncheck', async () => {
74-
const { container, getByText } = renderComponent()
75-
const input = container.querySelector('input')
76-
const checkbox = getByText('checkbox')
74+
renderComponent()
75+
const input = screen.getByLabelText('check') // input
76+
const checkbox = screen.getByText('check') // div with text
7777

7878
// click the first time, it's checked
7979
await userEvent.click(checkbox)
@@ -85,23 +85,21 @@ test('Uncontrolled - should check and uncheck', async () => {
8585
})
8686

8787
test('properly handles v-model', async () => {
88-
const { getByText, container } = renderComponent(
89-
{
90-
data: () => ({
91-
enable: false
92-
}),
93-
template: `
88+
renderComponent({
89+
data: () => ({
90+
enable: false
91+
}),
92+
template: `
9493
<div>
9594
<span>{{enable ? 'enabled' : 'disabled'}}</span>
96-
<CCheckbox v-model="enable" />
95+
<CCheckbox v-model="enable">check</CCheckbox>
9796
</div>`
98-
}
99-
)
100-
const input = container.querySelector('input')
97+
})
98+
const input = screen.getByLabelText('check') // input
10199

102-
expect(getByText('disabled')).toBeInTheDocument()
100+
expect(screen.getByText('disabled')).toBeInTheDocument()
103101

104102
await userEvent.click(input)
105103

106-
expect(getByText('enabled')).toBeInTheDocument()
104+
expect(screen.getByText('enabled')).toBeInTheDocument()
107105
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ exports[`should render correctly 1`] = `
4646
class="css-np4rym"
4747
data-chakra-component="CBox"
4848
>
49-
checkbox
49+
check
5050
</div>
5151
</label>
5252
</DocumentFragment>

0 commit comments

Comments
 (0)