2023-08-04 23:59:33 +03:00
|
|
|
import type { ShortUrlCreationSettings 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';
|
2021-12-25 12:49:12 +03:00
|
|
|
import { ShortUrlCreationSettings } from '../../src/settings/ShortUrlCreationSettings';
|
2023-09-30 11:45:52 +03:00
|
|
|
import { checkAccessibility } from '../__helpers__/accessibility';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-02-14 19:33:01 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<ShortUrlCreationSettings />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const setShortUrlCreationSettings = vi.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (shortUrlCreation?: ShortUrlsSettings) => renderWithEvents(
|
|
|
|
<ShortUrlCreationSettings
|
2023-04-13 22:48:29 +03:00
|
|
|
settings={fromPartial({ shortUrlCreation })}
|
2022-07-10 00:03:21 +03:00
|
|
|
setShortUrlCreationSettings={setShortUrlCreationSettings}
|
|
|
|
/>,
|
|
|
|
);
|
2022-05-02 10:48:49 +03:00
|
|
|
|
2023-09-30 11:45:52 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
2021-02-14 19:33:01 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[{ validateUrls: true }, true],
|
|
|
|
[{ validateUrls: false }, false],
|
|
|
|
[undefined, false],
|
2022-05-02 10:48:49 +03:00
|
|
|
])('URL validation switch has proper initial state', (shortUrlCreation, expectedChecked) => {
|
|
|
|
const matcher = /^Request validation on long URLs when creating new short URLs/;
|
|
|
|
|
|
|
|
setUp(shortUrlCreation);
|
|
|
|
|
|
|
|
const checkbox = screen.getByLabelText(matcher);
|
|
|
|
const label = screen.getByText(matcher);
|
|
|
|
|
|
|
|
if (expectedChecked) {
|
|
|
|
expect(checkbox).toBeChecked();
|
|
|
|
expect(label).toHaveTextContent('Validate URL checkbox will be checked');
|
|
|
|
expect(label).not.toHaveTextContent('Validate URL checkbox will be unchecked');
|
|
|
|
} else {
|
|
|
|
expect(checkbox).not.toBeChecked();
|
|
|
|
expect(label).toHaveTextContent('Validate URL checkbox will be unchecked');
|
|
|
|
expect(label).not.toHaveTextContent('Validate URL checkbox will be checked');
|
|
|
|
}
|
2021-02-14 19:33:01 +03:00
|
|
|
});
|
|
|
|
|
2021-08-15 19:13:13 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[{ forwardQuery: true }, true],
|
|
|
|
[{ forwardQuery: false }, false],
|
|
|
|
[{}, true],
|
2021-10-14 00:10:22 +03:00
|
|
|
])('forward query switch is toggled if option is true', (shortUrlCreation, expectedChecked) => {
|
2022-05-02 10:48:49 +03:00
|
|
|
const matcher = /^Make all new short URLs forward their query params to the long URL/;
|
|
|
|
|
|
|
|
setUp({ validateUrls: true, ...shortUrlCreation });
|
|
|
|
|
|
|
|
const checkbox = screen.getByLabelText(matcher);
|
|
|
|
const label = screen.getByText(matcher);
|
|
|
|
|
|
|
|
if (expectedChecked) {
|
|
|
|
expect(checkbox).toBeChecked();
|
|
|
|
expect(label).toHaveTextContent('Forward query params on redirect checkbox will be checked');
|
|
|
|
expect(label).not.toHaveTextContent('Forward query params on redirect checkbox will be unchecked');
|
|
|
|
} else {
|
|
|
|
expect(checkbox).not.toBeChecked();
|
|
|
|
expect(label).toHaveTextContent('Forward query params on redirect checkbox will be unchecked');
|
|
|
|
expect(label).not.toHaveTextContent('Forward query params on redirect checkbox will be checked');
|
|
|
|
}
|
2021-08-15 19:13:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[{ tagFilteringMode: 'includes' } as ShortUrlsSettings, 'Suggest tags including input', 'including'],
|
2021-08-15 19:13:13 +03:00
|
|
|
[
|
2021-12-25 12:49:12 +03:00
|
|
|
{ tagFilteringMode: 'startsWith' } as ShortUrlsSettings,
|
2021-08-15 19:13:13 +03:00
|
|
|
'Suggest tags starting with input',
|
|
|
|
'starting with',
|
|
|
|
],
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, 'Suggest tags starting with input', 'starting with'],
|
2021-08-15 19:13:13 +03:00
|
|
|
])('shows expected texts for tags suggestions', (shortUrlCreation, expectedText, expectedHint) => {
|
2022-05-02 10:48:49 +03:00
|
|
|
setUp(shortUrlCreation);
|
2021-08-15 19:13:13 +03:00
|
|
|
|
2022-05-02 10:48:49 +03:00
|
|
|
expect(screen.getByRole('button', { name: expectedText })).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(/^The list of suggested tags will contain those/)).toHaveTextContent(expectedHint);
|
2021-08-15 19:13:13 +03:00
|
|
|
});
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
it.each([[true], [false]])('invokes setShortUrlCreationSettings when URL validation toggle value changes', async (validateUrls) => {
|
|
|
|
const { user } = setUp({ validateUrls });
|
2021-02-14 19:33:01 +03:00
|
|
|
|
|
|
|
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByLabelText(/^Request validation on long URLs when creating new short URLs/));
|
2022-05-02 10:48:49 +03:00
|
|
|
expect(setShortUrlCreationSettings).toHaveBeenCalledWith({ validateUrls: !validateUrls });
|
2021-02-14 19:33:01 +03:00
|
|
|
});
|
|
|
|
|
2022-05-11 20:18:43 +03:00
|
|
|
it.each([[true], [false]])('invokes setShortUrlCreationSettings when forward query toggle value changes', async (forwardQuery) => {
|
|
|
|
const { user } = setUp({ validateUrls: true, forwardQuery });
|
2021-10-14 00:10:22 +03:00
|
|
|
|
|
|
|
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByLabelText(/^Make all new short URLs forward their query params to the long URL/));
|
2022-05-02 10:48:49 +03:00
|
|
|
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining({ forwardQuery: !forwardQuery }));
|
2021-10-14 00:10:22 +03:00
|
|
|
});
|
|
|
|
|
2022-05-02 10:48:49 +03:00
|
|
|
it('invokes setShortUrlCreationSettings when dropdown value changes', async () => {
|
2022-05-11 20:18:43 +03:00
|
|
|
const { user } = setUp();
|
2022-05-02 10:48:49 +03:00
|
|
|
const clickItem = async (name: string) => {
|
2022-05-11 20:18:43 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: 'Suggest tags starting with input' }));
|
|
|
|
await user.click(await screen.findByRole('menuitem', { name }));
|
2022-05-02 10:48:49 +03:00
|
|
|
};
|
2021-02-14 19:33:01 +03:00
|
|
|
|
2021-08-15 19:13:13 +03:00
|
|
|
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
|
|
|
|
|
2022-05-02 10:48:49 +03:00
|
|
|
await clickItem('Suggest tags including input');
|
2021-08-15 19:13:13 +03:00
|
|
|
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining(
|
2022-05-02 10:48:49 +03:00
|
|
|
{ tagFilteringMode: 'includes' },
|
2021-08-15 19:13:13 +03:00
|
|
|
));
|
|
|
|
|
2022-05-02 10:48:49 +03:00
|
|
|
await clickItem('Suggest tags starting with input');
|
2021-08-15 19:13:13 +03:00
|
|
|
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining(
|
2022-05-02 10:48:49 +03:00
|
|
|
{ tagFilteringMode: 'startsWith' },
|
2021-08-15 19:13:13 +03:00
|
|
|
));
|
2021-02-14 19:33:01 +03:00
|
|
|
});
|
|
|
|
});
|