2022-07-10 00:03:21 +03:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
|
|
|
import type { SelectedServer } from '../../src/servers/data';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlsOrderableFields } from '../../src/short-urls/data';
|
|
|
|
import { SHORT_URLS_ORDERABLE_FIELDS } from '../../src/short-urls/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlsList } from '../../src/short-urls/reducers/shortUrlsList';
|
|
|
|
import { ShortUrlsTable as shortUrlsTableCreator } from '../../src/short-urls/ShortUrlsTable';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2020-12-07 13:17:19 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlsTable />', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const shortUrlsList = fromPartial<ShortUrlsList>({});
|
2023-05-27 12:57:26 +03:00
|
|
|
const orderByColumn = vi.fn();
|
2022-07-05 21:46:31 +03:00
|
|
|
const ShortUrlsTable = shortUrlsTableCreator(() => <span>ShortUrlsRow</span>);
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (server: SelectedServer = null) => renderWithEvents(
|
|
|
|
<ShortUrlsTable shortUrlsList={shortUrlsList} selectedServer={server} orderByColumn={() => orderByColumn} />,
|
|
|
|
);
|
2021-03-05 16:20:49 +03:00
|
|
|
|
2020-12-07 13:17:19 +03:00
|
|
|
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', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ version: '2.0.0' }));
|
2022-07-05 21:46:31 +03:00
|
|
|
|
|
|
|
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
|
|
|
});
|