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

53 lines
1.8 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { MemoryRouter } from 'react-router-dom';
2022-05-28 11:47:39 +03:00
import { ServersListGroup } from '../../src/servers/ServersListGroup';
2023-02-18 12:40:37 +03:00
import type { ServerWithId } from '../../src/servers/data';
describe('<ServersListGroup />', () => {
2020-12-20 14:17:12 +03:00
const servers = [
Mock.of<ServerWithId>({ name: 'foo', id: '123' }),
Mock.of<ServerWithId>({ name: 'bar', id: '456' }),
];
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
);
};
2020-12-20 14:17:12 +03:00
it('renders title', () => {
setUp({});
expect(screen.getByRole('heading')).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 });
expect(screen.queryByRole('heading')).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 });
expect(screen.queryAllByRole('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 });
expect(screen.getByRole('list')).toHaveAttribute('class', `${expectedClasses} list-group`);
2020-12-20 14:17:12 +03:00
});
});