diff --git a/src/servers/DeleteServerModal.tsx b/src/servers/DeleteServerModal.tsx index 8fba141c..d22c93c4 100644 --- a/src/servers/DeleteServerModal.tsx +++ b/src/servers/DeleteServerModal.tsx @@ -1,4 +1,4 @@ -import { FC } from 'react'; +import { FC, useRef } from 'react'; import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; import { useNavigate } from 'react-router-dom'; import { ServerWithId } from './data'; @@ -18,14 +18,22 @@ export const DeleteServerModal: FC = ( { server, toggle, isOpen, deleteServer, redirectHome = true }, ) => { const navigate = useNavigate(); - const closeModal = () => { - deleteServer(server); + const doDelete = useRef(false); + const toggleAndDelete = () => { + doDelete.current = true; toggle(); + }; + const onClosed = () => { + if (!doDelete.current) { + return; + } + + deleteServer(server); redirectHome && navigate('/'); }; return ( - + Remove server

Are you sure you want to remove {server ? server.name : ''}?

@@ -38,7 +46,7 @@ export const DeleteServerModal: FC = (
- +
); diff --git a/test/servers/DeleteServerModal.test.tsx b/test/servers/DeleteServerModal.test.tsx index cf509b41..0e89afa9 100644 --- a/test/servers/DeleteServerModal.test.tsx +++ b/test/servers/DeleteServerModal.test.tsx @@ -1,25 +1,37 @@ -import { screen } from '@testing-library/react'; +import { FC } from 'react'; +import { screen, waitFor } from '@testing-library/react'; import { Mock } from 'ts-mockery'; import { useNavigate } from 'react-router-dom'; import { DeleteServerModal } from '../../src/servers/DeleteServerModal'; import { ServerWithId } from '../../src/servers/data'; import { renderWithEvents } from '../__helpers__/setUpTest'; +import { useToggle } from '../../src/utils/helpers/hooks'; 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 ( + ({ name })} + toggle={toggle} + isOpen={isOpen} + deleteServer={deleteServer} + /> + ); +}; + describe('', () => { const deleteServerMock = jest.fn(); const navigate = jest.fn(); - const toggleMock = jest.fn(); const serverName = 'the_server_name'; const setUp = () => { (useNavigate as any).mockReturnValue(navigate); return renderWithEvents( - ({ name: serverName })} - toggle={toggleMock} - isOpen + , ); @@ -47,10 +59,8 @@ describe('', () => { ])('toggles when clicking cancel button', async (getButton) => { const { user } = setUp(); - expect(toggleMock).not.toHaveBeenCalled(); await user.click(getButton()); - expect(toggleMock).toHaveBeenCalledTimes(1); expect(deleteServerMock).not.toHaveBeenCalled(); expect(navigate).not.toHaveBeenCalled(); }); @@ -58,13 +68,11 @@ describe('', () => { it('deletes server when clicking accept button', async () => { const { user } = setUp(); - expect(toggleMock).not.toHaveBeenCalled(); expect(deleteServerMock).not.toHaveBeenCalled(); expect(navigate).not.toHaveBeenCalled(); await user.click(screen.getByRole('button', { name: 'Delete' })); - expect(toggleMock).toHaveBeenCalledTimes(1); - expect(deleteServerMock).toHaveBeenCalledTimes(1); - expect(navigate).toHaveBeenCalledTimes(1); + await waitFor(() => expect(deleteServerMock).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(navigate).toHaveBeenCalledTimes(1)); }); });