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

Commit a1fad2a

Browse files
committed
test(radio): refactor use screen
1 parent b58b5f7 commit a1fad2a

File tree

1 file changed

+15
-17
lines changed

1 file changed

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

44
const renderComponent = (props) => {
55
const inlineAttrs = (props && props.inlineAttrs) || ''
@@ -18,52 +18,50 @@ it('should render correctly', () => {
1818

1919
it('should display a disabled radio', () => {
2020
const inlineAttrs = 'isDisabled'
21-
const { getByText, container } = renderComponent({ inlineAttrs })
22-
const input = container.querySelector('input')
21+
renderComponent({ inlineAttrs })
22+
const input = screen.getByLabelText('radio')
2323

2424
expect(input).toHaveAttribute('disabled')
25-
expect(getByText('radio').parentNode).toHaveStyle('cursor: not-allowed;')
25+
expect(input.parentNode).toHaveStyle('cursor: not-allowed;')
2626
})
2727

2828
it('should display a radio with a checked state', () => {
2929
const inlineAttrs = 'isChecked'
30-
const { container } = renderComponent({ inlineAttrs })
31-
const input = container.querySelector('input')
30+
renderComponent({ inlineAttrs })
31+
const input = screen.getByLabelText('radio')
3232

3333
expect(input).toBeChecked()
3434
})
3535

3636
it('should display a radio with an unchecked state', () => {
3737
const inlineAttrs = ':isChecked="false"'
38-
const { container } = renderComponent({ inlineAttrs })
39-
const input = container.querySelector('input')
38+
renderComponent({ inlineAttrs })
39+
const input = screen.getByLabelText('radio')
4040

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

4444
test('Uncontrolled - should not check if disabled', async () => {
4545
const inlineAttrs = 'isDisabled'
46-
const { container, getByText } = renderComponent({ inlineAttrs })
47-
const input = container.querySelector('input')
48-
const radio = getByText('radio')
46+
renderComponent({ inlineAttrs })
47+
const input = screen.getByLabelText('radio')
4948

5049
expect(input).toBeDisabled()
5150

52-
await userEvent.click(radio)
51+
await userEvent.click(input)
5352

5453
expect(input).not.toBeChecked()
5554
})
5655

5756
test('Uncontrolled - should be checked always', async () => {
58-
const { container, getByText } = renderComponent()
59-
const input = container.querySelector('input')
60-
const radio = getByText('radio')
57+
renderComponent()
58+
const input = screen.getByLabelText('radio')
6159

6260
// click the first time, it's checked
63-
await userEvent.click(radio)
61+
await userEvent.click(input)
6462
expect(input).toBeChecked()
6563

6664
// click the second time, it should be still checked
67-
await userEvent.click(radio)
65+
await userEvent.click(input)
6866
expect(input).toBeChecked()
6967
})

0 commit comments

Comments
 (0)