shlink-web-client/test/servers/ServersListGroup.test.tsx

56 lines
1.9 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
import { MemoryRouter } from 'react-router-dom';
2023-02-18 12:40:37 +03:00
import type { ServerWithId } from '../../src/servers/data';
2023-02-18 13:11:01 +03:00
import { ServersListGroup } from '../../src/servers/ServersListGroup';
2023-09-30 11:45:52 +03:00
import { checkAccessibility } from '../__helpers__/accessibility';
describe('<ServersListGroup />', () => {
const servers: ServerWithId[] = [
fromPartial({ name: 'foo', id: '123' }),
fromPartial({ name: 'bar', id: '456' }),
2020-12-20 14:17:12 +03:00
];
2023-09-30 11:45:52 +03:00
const setUp = (params: { servers?: ServerWithId[]; withChildren?: boolean; embedded?: boolean } = {}) => {
2020-12-20 14:17:12 +03:00
const { servers = [], withChildren = true, embedded } = params;
return render(
<MemoryRouter>
<ServersListGroup servers={servers} embedded={embedded}>
{withChildren ? 'The list of servers' : undefined}
</ServersListGroup>
</MemoryRouter>,
2020-12-20 14:17:12 +03:00
);
};
2023-09-30 11:45:52 +03:00
it('passes a11y checks', () => checkAccessibility(setUp()));
2020-12-20 14:17:12 +03:00
it('renders title', () => {
setUp({});
2023-09-30 11:45:52 +03:00
expect(screen.getByTestId('title')).toHaveTextContent('The list of servers');
});
2020-12-20 14:17:12 +03:00
it('does not render title when children is not provided', () => {
setUp({ withChildren: false });
2023-09-30 11:45:52 +03:00
expect(screen.queryByTestId('title')).not.toBeInTheDocument();
2020-12-20 14:17:12 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[servers],
2020-12-20 14:17:12 +03:00
[[]],
])('shows servers list', (servers) => {
setUp({ servers });
2023-09-30 11:45:52 +03:00
expect(screen.queryAllByTestId('list')).toHaveLength(servers.length ? 1 : 0);
expect(screen.queryAllByRole('link')).toHaveLength(servers.length);
});
2020-12-20 14:17:12 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[true, 'servers-list__list-group servers-list__list-group--embedded'],
[false, 'servers-list__list-group'],
[undefined, 'servers-list__list-group'],
2020-12-20 14:17:12 +03:00
])('renders proper classes for embedded', (embedded, expectedClasses) => {
setUp({ servers, embedded });
2023-09-30 11:45:52 +03:00
expect(screen.getByTestId('list')).toHaveAttribute('class', `${expectedClasses} list-group`);
2020-12-20 14:17:12 +03:00
});
});