Created EditDomainRedirectsModal test

This commit is contained in:
Alejandro Celaya 2021-08-23 19:12:41 +02:00
parent 13785c7beb
commit 74ac122787
4 changed files with 91 additions and 20 deletions

View file

@ -1,7 +1,7 @@
import { FC, useState } from 'react';
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
import { ShlinkDomain, ShlinkDomainRedirects } from '../../api/types';
import { FormGroupContainer } from '../../utils/FormGroupContainer';
import { FormGroupContainer, FormGroupContainerProps } from '../../utils/FormGroupContainer';
import { handleEventPreventingDefault, nonEmptyValueOrNull } from '../../utils/utils';
import { InfoTooltip } from '../../utils/InfoTooltip';
@ -12,19 +12,14 @@ interface EditDomainRedirectsModalProps {
editDomainRedirects: (domain: string, redirects: Partial<ShlinkDomainRedirects>) => Promise<void>;
}
const FormGroup: FC<{ value: string; onChange: (newValue: string) => void; isLast?: boolean }> = (
{ value, onChange, isLast, children },
) => (
const FormGroup: FC<FormGroupContainerProps & { isLast?: boolean }> = ({ isLast, ...rest }) => (
<FormGroupContainer
value={value}
{...rest}
required={false}
type="url"
placeholder="No redirect"
className={isLast ? 'mb-0' : ''}
onChange={onChange}
>
{children}
</FormGroupContainer>
/>
);
export const EditDomainRedirectsModal: FC<EditDomainRedirectsModalProps> = (

View file

@ -1,5 +1,5 @@
import { FC, ReactNode, useEffect, useState } from 'react';
import { FormGroupContainer } from '../../utils/FormGroupContainer';
import { FormGroupContainer, FormGroupContainerProps } from '../../utils/FormGroupContainer';
import { handleEventPreventingDefault } from '../../utils/utils';
import { ServerData } from '../data';
import { SimpleCard } from '../../utils/SimpleCard';
@ -11,6 +11,9 @@ interface ServerFormProps {
title?: ReactNode;
}
const FormGroup: FC<FormGroupContainerProps> = (props) =>
<FormGroupContainer {...props} labelClassName="create-server__label" />;
export const ServerForm: FC<ServerFormProps> = ({ onSubmit, initialValues, children, title }) => {
const [ name, setName ] = useState('');
const [ url, setUrl ] = useState('');
@ -26,15 +29,9 @@ export const ServerForm: FC<ServerFormProps> = ({ onSubmit, initialValues, child
return (
<form className="server-form" onSubmit={handleSubmit}>
<SimpleCard className="mb-3" title={title}>
<FormGroupContainer labelClassName="create-server__label" value={name} onChange={setName}>
Name
</FormGroupContainer>
<FormGroupContainer labelClassName="create-server__label" type="url" value={url} onChange={setUrl}>
URL
</FormGroupContainer>
<FormGroupContainer labelClassName="create-server__label" value={apiKey} onChange={setApiKey}>API
key
</FormGroupContainer>
<FormGroup value={name} onChange={setName}>Name</FormGroup>
<FormGroup type="url" value={url} onChange={setUrl}>URL</FormGroup>
<FormGroup value={apiKey} onChange={setApiKey}>APIkey</FormGroup>
</SimpleCard>
<div className="text-right">{children}</div>

View file

@ -2,7 +2,7 @@ import { FC, useRef } from 'react';
import { v4 as uuid } from 'uuid';
import { InputType } from 'reactstrap/lib/Input';
interface FormGroupContainerProps {
export interface FormGroupContainerProps {
value: string;
onChange: (newValue: string) => void;
id?: string;

View file

@ -0,0 +1,79 @@
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
import { Button, ModalHeader } from 'reactstrap';
import { ShlinkDomain } from '../../../src/api/types';
import { EditDomainRedirectsModal } from '../../../src/domains/helpers/EditDomainRedirectsModal';
import { InfoTooltip } from '../../../src/utils/InfoTooltip';
describe('<EditDomainRedirectsModal />', () => {
let wrapper: ShallowWrapper;
const editDomainRedirects = jest.fn().mockResolvedValue(undefined);
const toggle = jest.fn();
const domain = Mock.of<ShlinkDomain>({ domain: 'foo.com' });
beforeEach(() => {
wrapper = shallow(
<EditDomainRedirectsModal domain={domain} isOpen toggle={toggle} editDomainRedirects={editDomainRedirects} />,
);
});
afterEach(jest.clearAllMocks);
afterEach(() => wrapper?.unmount());
it('renders domain in header', () => {
const header = wrapper.find(ModalHeader);
expect(header.html()).toContain('foo.com');
});
it('expected amount of form groups and tooltips', () => {
const formGroups = wrapper.find('FormGroup');
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();
(wrapper.prop('toggle') as Function)(); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
(wrapper.find(ModalHeader).prop('toggle') as Function)();
wrapper.find(Button).first().simulate('click');
expect(toggle).toHaveBeenCalledTimes(3);
});
it('saves expected values when form is submitted', () => {
const formGroups = wrapper.find('FormGroup');
expect(editDomainRedirects).not.toHaveBeenCalled();
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
baseUrlRedirect: null,
regular404Redirect: null,
invalidShortUrlRedirect: null,
});
formGroups.at(0).simulate('change', 'new_base_url');
formGroups.at(2).simulate('change', 'new_invalid_short_url');
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
baseUrlRedirect: 'new_base_url',
regular404Redirect: null,
invalidShortUrlRedirect: 'new_invalid_short_url',
});
formGroups.at(1).simulate('change', 'new_regular_404');
formGroups.at(2).simulate('change', '');
wrapper.find('form').simulate('submit', { preventDefault: jest.fn() });
expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', {
baseUrlRedirect: 'new_base_url',
regular404Redirect: 'new_regular_404',
invalidShortUrlRedirect: null,
});
});
});