shlink-web-client/test/settings/ShortUrlsListSettings.test.tsx

39 lines
1.7 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
2023-08-04 09:56:06 +03:00
import type { ShortUrlsListSettings as ShortUrlsSettings } from '../../shlink-web-component/src';
import type { ShortUrlsOrder } from '../../shlink-web-component/src/short-urls/data';
import { ShortUrlsListSettings } from '../../src/settings/ShortUrlsListSettings';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
describe('<ShortUrlsListSettings />', () => {
2023-05-27 12:57:26 +03:00
const setSettings = vi.fn();
const setUp = (shortUrlsList?: ShortUrlsSettings) => renderWithEvents(
<ShortUrlsListSettings settings={fromPartial({ shortUrlsList })} setShortUrlsListSettings={setSettings} />,
);
it.each([
[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'],
])('shows expected ordering', (shortUrlsList, expectedOrder) => {
setUp(shortUrlsList);
expect(screen.getByRole('button')).toHaveTextContent(expectedOrder);
});
it.each([
['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();
expect(setSettings).not.toHaveBeenCalled();
await user.click(screen.getByRole('button'));
await user.click(screen.getByRole('menuitem', { name }));
expect(setSettings).toHaveBeenCalledWith({ defaultOrdering: { field, dir } });
});
});