Extracted helper to test modals

This commit is contained in:
Alejandro Celaya 2022-11-22 20:18:11 +01:00
parent cf91637848
commit f0a6420ba9
2 changed files with 23 additions and 18 deletions

View file

@ -0,0 +1,14 @@
import { FC, ReactElement } from 'react';
import { useToggle } from '../../src/utils/helpers/hooks';
interface RenderModalArgs {
isOpen: boolean;
toggle: () => void;
}
export const TestModalWrapper: FC<{ renderModal: (args: RenderModalArgs) => ReactElement }> = (
{ renderModal },
) => {
const [isOpen, toggle] = useToggle(true);
return renderModal({ isOpen, toggle });
};

View file

@ -1,27 +1,13 @@
import { FC } from 'react';
import { screen, waitFor } from '@testing-library/react'; import { screen, waitFor } from '@testing-library/react';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { DeleteServerModal } from '../../src/servers/DeleteServerModal'; import { DeleteServerModal } from '../../src/servers/DeleteServerModal';
import { ServerWithId } from '../../src/servers/data'; import { ServerWithId } from '../../src/servers/data';
import { renderWithEvents } from '../__helpers__/setUpTest'; import { renderWithEvents } from '../__helpers__/setUpTest';
import { useToggle } from '../../src/utils/helpers/hooks'; import { TestModalWrapper } from '../__helpers__/TestModalWrapper';
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() })); jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
const DeleteServerModalWrapper: FC<{ name: string; deleteServer: () => void }> = ({ name, deleteServer }) => {
const [isOpen, toggle] = useToggle(true);
return (
<DeleteServerModal
server={Mock.of<ServerWithId>({ name })}
toggle={toggle}
isOpen={isOpen}
deleteServer={deleteServer}
/>
);
};
describe('<DeleteServerModal />', () => { describe('<DeleteServerModal />', () => {
const deleteServerMock = jest.fn(); const deleteServerMock = jest.fn();
const navigate = jest.fn(); const navigate = jest.fn();
@ -30,9 +16,14 @@ describe('<DeleteServerModal />', () => {
(useNavigate as any).mockReturnValue(navigate); (useNavigate as any).mockReturnValue(navigate);
return renderWithEvents( return renderWithEvents(
<DeleteServerModalWrapper <TestModalWrapper
name={serverName} renderModal={(args) => (
deleteServer={deleteServerMock} <DeleteServerModal
{...args}
server={Mock.of<ServerWithId>({ name: serverName })}
deleteServer={deleteServerMock}
/>
)}
/>, />,
); );
}; };