Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(List, ListItem): Improve accessibility for List and migrate tests #949

Merged
merged 13 commits into from
May 15, 2023
Merged
10 changes: 0 additions & 10 deletions src/components/List/ListActions/__tests__/ListActions.spec.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/components/List/ListActions/__tests__/ListActions.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ListActions } from '../ListActions';

describe('<ListActions>', () => {
it('should render ListActions', () => {
const view = render(<ListActions>action</ListActions>);
expect(view).toMatchSnapshot();

expect(screen.getByText('action')).toBeInTheDocument();
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<ListActions> should render ListActions 1`] = `
Object {
"asFragment": [Function],
"baseElement": <body>
<div>
<div
class="ListActions"
>
action
</div>
</div>
</body>,
"container": <div>
<div
class="ListActions"
>
action
</div>
</div>,
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;
58 changes: 0 additions & 58 deletions src/components/List/ListItem/__tests__/ListItem.spec.js

This file was deleted.

70 changes: 70 additions & 0 deletions src/components/List/ListItem/__tests__/ListItem.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { ListItem } from '../ListItem';
import { Text } from '../../../Text';

describe('ListItem component', () => {
it('should render ListItem correctly', () => {
const ref = React.createRef<HTMLLIElement>();
const wrapper = render(
<ListItem ref={ref}>
<Text>An item</Text>
</ListItem>
);

expect(wrapper.asFragment()).toMatchSnapshot();
});

it('should turn string items to Text', () => {
const wrapper = render(<ListItem>Simple string</ListItem>);

expect(wrapper.asFragment()).toMatchSnapshot();
expect(screen.getByText('Simple string')).toBeInTheDocument();
});
it('should not add clickable class when onClick is not defined', () => {
render(<ListItem>An item</ListItem>);

expect(screen.getByRole('presentation')).toHaveClass('ListItem');
});
it('should add clickable class when onClick is defined', () => {
const view = render(<ListItem onClick={jest.fn()}>An item</ListItem>);

expect(view.asFragment()).toMatchSnapshot();
expect(screen.getByRole('presentation')).toHaveClass('ListItem ListItem--clickable');
});
it('should call onClick function when clicked', async () => {
const onClick = jest.fn();
render(<ListItem onClick={onClick}>An item</ListItem>);

await userEvent.click(screen.getByRole('presentation'));
expect(onClick).toHaveBeenCalledTimes(1);
});
it('should render correctly when disabled', () => {
const wrapper = render(<ListItem disabled>An item</ListItem>);
expect(wrapper.asFragment()).toMatchSnapshot();

const li = screen.getByRole('presentation');
expect(li).toHaveClass('ListItem ListItem--disabled');
});
it('should set disabled prop on li when required', () => {
const view = render(
<ListItem disabled passDisabledToLi>
An item
</ListItem>
);
expect(view.asFragment()).toMatchSnapshot();

const li = screen.getByRole('presentation');
expect(li).toHaveClass('ListItem--disabled');
expect(li).toHaveAttribute('disabled', '');

view.rerender(
<ListItem disabled={false} passDisabledToLi>
An item
</ListItem>
);
expect(screen.getByRole('presentation')).not.toHaveClass('ListItem--disabled');
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ListItem component should add clickable class when onClick is defined 1`] = `
<DocumentFragment>
<li
class="ListItem ListItem--clickable"
role="presentation"
>
<span>
An item
</span>
</li>
</DocumentFragment>
`;

exports[`ListItem component should render ListItem correctly 1`] = `
<DocumentFragment>
<li
class="ListItem"
role="presentation"
>
<p>
An item
</p>
</li>
</DocumentFragment>
`;

exports[`ListItem component should render correctly when disabled 1`] = `
<DocumentFragment>
<li
class="ListItem ListItem--disabled"
role="presentation"
>
<span>
An item
</span>
</li>
</DocumentFragment>
`;

exports[`ListItem component should set disabled prop on li when required 1`] = `
<DocumentFragment>
<li
class="ListItem ListItem--disabled"
disabled=""
role="presentation"
>
<span>
An item
</span>
</li>
</DocumentFragment>
`;

exports[`ListItem component should turn string items to Text 1`] = `
<DocumentFragment>
<li
class="ListItem"
role="presentation"
>
<span>
Simple string
</span>
</li>
</DocumentFragment>
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { render } from 'enzyme';
import toJson from 'enzyme-to-json';
import { render } from '@testing-library/react';
import { ListOptimizer } from '../ListOptimizer';

describe('<ListOptimizer> that optimally renders list items', () => {
Expand All @@ -10,7 +9,7 @@ describe('<ListOptimizer> that optimally renders list items', () => {
{({ key, index, style }) => <span key={key} style={style}>{`Item ${index}`}</span>}
</ListOptimizer>
);
expect(toJson(wrapper)).toMatchSnapshot();
expect(wrapper.asFragment()).toMatchSnapshot();
});

it('should render part of list items due to middle height of block', () => {
Expand All @@ -19,14 +18,14 @@ describe('<ListOptimizer> that optimally renders list items', () => {
{({ key, index, style }) => <span key={key} style={style}>{`Item ${index}`}</span>}
</ListOptimizer>
);
expect(toJson(wrapper)).toMatchSnapshot();
expect(wrapper.asFragment()).toMatchSnapshot();
});
it('should render full list on maximum height', () => {
const wrapper = render(
<ListOptimizer defaultHeight={2000} defaultWidth={10} rowCount={20}>
{({ key, index, style }) => <span key={key} style={style}>{`Item ${index}`}</span>}
</ListOptimizer>
);
expect(toJson(wrapper)).toMatchSnapshot();
expect(wrapper.asFragment()).toMatchSnapshot();
});
});
Loading