1
1
import CCheckbox from '..'
2
- import { render , userEvent } from '@/tests/test-utils'
2
+ import { render , userEvent , screen } from '@/tests/test-utils'
3
3
4
4
jest . mock ( '@/packages/chakra-ui-core/src/utils/generators.js' , ( ) => {
5
5
return {
@@ -13,7 +13,7 @@ const renderComponent = (props) => {
13
13
const inlineAttrs = ( props && props . inlineAttrs ) || ''
14
14
const base = {
15
15
components : { CCheckbox } ,
16
- template : `<CCheckbox ${ inlineAttrs } >checkbox </CCheckbox>` ,
16
+ template : `<CCheckbox ${ inlineAttrs } >check </CCheckbox>` ,
17
17
...props
18
18
}
19
19
return render ( base )
@@ -26,42 +26,42 @@ it('should render correctly', () => {
26
26
27
27
it ( 'should display a disabled checkbox' , ( ) => {
28
28
const inlineAttrs = 'isDisabled'
29
- const { getByText , container } = renderComponent ( { inlineAttrs } )
30
- const input = container . querySelector ( 'input ')
29
+ renderComponent ( { inlineAttrs } )
30
+ const input = screen . getByLabelText ( 'check ')
31
31
32
32
expect ( input ) . toHaveAttribute ( 'disabled' )
33
- expect ( getByText ( 'checkbox ' ) . parentNode ) . toHaveStyle ( 'cursor: not-allowed;' )
33
+ expect ( screen . getByText ( 'check ' ) . parentNode ) . toHaveStyle ( 'cursor: not-allowed;' )
34
34
} )
35
35
36
36
it ( 'should display a checkbox with a checked state' , ( ) => {
37
37
const inlineAttrs = 'isChecked'
38
- const { container } = renderComponent ( { inlineAttrs } )
39
- const input = container . querySelector ( 'input ')
38
+ renderComponent ( { inlineAttrs } )
39
+ const input = screen . getByLabelText ( 'check ')
40
40
41
41
expect ( input ) . toBeChecked ( )
42
42
} )
43
43
44
44
it ( 'should display a checkbox with an unchecked state' , ( ) => {
45
45
const inlineAttrs = ':isChecked="false"'
46
- const { container } = renderComponent ( { inlineAttrs } )
47
- const input = container . querySelector ( 'input ')
46
+ renderComponent ( { inlineAttrs } )
47
+ const input = screen . getByLabelText ( 'check ')
48
48
49
49
expect ( input ) . not . toBeChecked ( )
50
50
} )
51
51
52
52
it ( 'should have a checked state when setting defaultIsChecked' , ( ) => {
53
53
const inlineAttrs = 'defaultIsChecked'
54
- const { container } = renderComponent ( { inlineAttrs } )
55
- const input = container . querySelector ( 'input ')
54
+ renderComponent ( { inlineAttrs } )
55
+ const input = screen . getByLabelText ( 'check ')
56
56
57
57
expect ( input ) . toBeChecked ( )
58
58
} )
59
59
60
60
test ( 'Uncontrolled - should not check if disabled' , async ( ) => {
61
61
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
65
65
66
66
expect ( input ) . toBeDisabled ( )
67
67
@@ -71,9 +71,9 @@ test('Uncontrolled - should not check if disabled', async () => {
71
71
} )
72
72
73
73
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
77
77
78
78
// click the first time, it's checked
79
79
await userEvent . click ( checkbox )
@@ -85,23 +85,21 @@ test('Uncontrolled - should check and uncheck', async () => {
85
85
} )
86
86
87
87
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 : `
94
93
<div>
95
94
<span>{{enable ? 'enabled' : 'disabled'}}</span>
96
- <CCheckbox v-model="enable" / >
95
+ <CCheckbox v-model="enable">check</CCheckbox >
97
96
</div>`
98
- }
99
- )
100
- const input = container . querySelector ( 'input' )
97
+ } )
98
+ const input = screen . getByLabelText ( 'check' ) // input
101
99
102
- expect ( getByText ( 'disabled' ) ) . toBeInTheDocument ( )
100
+ expect ( screen . getByText ( 'disabled' ) ) . toBeInTheDocument ( )
103
101
104
102
await userEvent . click ( input )
105
103
106
- expect ( getByText ( 'enabled' ) ) . toBeInTheDocument ( )
104
+ expect ( screen . getByText ( 'enabled' ) ) . toBeInTheDocument ( )
107
105
} )
0 commit comments