mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-03 23:07:26 +03:00
Extracted helper to test modals
This commit is contained in:
parent
cf91637848
commit
f0a6420ba9
2 changed files with 23 additions and 18 deletions
14
test/__helpers__/TestModalWrapper.tsx
Normal file
14
test/__helpers__/TestModalWrapper.tsx
Normal 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 });
|
||||||
|
};
|
|
@ -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}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue