mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Migrated EditDomainRedirectsModal to react testing library
This commit is contained in:
parent
33adb08105
commit
e53f90fc5c
2 changed files with 36 additions and 52 deletions
|
@ -38,7 +38,7 @@ export const EditDomainRedirectsModal: FC<EditDomainRedirectsModalProps> = (
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal isOpen={isOpen} toggle={toggle} centered>
|
<Modal isOpen={isOpen} toggle={toggle} centered>
|
||||||
<form onSubmit={handleSubmit}>
|
<form name="domainRedirectsModal" onSubmit={handleSubmit}>
|
||||||
<ModalHeader toggle={toggle}>Edit redirects for <b>{domain.domain}</b></ModalHeader>
|
<ModalHeader toggle={toggle}>Edit redirects for <b>{domain.domain}</b></ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
<FormGroup value={baseUrlRedirect} onChange={setBaseUrlRedirect}>
|
<FormGroup value={baseUrlRedirect} onChange={setBaseUrlRedirect}>
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
import { shallow, ShallowWrapper } from 'enzyme';
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||||
|
import userEvent from '@testing-library/user-event';
|
||||||
import { Mock } from 'ts-mockery';
|
import { Mock } from 'ts-mockery';
|
||||||
import { Button, ModalHeader } from 'reactstrap';
|
|
||||||
import { ShlinkDomain } from '../../../src/api/types';
|
import { ShlinkDomain } from '../../../src/api/types';
|
||||||
import { EditDomainRedirectsModal } from '../../../src/domains/helpers/EditDomainRedirectsModal';
|
import { EditDomainRedirectsModal } from '../../../src/domains/helpers/EditDomainRedirectsModal';
|
||||||
import { InfoTooltip } from '../../../src/utils/InfoTooltip';
|
|
||||||
|
|
||||||
describe('<EditDomainRedirectsModal />', () => {
|
describe('<EditDomainRedirectsModal />', () => {
|
||||||
let wrapper: ShallowWrapper;
|
|
||||||
const editDomainRedirects = jest.fn().mockResolvedValue(undefined);
|
const editDomainRedirects = jest.fn().mockResolvedValue(undefined);
|
||||||
const toggle = jest.fn();
|
const toggle = jest.fn();
|
||||||
const domain = Mock.of<ShlinkDomain>({
|
const domain = Mock.of<ShlinkDomain>({
|
||||||
|
@ -15,81 +13,67 @@ describe('<EditDomainRedirectsModal />', () => {
|
||||||
baseUrlRedirect: 'baz',
|
baseUrlRedirect: 'baz',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const setUp = () => ({
|
||||||
beforeEach(() => {
|
user: userEvent.setup(),
|
||||||
wrapper = shallow(
|
...render(
|
||||||
<EditDomainRedirectsModal domain={domain} isOpen toggle={toggle} editDomainRedirects={editDomainRedirects} />,
|
<EditDomainRedirectsModal domain={domain} isOpen toggle={toggle} editDomainRedirects={editDomainRedirects} />,
|
||||||
);
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(jest.clearAllMocks);
|
afterEach(jest.clearAllMocks);
|
||||||
afterEach(() => wrapper?.unmount());
|
|
||||||
|
|
||||||
it('renders domain in header', () => {
|
it('renders domain in header', () => {
|
||||||
const header = wrapper.find(ModalHeader);
|
setUp();
|
||||||
|
expect(screen.getByRole('heading')).toHaveTextContent('Edit redirects for foo.com');
|
||||||
expect(header.html()).toContain('foo.com');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('expected amount of form groups and tooltips', () => {
|
it('has different handlers to toggle the modal', async () => {
|
||||||
const formGroups = wrapper.find('FormGroup');
|
const { user } = setUp();
|
||||||
const tooltips = wrapper.find(InfoTooltip);
|
|
||||||
|
|
||||||
expect(formGroups).toHaveLength(3);
|
|
||||||
expect(tooltips).toHaveLength(3);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('has different handlers to toggle the modal', () => {
|
|
||||||
expect(toggle).not.toHaveBeenCalled();
|
expect(toggle).not.toHaveBeenCalled();
|
||||||
|
await user.click(screen.getByLabelText('Close'));
|
||||||
(wrapper.prop('toggle') as Function)();
|
await user.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||||
(wrapper.find(ModalHeader).prop('toggle') as Function)();
|
expect(toggle).toHaveBeenCalledTimes(2);
|
||||||
wrapper.find(Button).first().simulate('click');
|
|
||||||
|
|
||||||
expect(toggle).toHaveBeenCalledTimes(3);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('saves expected values when form is submitted', () => {
|
it('saves expected values when form is submitted', async () => {
|
||||||
const formGroups = wrapper.find('FormGroup');
|
const { user } = setUp();
|
||||||
|
// TODO Using fire event because userEvent.click on the Submit button does not submit the form
|
||||||
|
const submitForm = () => fireEvent.submit(screen.getByRole('form'));
|
||||||
|
|
||||||
expect(editDomainRedirects).not.toHaveBeenCalled();
|
expect(editDomainRedirects).not.toHaveBeenCalled();
|
||||||
|
submitForm();
|
||||||
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
|
await waitFor(() => expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
||||||
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
|
||||||
baseUrlRedirect: 'baz',
|
baseUrlRedirect: 'baz',
|
||||||
regular404Redirect: null,
|
regular404Redirect: null,
|
||||||
invalidShortUrlRedirect: null,
|
invalidShortUrlRedirect: null,
|
||||||
});
|
}));
|
||||||
|
|
||||||
formGroups.at(0).simulate('change', 'new_base_url');
|
await user.clear(screen.getByDisplayValue('baz'));
|
||||||
formGroups.at(2).simulate('change', 'new_invalid_short_url');
|
await user.type(screen.getAllByPlaceholderText('No redirect')[0], 'new_base_url');
|
||||||
|
await user.type(screen.getAllByPlaceholderText('No redirect')[2], 'new_invalid_short_url');
|
||||||
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
|
submitForm();
|
||||||
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
await waitFor(() => expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
||||||
baseUrlRedirect: 'new_base_url',
|
baseUrlRedirect: 'new_base_url',
|
||||||
regular404Redirect: null,
|
regular404Redirect: null,
|
||||||
invalidShortUrlRedirect: 'new_invalid_short_url',
|
invalidShortUrlRedirect: 'new_invalid_short_url',
|
||||||
});
|
}));
|
||||||
|
|
||||||
formGroups.at(1).simulate('change', 'new_regular_404');
|
await user.type(screen.getAllByPlaceholderText('No redirect')[1], 'new_regular_404');
|
||||||
formGroups.at(2).simulate('change', '');
|
await user.clear(screen.getByDisplayValue('new_invalid_short_url'));
|
||||||
|
submitForm();
|
||||||
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
|
await waitFor(() => expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
||||||
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
|
||||||
baseUrlRedirect: 'new_base_url',
|
baseUrlRedirect: 'new_base_url',
|
||||||
regular404Redirect: 'new_regular_404',
|
regular404Redirect: 'new_regular_404',
|
||||||
invalidShortUrlRedirect: null,
|
invalidShortUrlRedirect: null,
|
||||||
});
|
}));
|
||||||
|
|
||||||
formGroups.at(0).simulate('change', '');
|
await Promise.all(screen.getAllByPlaceholderText('No redirect').map((element) => user.clear(element)));
|
||||||
formGroups.at(1).simulate('change', '');
|
submitForm();
|
||||||
formGroups.at(2).simulate('change', '');
|
await waitFor(() => expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
||||||
|
|
||||||
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
|
|
||||||
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
|
|
||||||
baseUrlRedirect: null,
|
baseUrlRedirect: null,
|
||||||
regular404Redirect: null,
|
regular404Redirect: null,
|
||||||
invalidShortUrlRedirect: null,
|
invalidShortUrlRedirect: null,
|
||||||
});
|
}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue