2022-07-17 12:50:26 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-07-16 09:47:10 +03:00
|
|
|
import type { NormalizedVisit } from '../../src/shlink-web-component/visits/types';
|
|
|
|
import type { VisitsTableProps } from '../../src/shlink-web-component/visits/VisitsTable';
|
|
|
|
import { VisitsTable } from '../../src/shlink-web-component/visits/VisitsTable';
|
2020-04-09 11:21:38 +03:00
|
|
|
import { rangeOf } from '../../src/utils/utils';
|
2022-07-17 12:50:26 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2020-04-09 11:21:38 +03:00
|
|
|
|
|
|
|
describe('<VisitsTable />', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const matchMedia = () => fromPartial<MediaQueryList>({ matches: false });
|
2023-05-27 12:57:26 +03:00
|
|
|
const setSelectedVisits = vi.fn();
|
2022-07-17 12:50:26 +03:00
|
|
|
const setUpFactory = (props: Partial<VisitsTableProps> = {}) => renderWithEvents(
|
|
|
|
<VisitsTable
|
|
|
|
visits={[]}
|
|
|
|
{...props}
|
|
|
|
matchMedia={matchMedia}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
const setUp = (visits: NormalizedVisit[], selectedVisits: NormalizedVisit[] = []) => setUpFactory(
|
2021-06-13 12:38:13 +03:00
|
|
|
{ visits, selectedVisits },
|
|
|
|
);
|
2022-12-23 23:06:59 +03:00
|
|
|
const setUpForOrphanVisits = (isOrphanVisits: boolean) => setUpFactory({ isOrphanVisits });
|
2022-07-17 12:50:26 +03:00
|
|
|
const setUpWithBots = () => setUpFactory({
|
2021-06-13 12:49:53 +03:00
|
|
|
visits: [
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial({ potentialBot: false, date: '2022-05-05' }),
|
|
|
|
fromPartial({ potentialBot: true, date: '2022-05-05' }),
|
2021-06-13 12:49:53 +03:00
|
|
|
],
|
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2022-12-23 23:06:59 +03:00
|
|
|
it('renders expected amount of columns', () => {
|
|
|
|
setUp([], []);
|
|
|
|
expect(screen.getAllByRole('columnheader')).toHaveLength(8);
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows warning when no visits are found', () => {
|
2022-07-17 12:50:26 +03:00
|
|
|
setUp([]);
|
|
|
|
expect(screen.getByText('No visits found with current filtering')).toBeInTheDocument();
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-07-17 12:50:26 +03:00
|
|
|
[50, 5, 1],
|
|
|
|
[21, 4, 1],
|
|
|
|
[30, 4, 1],
|
|
|
|
[60, 5, 1],
|
|
|
|
[115, 7, 2], // This one will have ellipsis
|
|
|
|
])('renders the expected amount of pages', (visitsCount, expectedAmountOfPageItems, expectedDisabledItems) => {
|
|
|
|
const { container } = setUp(
|
2023-04-13 23:47:13 +03:00
|
|
|
rangeOf(visitsCount, () => fromPartial<NormalizedVisit>({ browser: '', date: '2022-01-01', referer: '' })),
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(container.querySelectorAll('.page-item')).toHaveLength(expectedAmountOfPageItems);
|
|
|
|
expect(container.querySelectorAll('.disabled')).toHaveLength(expectedDisabledItems);
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each(
|
2022-03-26 14:17:42 +03:00
|
|
|
rangeOf(20, (value) => [value]),
|
2020-04-09 11:21:38 +03:00
|
|
|
)('does not render footer when there is only one page to render', (visitsCount) => {
|
2022-07-17 12:50:26 +03:00
|
|
|
const { container } = setUp(
|
2023-04-13 23:47:13 +03:00
|
|
|
rangeOf(visitsCount, () => fromPartial<NormalizedVisit>({ browser: '', date: '2022-01-01', referer: '' })),
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(container.querySelector('tfoot')).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByLabelText('pagination')).not.toBeInTheDocument();
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
it('selected rows are highlighted', async () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const visits = rangeOf(10, () => fromPartial<NormalizedVisit>({ browser: '', date: '2022-01-01', referer: '' }));
|
2022-07-17 12:50:26 +03:00
|
|
|
const { container, user } = setUp(visits, [visits[1], visits[2]]);
|
2020-04-09 11:21:38 +03:00
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
// Initial situation
|
|
|
|
expect(container.querySelectorAll('.table-active')).toHaveLength(2);
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
// Select one extra
|
2022-07-17 12:50:26 +03:00
|
|
|
await user.click(screen.getAllByRole('row')[5]);
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith([visits[1], visits[2], visits[4]]);
|
2020-04-10 12:59:53 +03:00
|
|
|
|
|
|
|
// Deselect one
|
2022-07-17 12:50:26 +03:00
|
|
|
await user.click(screen.getAllByRole('row')[3]);
|
2022-03-26 14:17:42 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith([visits[1]]);
|
2020-04-09 11:56:54 +03:00
|
|
|
|
|
|
|
// Select all
|
2022-07-17 12:50:26 +03:00
|
|
|
await user.click(screen.getAllByRole('columnheader')[0]);
|
2020-04-10 12:59:53 +03:00
|
|
|
expect(setSelectedVisits).toHaveBeenCalledWith(visits);
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
it('orders visits when column is clicked', async () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const { user } = setUp(rangeOf(9, (index) => fromPartial<NormalizedVisit>({
|
2020-04-10 12:59:53 +03:00
|
|
|
browser: '',
|
2022-07-17 12:50:26 +03:00
|
|
|
date: `2022-01-0${10 - index}`,
|
2020-04-09 11:21:38 +03:00
|
|
|
referer: `${index}`,
|
2020-04-10 12:59:53 +03:00
|
|
|
country: `Country_${index}`,
|
2020-04-09 11:21:38 +03:00
|
|
|
})));
|
2022-12-23 23:06:59 +03:00
|
|
|
const getFirstColumnValue = () => screen.getAllByRole('row')[2]?.querySelectorAll('td')[3]?.textContent;
|
2022-07-17 12:50:26 +03:00
|
|
|
const clickColumn = async (index: number) => user.click(screen.getAllByRole('columnheader')[index]);
|
|
|
|
|
|
|
|
expect(getFirstColumnValue()).toContain('Country_1');
|
2022-12-23 23:06:59 +03:00
|
|
|
await clickColumn(2); // Date column ASC
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(getFirstColumnValue()).toContain('Country_9');
|
2022-12-23 23:06:59 +03:00
|
|
|
await clickColumn(7); // Referer column - ASC
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(getFirstColumnValue()).toContain('Country_1');
|
2022-12-23 23:06:59 +03:00
|
|
|
await clickColumn(7); // Referer column - DESC
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(getFirstColumnValue()).toContain('Country_9');
|
2022-12-23 23:06:59 +03:00
|
|
|
await clickColumn(7); // Referer column - reset
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(getFirstColumnValue()).toContain('Country_1');
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
it('filters list when writing in search box', async () => {
|
|
|
|
const { user } = setUp([
|
2023-04-13 23:47:13 +03:00
|
|
|
...rangeOf(7, () => fromPartial<NormalizedVisit>({ browser: 'aaa', date: '2022-01-01', referer: 'aaa' })),
|
|
|
|
...rangeOf(2, () => fromPartial<NormalizedVisit>({ browser: 'bbb', date: '2022-01-01', referer: 'bbb' })),
|
2020-04-09 11:21:38 +03:00
|
|
|
]);
|
2022-07-17 12:50:26 +03:00
|
|
|
const searchField = screen.getByPlaceholderText('Search...');
|
|
|
|
const searchText = async (text: string) => {
|
|
|
|
await user.clear(searchField);
|
|
|
|
text.length > 0 && await user.type(searchField, text);
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(screen.getAllByRole('row')).toHaveLength(9 + 2);
|
|
|
|
await searchText('aa');
|
|
|
|
await waitFor(() => expect(screen.getAllByRole('row')).toHaveLength(7 + 2));
|
|
|
|
await searchText('bb');
|
|
|
|
await waitFor(() => expect(screen.getAllByRole('row')).toHaveLength(2 + 2));
|
|
|
|
await searchText('');
|
|
|
|
await waitFor(() => expect(screen.getAllByRole('row')).toHaveLength(9 + 2));
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|
2021-03-28 17:06:37 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-12-23 23:06:59 +03:00
|
|
|
[true, 9],
|
|
|
|
[false, 8],
|
|
|
|
])('displays proper amount of columns for orphan and non-orphan visits', (isOrphanVisits, expectedCols) => {
|
|
|
|
setUpForOrphanVisits(isOrphanVisits);
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(screen.getAllByRole('columnheader')).toHaveLength(expectedCols);
|
2021-03-28 17:06:37 +03:00
|
|
|
});
|
2021-06-13 12:49:53 +03:00
|
|
|
|
|
|
|
it('displays bots icon when a visit is a potential bot', () => {
|
2022-07-17 12:50:26 +03:00
|
|
|
setUpWithBots();
|
|
|
|
const [,, nonBotVisitRow, botVisitRow] = screen.getAllByRole('row');
|
2021-06-13 12:49:53 +03:00
|
|
|
|
2022-07-17 12:50:26 +03:00
|
|
|
expect(nonBotVisitRow.querySelectorAll('td')[1]).toBeEmptyDOMElement();
|
|
|
|
expect(botVisitRow.querySelectorAll('td')[1]).not.toBeEmptyDOMElement();
|
2021-06-13 12:49:53 +03:00
|
|
|
});
|
2020-04-09 11:21:38 +03:00
|
|
|
});
|