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';
|
2021-02-14 19:33:01 +03:00
|
|
|
import { ShortUrlCreationSettings, Settings } from '../../src/settings/reducers/settings';
|
|
|
|
import { ShortUrlCreation } from '../../src/settings/ShortUrlCreation';
|
|
|
|
import ToggleSwitch from '../../src/utils/ToggleSwitch';
|
2021-08-15 19:13:13 +03:00
|
|
|
import { DropdownBtn } from '../../src/utils/DropdownBtn';
|
2021-02-14 19:33:01 +03:00
|
|
|
|
|
|
|
describe('<ShortUrlCreation />', () => {
|
|
|
|
let wrapper: ShallowWrapper;
|
|
|
|
const setShortUrlCreationSettings = jest.fn();
|
|
|
|
const createWrapper = (shortUrlCreation?: ShortUrlCreationSettings) => {
|
|
|
|
wrapper = shallow(
|
|
|
|
<ShortUrlCreation
|
|
|
|
settings={Mock.of<Settings>({ shortUrlCreation })}
|
|
|
|
setShortUrlCreationSettings={setShortUrlCreationSettings}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => wrapper?.unmount());
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[{ validateUrls: true }, true ],
|
|
|
|
[{ validateUrls: false }, false ],
|
|
|
|
[ undefined, false ],
|
2021-08-15 19:13:13 +03:00
|
|
|
])('URL validation switch is toggled if option is true', (shortUrlCreation, expectedChecked) => {
|
2021-02-14 19:33:01 +03:00
|
|
|
const wrapper = createWrapper(shortUrlCreation);
|
2021-10-14 00:10:22 +03:00
|
|
|
const urlValidationToggle = wrapper.find(ToggleSwitch).first();
|
2021-02-14 19:33:01 +03:00
|
|
|
|
2021-10-14 00:10:22 +03:00
|
|
|
expect(urlValidationToggle.prop('checked')).toEqual(expectedChecked);
|
2021-02-14 19:33:01 +03:00
|
|
|
});
|
|
|
|
|
2021-08-15 19:13:13 +03:00
|
|
|
it.each([
|
2021-10-14 00:10:22 +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([
|
|
|
|
[{ validateUrls: true }, 'Validate URL checkbox will be checked' ],
|
|
|
|
[{ validateUrls: false }, 'Validate URL checkbox will be unchecked' ],
|
|
|
|
[ undefined, 'Validate URL checkbox will be unchecked' ],
|
2021-08-15 19:13:13 +03:00
|
|
|
])('shows expected helper text for URL validation', (shortUrlCreation, expectedText) => {
|
|
|
|
const wrapper = createWrapper(shortUrlCreation);
|
2021-10-14 00:10:22 +03:00
|
|
|
const validateUrlText = wrapper.find('.form-text').first();
|
2021-08-15 19:13:13 +03:00
|
|
|
|
2021-10-14 00:10:22 +03:00
|
|
|
expect(validateUrlText.text()).toContain(expectedText);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[{ forwardQuery: true }, 'Forward query params on redirect checkbox will be checked' ],
|
|
|
|
[{ forwardQuery: false }, 'Forward query params on redirect checkbox will be unchecked' ],
|
|
|
|
[{}, 'Forward query params on redirect checkbox will be checked' ],
|
|
|
|
])('shows expected helper text for query forwarding', (shortUrlCreation, expectedText) => {
|
|
|
|
const wrapper = createWrapper({ validateUrls: true, ...shortUrlCreation });
|
|
|
|
const forwardQueryText = wrapper.find('.form-text').at(1);
|
|
|
|
|
|
|
|
expect(forwardQueryText.text()).toContain(expectedText);
|
2021-08-15 19:13:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[ { tagFilteringMode: 'includes' } as ShortUrlCreationSettings, 'Suggest tags including input', 'including' ],
|
|
|
|
[
|
|
|
|
{ tagFilteringMode: 'startsWith' } as ShortUrlCreationSettings,
|
|
|
|
'Suggest tags starting with input',
|
|
|
|
'starting with',
|
|
|
|
],
|
|
|
|
[ undefined, 'Suggest tags starting with input', 'starting with' ],
|
|
|
|
])('shows expected texts for tags suggestions', (shortUrlCreation, expectedText, expectedHint) => {
|
|
|
|
const wrapper = createWrapper(shortUrlCreation);
|
|
|
|
const hintText = wrapper.find('.form-text').last();
|
|
|
|
const dropdown = wrapper.find(DropdownBtn);
|
|
|
|
|
|
|
|
expect(dropdown.prop('text')).toEqual(expectedText);
|
|
|
|
expect(hintText.text()).toContain(expectedHint);
|
|
|
|
});
|
|
|
|
|
2021-10-14 00:10:22 +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();
|
2021-10-14 00:10:22 +03:00
|
|
|
const urlValidationToggle = wrapper.find(ToggleSwitch).first();
|
2021-02-14 19:33:01 +03:00
|
|
|
|
|
|
|
expect(setShortUrlCreationSettings).not.toHaveBeenCalled();
|
2021-10-14 00:10:22 +03:00
|
|
|
urlValidationToggle.simulate('change', validateUrls);
|
2021-02-14 19:33:01 +03:00
|
|
|
expect(setShortUrlCreationSettings).toHaveBeenCalledWith({ validateUrls });
|
|
|
|
});
|
|
|
|
|
2021-10-14 00:10:22 +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 }));
|
|
|
|
});
|
|
|
|
|
2021-08-15 19:13:13 +03:00
|
|
|
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
|
|
|
|
2021-08-15 19:13:13 +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
|
|
|
});
|
|
|
|
});
|