2022-05-11 20:18:43 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2021-02-14 19:33:01 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-12-25 12:49:12 +03:00
|
|
|
import { ShortUrlCreationSettings as ShortUrlsSettings, Settings } from '../../src/settings/reducers/settings';
|
|
|
|
import { ShortUrlCreationSettings } from '../../src/settings/ShortUrlCreationSettings';
|
2021-02-14 19:33:01 +03:00
|
|
|
|
2021-12-25 12:49:12 +03:00
|
|
|
describe('<ShortUrlCreationSettings />', () => {
|
2021-02-14 19:33:01 +03:00
|
|
|
const setShortUrlCreationSettings = jest.fn();
|
2022-05-11 20:18:43 +03:00
|
|
|
const setUp = (shortUrlCreation?: ShortUrlsSettings) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
|
|
|
<ShortUrlCreationSettings
|
|
|
|
settings={Mock.of<Settings>({ shortUrlCreation })}
|
|
|
|
setShortUrlCreationSettings={setShortUrlCreationSettings}
|
|
|
|
/>,
|
|
|
|
),
|
|
|
|
});
|
2022-05-02 10:48:49 +03:00
|
|
|
|
2021-02-14 19:33:01 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|