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

124 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-02-14 19:33:01 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
2021-08-15 19:21:36 +03:00
import { DropdownItem } from 'reactstrap';
import { ShortUrlCreationSettings as ShortUrlsSettings, Settings } from '../../src/settings/reducers/settings';
import { ShortUrlCreationSettings } from '../../src/settings/ShortUrlCreationSettings';
2022-03-05 16:43:43 +03:00
import { FormText } from '../../src/utils/forms/FormText';
2021-02-14 19:33:01 +03:00
import ToggleSwitch from '../../src/utils/ToggleSwitch';
import { DropdownBtn } from '../../src/utils/DropdownBtn';
2021-02-14 19:33:01 +03:00
describe('<ShortUrlCreationSettings />', () => {
2021-02-14 19:33:01 +03:00
let wrapper: ShallowWrapper;
const setShortUrlCreationSettings = jest.fn();
const createWrapper = (shortUrlCreation?: ShortUrlsSettings) => {
2021-02-14 19:33:01 +03:00
wrapper = shallow(
<ShortUrlCreationSettings
2021-02-14 19:33:01 +03:00
settings={Mock.of<Settings>({ shortUrlCreation })}
setShortUrlCreationSettings={setShortUrlCreationSettings}
/>,
);
return wrapper;
};
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
it.each([
2022-03-26 14:17:42 +03:00
[{ validateUrls: true }, true],
[{ validateUrls: false }, false],
[undefined, false],
])('URL validation switch is toggled if option is true', (shortUrlCreation, expectedChecked) => {
2021-02-14 19:33:01 +03:00
const wrapper = createWrapper(shortUrlCreation);
const urlValidationToggle = wrapper.find(ToggleSwitch).first();
2021-02-14 19:33:01 +03:00
expect(urlValidationToggle.prop('checked')).toEqual(expectedChecked);
2021-02-14 19:33:01 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[{ forwardQuery: true }, true],
[{ forwardQuery: false }, false],
[{}, true],
])('forward query switch is toggled if option is true', (shortUrlCreation, expectedChecked) => {
const wrapper = createWrapper({ validateUrls: true, ...shortUrlCreation });
const forwardQueryToggle = wrapper.find(ToggleSwitch).last();
expect(forwardQueryToggle.prop('checked')).toEqual(expectedChecked);
});
it.each([
2022-03-26 14:17:42 +03:00
[{ validateUrls: true }, '<b>Validate URL</b> checkbox will be <b>checked</b>'],
[{ validateUrls: false }, '<b>Validate URL</b> checkbox will be <b>unchecked</b>'],
[undefined, '<b>Validate URL</b> checkbox will be <b>unchecked</b>'],
])('shows expected helper text for URL validation', (shortUrlCreation, expectedText) => {
const wrapper = createWrapper(shortUrlCreation);
2022-03-05 16:04:01 +03:00
const validateUrlText = wrapper.find(FormText).first();
2022-03-07 13:17:40 +03:00
expect(validateUrlText.html()).toContain(expectedText);
});
it.each([
2022-03-26 14:17:42 +03:00
[{ forwardQuery: true }, '<b>Forward query params on redirect</b> checkbox will be <b>checked</b>'],
[{ forwardQuery: false }, '<b>Forward query params on redirect</b> checkbox will be <b>unchecked</b>'],
[{}, '<b>Forward query params on redirect</b> checkbox will be <b>checked</b>'],
])('shows expected helper text for query forwarding', (shortUrlCreation, expectedText) => {
const wrapper = createWrapper({ validateUrls: true, ...shortUrlCreation });
2022-03-05 16:04:01 +03:00
const forwardQueryText = wrapper.find(FormText).at(1);
2022-03-07 13:17:40 +03:00
expect(forwardQueryText.html()).toContain(expectedText);
});
it.each([
2022-03-26 14:17:42 +03:00
[{ tagFilteringMode: 'includes' } as ShortUrlsSettings, 'Suggest tags including input', 'including'],
[
{ tagFilteringMode: 'startsWith' } as ShortUrlsSettings,
'Suggest tags starting with input',
'starting with',
],
2022-03-26 14:17:42 +03:00
[undefined, 'Suggest tags starting with input', 'starting with'],
])('shows expected texts for tags suggestions', (shortUrlCreation, expectedText, expectedHint) => {
const wrapper = createWrapper(shortUrlCreation);
2022-03-05 16:04:01 +03:00
const hintText = wrapper.find(FormText).last();
const dropdown = wrapper.find(DropdownBtn);
expect(dropdown.prop('text')).toEqual(expectedText);
2022-03-07 13:17:40 +03:00
expect(hintText.html()).toContain(expectedHint);
});
2022-03-26 14:17:42 +03:00
it.each([[true], [false]])('invokes setShortUrlCreationSettings when URL validation toggle value changes', (validateUrls) => {
2021-02-14 19:33:01 +03:00
const wrapper = createWrapper();
const urlValidationToggle = wrapper.find(ToggleSwitch).first();
2021-02-14 19:33:01 +03:00
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
urlValidationToggle.simulate('change', validateUrls);
2021-02-14 19:33:01 +03:00
expect(setShortUrlCreationSettings).toHaveBeenCalledWith({ validateUrls });
});
2022-03-26 14:17:42 +03:00
it.each([[true], [false]])('invokes setShortUrlCreationSettings when forward query toggle value changes', (forwardQuery) => {
const wrapper = createWrapper();
const urlValidationToggle = wrapper.find(ToggleSwitch).last();
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
urlValidationToggle.simulate('change', forwardQuery);
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining({ forwardQuery }));
});
it('invokes setShortUrlCreationSettings when dropdown value changes', () => {
const wrapper = createWrapper();
const firstDropdownItem = wrapper.find(DropdownItem).first();
const secondDropdownItem = wrapper.find(DropdownItem).last();
2021-02-14 19:33:01 +03:00
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
firstDropdownItem.simulate('click');
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining(
{ tagFilteringMode: 'startsWith' },
));
secondDropdownItem.simulate('click');
expect(setShortUrlCreationSettings).toHaveBeenCalledWith(expect.objectContaining(
{ tagFilteringMode: 'includes' },
));
2021-02-14 19:33:01 +03:00
});
});