2023-08-04 23:59:33 +03:00
|
|
|
import type { ShortUrlsListSettings as ShortUrlsSettings } from '@shlinkio/shlink-web-component';
|
2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 09:23:48 +03:00
|
|
|
import type { ShortUrlsOrder } from '../../shlink-web-component/src/short-urls/data';
|
2021-12-25 12:49:12 +03:00
|
|
|
import { ShortUrlsListSettings } from '../../src/settings/ShortUrlsListSettings';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-12-24 16:15:28 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<ShortUrlsListSettings />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const setSettings = vi.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (shortUrlsList?: ShortUrlsSettings) => renderWithEvents(
|
2023-04-13 22:48:29 +03:00
|
|
|
<ShortUrlsListSettings settings={fromPartial({ shortUrlsList })} setShortUrlsListSettings={setSettings} />,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
2021-12-24 16:15:28 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-06-06 23:38:07 +03:00
|
|
|
[undefined, 'Order by: Created at - DESC'],
|
|
|
|
[{}, 'Order by: Created at - DESC'],
|
|
|
|
[{ defaultOrdering: {} }, 'Order by...'],
|
|
|
|
[{ defaultOrdering: { field: 'longUrl', dir: 'DESC' } as ShortUrlsOrder }, 'Order by: Long URL - DESC'],
|
|
|
|
[{ defaultOrdering: { field: 'visits', dir: 'ASC' } as ShortUrlsOrder }, 'Order by: Visits - ASC'],
|
2021-12-24 16:15:28 +03:00
|
|
|
])('shows expected ordering', (shortUrlsList, expectedOrder) => {
|
2022-06-06 23:38:07 +03:00
|
|
|
setUp(shortUrlsList);
|
|
|
|
expect(screen.getByRole('button')).toHaveTextContent(expectedOrder);
|
2021-12-24 16:15:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-06-06 23:38:07 +03:00
|
|
|
['Clear selection', undefined, undefined],
|
|
|
|
['Long URL', 'longUrl', 'ASC'],
|
|
|
|
['Visits', 'visits', 'ASC'],
|
|
|
|
['Title', 'title', 'ASC'],
|
|
|
|
])('invokes setSettings when ordering changes', async (name, field, dir) => {
|
|
|
|
const { user } = setUp();
|
2021-12-24 16:15:28 +03:00
|
|
|
|
|
|
|
expect(setSettings).not.toHaveBeenCalled();
|
2022-06-06 23:38:07 +03:00
|
|
|
await user.click(screen.getByRole('button'));
|
|
|
|
await user.click(screen.getByRole('menuitem', { name }));
|
2021-12-24 16:15:28 +03:00
|
|
|
expect(setSettings).toHaveBeenCalledWith({ defaultOrdering: { field, dir } });
|
|
|
|
});
|
|
|
|
});
|