1
- import React from 'react' ;
2
-
3
1
import { renderWithTheme , theme } from '../../test/utils' ;
4
2
import { blockSizes } from '../common/system' ;
5
- import ListItem from './ListItem' ;
3
+ import { ListItem } from './ListItem' ;
6
4
7
5
const defaultSize = 'lg' ;
8
6
describe ( '<ListItem />' , ( ) => {
9
7
it ( 'renders ListItem' , ( ) => {
10
- const { container } = renderWithTheme ( < ListItem /> ) ;
11
- const listItem = container . firstChild ;
8
+ const { getByRole } = renderWithTheme ( < ListItem /> ) ;
9
+ const listItem = getByRole ( 'menuitem' ) ;
12
10
expect ( listItem ) . toBeInTheDocument ( ) ;
13
- expect ( listItem ) . toHaveAttribute ( 'aria-disabled' , 'false' ) ;
14
- expect ( ListItem . defaultProps . size ) . toBe ( defaultSize ) ;
11
+ expect ( listItem ) . not . toHaveAttribute ( 'aria-disabled' ) ;
15
12
} ) ;
16
13
it ( 'renders children' , ( ) => {
17
14
const textContent = 'Hi there!' ;
@@ -23,36 +20,36 @@ describe('<ListItem />', () => {
23
20
expect ( getByText ( textContent ) ) . toBeInTheDocument ( ) ;
24
21
} ) ;
25
22
it ( 'should have a default role of menuitem' , ( ) => {
26
- const { container } = renderWithTheme ( < ListItem /> ) ;
27
- const listItem = container . firstChild ;
23
+ const { getByRole } = renderWithTheme ( < ListItem /> ) ;
24
+ const listItem = getByRole ( 'menuitem' ) ;
28
25
expect ( listItem ) . toHaveAttribute ( 'role' , 'menuitem' ) ;
29
26
} ) ;
30
27
31
28
it ( 'should render with custom role' , ( ) => {
32
- const { container } = renderWithTheme ( < ListItem role = 'option' /> ) ;
33
- const listItem = container . firstChild ;
29
+ const { getByRole } = renderWithTheme ( < ListItem role = 'option' /> ) ;
30
+ const listItem = getByRole ( 'option' ) ;
34
31
expect ( listItem ) . toHaveAttribute ( 'role' , 'option' ) ;
35
32
} ) ;
36
33
37
34
// it('should have a tabIndex of -1 by default', () => {
38
- // const { container } = renderWithTheme(<ListItem role='option' />);
39
- // const listItem = container.firstChild ;
35
+ // const { getByRole } = renderWithTheme(<ListItem role='option' />);
36
+ // const listItem = getByRole('menuitem') ;
40
37
// expect(listItem).toHaveAttribute('tabIndex', '-1');
41
38
// });
42
39
describe ( 'prop: disabled' , ( ) => {
43
40
it ( 'should not trigger onClick callback' , ( ) => {
44
41
const clickHandler = jest . fn ( ) ;
45
- const { container } = renderWithTheme (
42
+ const { getByRole } = renderWithTheme (
46
43
< ListItem disabled onClick = { clickHandler } />
47
44
) ;
48
- const listItem = container . firstChild ;
45
+ const listItem = getByRole ( 'menuitem' ) as HTMLElement ;
49
46
listItem . click ( ) ;
50
47
expect ( clickHandler ) . not . toBeCalled ( ) ;
51
48
expect ( listItem ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
52
49
} ) ;
53
50
it ( 'renders with disabled styles ' , ( ) => {
54
- const { container } = renderWithTheme ( < ListItem disabled /> ) ;
55
- const listItem = container . firstChild ;
51
+ const { getByRole } = renderWithTheme ( < ListItem disabled /> ) ;
52
+ const listItem = getByRole ( 'menuitem' ) ;
56
53
expect ( listItem ) . toHaveStyleRule ( 'pointer-events' , 'none' ) ;
57
54
expect ( listItem ) . toHaveStyleRule ( 'color' , theme . materialTextDisabled ) ;
58
55
expect ( listItem ) . toHaveStyleRule (
@@ -64,10 +61,10 @@ describe('<ListItem />', () => {
64
61
describe ( 'prop: onClick' , ( ) => {
65
62
it ( 'should be called when clicked' , ( ) => {
66
63
const clickHandler = jest . fn ( ) ;
67
- const { container } = renderWithTheme (
64
+ const { getByRole } = renderWithTheme (
68
65
< ListItem onClick = { clickHandler } />
69
66
) ;
70
- const listItem = container . firstChild ;
67
+ const listItem = getByRole ( 'menuitem' ) as HTMLElement ;
71
68
listItem . click ( ) ;
72
69
expect ( clickHandler ) . toHaveBeenCalledTimes ( 1 ) ;
73
70
} ) ;
0 commit comments