Skip to content

Commit 5d9be8d

Browse files
author
Eric Olkowski
committed
Added tests for ListItem
1 parent 3da3d94 commit 5d9be8d

File tree

6 files changed

+101
-32
lines changed

6 files changed

+101
-32
lines changed

packages/react-core/src/components/List/ListItem.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import styles from '@patternfly/react-styles/css/components/List/list';
22
import { css } from '@patternfly/react-styles';
33

44
export interface ListItemProps extends React.HTMLProps<HTMLLIElement> {
5-
/** Icon for the list item */
6-
icon?: React.ReactNode | null;
5+
/** Additional classes added to the list item */
6+
className?: string;
77
/** Anything that can be rendered inside of list item */
88
children: React.ReactNode;
9+
/** Icon for the list item */
10+
icon?: React.ReactNode | null;
911
}
1012

1113
export const ListItem: React.FunctionComponent<ListItemProps> = ({
12-
icon = null,
14+
className,
1315
children = null,
16+
icon = null,
1417
...props
1518
}: ListItemProps) => (
16-
<li className={css(icon && styles.listItem)} {...props}>
19+
<li className={css(icon && styles.listItem, className)} {...props}>
1720
{icon && <span className={css(styles.listItemIcon)}>{icon}</span>}
1821
<span className={icon && css(`${styles.list}__item-text`)}>{children}</span>
1922
</li>

packages/react-core/src/components/List/__tests__/Generated/ListItem.test.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/react-core/src/components/List/__tests__/Generated/__snapshots__/ListItem.test.tsx.snap

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/react-core/src/components/List/__tests__/List.test.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Shared tests between ol and ul lists', () => {
6464
expect(screen.getByRole('list')).toHaveAccessibleName('Testing stuff');
6565
});
6666

67-
test(`Renders with spread props for ${component} list`, () => {
67+
test(`Spreads additional props when passed for ${component} list`, () => {
6868
render(<List component={component} id="Test ID"></List>);
6969

7070
expect(screen.getByRole('list')).toHaveAttribute('id', 'Test ID');
@@ -91,10 +91,16 @@ describe('Shared tests between ol and ul lists', () => {
9191
});
9292

9393
describe('Ol component list', () => {
94+
test(`Renders with type of "1" by default`, () => {
95+
render(<List component={ListComponent.ol}></List>);
96+
97+
expect(screen.getByRole('list')).toHaveAttribute('type', '1');
98+
});
99+
94100
test(`Renders with type attribute when type is passed`, () => {
95-
render(<List component={ListComponent.ol} type={OrderType.lowercaseRomanNumber}></List>);
101+
render(<List component={ListComponent.ol} type={OrderType.uppercaseLetter}></List>);
96102

97-
expect(screen.getByRole('list')).toHaveAttribute('type', 'i');
103+
expect(screen.getByRole('list')).toHaveAttribute('type', 'A');
98104
});
99105
});
100106

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { render, screen } from '@testing-library/react';
2+
import { ListItem } from '../ListItem';
3+
import styles from '@patternfly/react-styles/css/components/List/list';
4+
5+
test('Renders with children', () => {
6+
render(<ListItem>List item content</ListItem>);
7+
8+
expect(screen.getByRole('listitem')).toHaveTextContent('List item content');
9+
});
10+
11+
test(`Does not render with a class by default`, () => {
12+
render(<ListItem>List item content</ListItem>);
13+
14+
expect(screen.getByRole('listitem')).not.toHaveClass();
15+
});
16+
17+
test(`Renders with custom class when className is passed`, () => {
18+
render(<ListItem className="test-class">List item content</ListItem>);
19+
20+
expect(screen.getByRole('listitem')).toHaveClass('test-class', { exact: true });
21+
});
22+
23+
test(`Renders with icon content when icon prop is passed`, () => {
24+
render(<ListItem icon={<div>Icon content</div>}>List item content</ListItem>);
25+
26+
expect(screen.getByRole('listitem')).toContainHTML('<div>Icon content</div>');
27+
});
28+
29+
test(`Renders with class ${styles.listItem} when icon prop is passed`, () => {
30+
render(<ListItem icon={<div>Icon content</div>}>List item content</ListItem>);
31+
32+
expect(screen.getByRole('listitem')).toHaveClass(styles.listItem, { exact: true });
33+
});
34+
35+
test(`Spreads additional props when passed`, () => {
36+
render(<ListItem id="test-ID">List item content</ListItem>);
37+
38+
expect(screen.getByRole('listitem')).toHaveAttribute('id', 'test-ID');
39+
});
40+
41+
test('Matches snapshot without icon', () => {
42+
const { asFragment } = render(<ListItem>List item content</ListItem>);
43+
44+
expect(asFragment()).toMatchSnapshot();
45+
});
46+
47+
test('Matches snapshot with icon', () => {
48+
const { asFragment } = render(<ListItem icon={<div>Icon content</div>}>List item content</ListItem>);
49+
50+
expect(asFragment()).toMatchSnapshot();
51+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Matches snapshot with icon 1`] = `
4+
<DocumentFragment>
5+
<li
6+
class="pf-v6-c-list__item"
7+
>
8+
<span
9+
class="pf-v6-c-list__item-icon"
10+
>
11+
<div>
12+
Icon content
13+
</div>
14+
</span>
15+
<span
16+
class="pf-v6-c-list__item-text"
17+
>
18+
List item content
19+
</span>
20+
</li>
21+
</DocumentFragment>
22+
`;
23+
24+
exports[`Matches snapshot without icon 1`] = `
25+
<DocumentFragment>
26+
<li
27+
class=""
28+
>
29+
<span>
30+
List item content
31+
</span>
32+
</li>
33+
</DocumentFragment>
34+
`;

0 commit comments

Comments
 (0)