mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Ensured DeleteServerModal is not removed from the DOM before close transition has finished
This commit is contained in:
parent
d21758c410
commit
9bdf55374c
2 changed files with 33 additions and 17 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { FC } from 'react';
|
import { FC, useRef } from 'react';
|
||||||
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { ServerWithId } from './data';
|
import { ServerWithId } from './data';
|
||||||
|
@ -18,14 +18,22 @@ export const DeleteServerModal: FC<DeleteServerModalConnectProps> = (
|
||||||
{ server, toggle, isOpen, deleteServer, redirectHome = true },
|
{ server, toggle, isOpen, deleteServer, redirectHome = true },
|
||||||
) => {
|
) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const closeModal = () => {
|
const doDelete = useRef<boolean>(false);
|
||||||
deleteServer(server);
|
const toggleAndDelete = () => {
|
||||||
|
doDelete.current = true;
|
||||||
toggle();
|
toggle();
|
||||||
|
};
|
||||||
|
const onClosed = () => {
|
||||||
|
if (!doDelete.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteServer(server);
|
||||||
redirectHome && navigate('/');
|
redirectHome && navigate('/');
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal isOpen={isOpen} toggle={toggle} centered>
|
<Modal isOpen={isOpen} toggle={toggle} centered onClosed={onClosed}>
|
||||||
<ModalHeader toggle={toggle} className="text-danger">Remove server</ModalHeader>
|
<ModalHeader toggle={toggle} className="text-danger">Remove server</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
<p>Are you sure you want to remove <b>{server ? server.name : ''}</b>?</p>
|
<p>Are you sure you want to remove <b>{server ? server.name : ''}</b>?</p>
|
||||||
|
@ -38,7 +46,7 @@ export const DeleteServerModal: FC<DeleteServerModalConnectProps> = (
|
||||||
</ModalBody>
|
</ModalBody>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<Button color="link" onClick={toggle}>Cancel</Button>
|
<Button color="link" onClick={toggle}>Cancel</Button>
|
||||||
<Button color="danger" onClick={() => closeModal()}>Delete</Button>
|
<Button color="danger" onClick={toggleAndDelete}>Delete</Button>
|
||||||
</ModalFooter>
|
</ModalFooter>
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
|
|
|
@ -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 { 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';
|
||||||
|
|
||||||
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();
|
||||||
const toggleMock = jest.fn();
|
|
||||||
const serverName = 'the_server_name';
|
const serverName = 'the_server_name';
|
||||||
const setUp = () => {
|
const setUp = () => {
|
||||||
(useNavigate as any).mockReturnValue(navigate);
|
(useNavigate as any).mockReturnValue(navigate);
|
||||||
|
|
||||||
return renderWithEvents(
|
return renderWithEvents(
|
||||||
<DeleteServerModal
|
<DeleteServerModalWrapper
|
||||||
server={Mock.of<ServerWithId>({ name: serverName })}
|
name={serverName}
|
||||||
toggle={toggleMock}
|
|
||||||
isOpen
|
|
||||||
deleteServer={deleteServerMock}
|
deleteServer={deleteServerMock}
|
||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
|
@ -47,10 +59,8 @@ describe('<DeleteServerModal />', () => {
|
||||||
])('toggles when clicking cancel button', async (getButton) => {
|
])('toggles when clicking cancel button', async (getButton) => {
|
||||||
const { user } = setUp();
|
const { user } = setUp();
|
||||||
|
|
||||||
expect(toggleMock).not.toHaveBeenCalled();
|
|
||||||
await user.click(getButton());
|
await user.click(getButton());
|
||||||
|
|
||||||
expect(toggleMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(deleteServerMock).not.toHaveBeenCalled();
|
expect(deleteServerMock).not.toHaveBeenCalled();
|
||||||
expect(navigate).not.toHaveBeenCalled();
|
expect(navigate).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
@ -58,13 +68,11 @@ describe('<DeleteServerModal />', () => {
|
||||||
it('deletes server when clicking accept button', async () => {
|
it('deletes server when clicking accept button', async () => {
|
||||||
const { user } = setUp();
|
const { user } = setUp();
|
||||||
|
|
||||||
expect(toggleMock).not.toHaveBeenCalled();
|
|
||||||
expect(deleteServerMock).not.toHaveBeenCalled();
|
expect(deleteServerMock).not.toHaveBeenCalled();
|
||||||
expect(navigate).not.toHaveBeenCalled();
|
expect(navigate).not.toHaveBeenCalled();
|
||||||
await user.click(screen.getByRole('button', { name: 'Delete' }));
|
await user.click(screen.getByRole('button', { name: 'Delete' }));
|
||||||
|
|
||||||
expect(toggleMock).toHaveBeenCalledTimes(1);
|
await waitFor(() => expect(deleteServerMock).toHaveBeenCalledTimes(1));
|
||||||
expect(deleteServerMock).toHaveBeenCalledTimes(1);
|
await waitFor(() => expect(navigate).toHaveBeenCalledTimes(1));
|
||||||
expect(navigate).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue