2022-07-05 21:46:31 +03:00
|
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-12-07 13:17:19 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { ShortUrlsTable as shortUrlsTableCreator } from '../../src/short-urls/ShortUrlsTable';
|
|
|
|
import { ShortUrlsList } from '../../src/short-urls/reducers/shortUrlsList';
|
2021-03-05 16:20:49 +03:00
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2021-12-25 12:24:37 +03:00
|
|
|
import { ShortUrlsOrderableFields, SHORT_URLS_ORDERABLE_FIELDS } from '../../src/short-urls/data';
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsTable />', () => {
|
|
|
|
const shortUrlsList = Mock.all<ShortUrlsList>();
|
|
|
|
const orderByColumn = jest.fn();
|
2022-07-05 21:46:31 +03:00
|
|
|
const ShortUrlsTable = shortUrlsTableCreator(() => <span>ShortUrlsRow</span>);
|
|
|
|
const setUp = (server: SelectedServer = null) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
2021-03-05 16:20:49 +03:00
|
|
|
<ShortUrlsTable shortUrlsList={shortUrlsList} selectedServer={server} orderByColumn={() => orderByColumn} />,
|
2022-07-05 21:46:31 +03:00
|
|
|
),
|
|
|
|
});
|
2021-03-05 16:20:49 +03:00
|
|
|
|
2020-12-07 13:17:19 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
|
|
|
|
|
|
|
it('should render inner table by default', () => {
|
2022-07-05 21:46:31 +03:00
|
|
|
setUp();
|
|
|
|
expect(screen.getByRole('table')).toBeInTheDocument();
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|
|
|
|
|
2022-07-05 21:46:31 +03:00
|
|
|
it('should render row groups by default', () => {
|
|
|
|
setUp();
|
|
|
|
expect(screen.getAllByRole('rowgroup')).toHaveLength(2);
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render 6 table header cells by default', () => {
|
2022-07-05 21:46:31 +03:00
|
|
|
setUp();
|
|
|
|
expect(screen.getAllByRole('columnheader')).toHaveLength(6);
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|
|
|
|
|
2022-07-05 21:46:31 +03:00
|
|
|
it('should render table header cells without "order by" icon by default', () => {
|
|
|
|
setUp();
|
|
|
|
expect(screen.queryByRole('img', { hidden: true })).not.toBeInTheDocument();
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|
|
|
|
|
2021-03-05 16:20:49 +03:00
|
|
|
it('should render table header cells with conditional order by icon', () => {
|
2022-07-05 21:46:31 +03:00
|
|
|
setUp();
|
|
|
|
|
|
|
|
const getThElementForSortableField = (orderableField: string) => screen.getAllByRole('columnheader').find(
|
|
|
|
({ innerHTML }) => innerHTML.includes(SHORT_URLS_ORDERABLE_FIELDS[orderableField as ShortUrlsOrderableFields]),
|
|
|
|
);
|
2021-12-25 12:24:37 +03:00
|
|
|
const sortableFields = Object.keys(SHORT_URLS_ORDERABLE_FIELDS).filter((sortableField) => sortableField !== 'title');
|
2020-12-07 13:17:19 +03:00
|
|
|
|
2022-07-05 21:46:31 +03:00
|
|
|
expect.assertions(sortableFields.length * 2);
|
2020-12-07 13:17:19 +03:00
|
|
|
sortableFields.forEach((sortableField) => {
|
2022-07-05 21:46:31 +03:00
|
|
|
const element = getThElementForSortableField(sortableField);
|
|
|
|
|
|
|
|
expect(element).toBeDefined();
|
|
|
|
element && fireEvent.click(element);
|
2020-12-07 13:17:19 +03:00
|
|
|
expect(orderByColumn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-03-05 16:20:49 +03:00
|
|
|
|
2022-05-01 12:00:46 +03:00
|
|
|
it('should render composed title column', () => {
|
2022-07-05 21:46:31 +03:00
|
|
|
setUp(Mock.of<ReachableServer>({ version: '2.0.0' }));
|
|
|
|
|
|
|
|
const { innerHTML } = screen.getAllByRole('columnheader')[2];
|
2021-03-05 16:20:49 +03:00
|
|
|
|
2022-07-05 21:46:31 +03:00
|
|
|
expect(innerHTML).toContain('Title');
|
|
|
|
expect(innerHTML).toContain('Long URL');
|
2021-03-05 16:20:49 +03:00
|
|
|
});
|
2020-12-07 13:17:19 +03:00
|
|
|
});
|