2022-05-29 21:45:19 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-08-29 10:19:15 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { DeleteServerModal } from '../../src/servers/DeleteServerModal';
|
2020-08-29 10:19:15 +03:00
|
|
|
import { ServerWithId } from '../../src/servers/data';
|
2018-08-24 13:36:14 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
2018-08-24 13:36:14 +03:00
|
|
|
describe('<DeleteServerModal />', () => {
|
2019-04-19 11:29:49 +03:00
|
|
|
const deleteServerMock = jest.fn();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2019-04-19 11:29:49 +03:00
|
|
|
const toggleMock = jest.fn();
|
2018-08-24 13:36:14 +03:00
|
|
|
const serverName = 'the_server_name';
|
2022-05-29 21:45:19 +03:00
|
|
|
const setUp = () => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
return {
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
|
|
|
<DeleteServerModal
|
|
|
|
server={Mock.of<ServerWithId>({ name: serverName })}
|
|
|
|
toggle={toggleMock}
|
|
|
|
isOpen
|
|
|
|
deleteServer={deleteServerMock}
|
|
|
|
/>,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-29 10:19:15 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-08-24 13:36:14 +03:00
|
|
|
|
|
|
|
it('renders a modal window', () => {
|
2022-05-29 21:45:19 +03:00
|
|
|
setUp();
|
|
|
|
|
|
|
|
expect(screen.getByRole('dialog')).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('heading')).toHaveTextContent('Remove server');
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the name of the server as part of the content', () => {
|
2022-05-29 21:45:19 +03:00
|
|
|
setUp();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
expect(screen.getByText(/^Are you sure you want to remove/)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(serverName)).toBeInTheDocument();
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
it.each([
|
|
|
|
[() => screen.getByRole('button', { name: 'Cancel' })],
|
|
|
|
[() => screen.getByLabelText('Close')],
|
|
|
|
])('toggles when clicking cancel button', async (getButton) => {
|
|
|
|
const { user } = setUp();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
expect(toggleMock).not.toHaveBeenCalled();
|
|
|
|
await user.click(getButton());
|
2018-08-24 13:36:14 +03:00
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(toggleMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteServerMock).not.toHaveBeenCalled();
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
it('deletes server when clicking accept button', async () => {
|
|
|
|
const { user } = setUp();
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2022-05-29 21:45:19 +03:00
|
|
|
expect(toggleMock).not.toHaveBeenCalled();
|
|
|
|
expect(deleteServerMock).not.toHaveBeenCalled();
|
|
|
|
expect(navigate).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Delete' }));
|
2018-08-24 13:36:14 +03:00
|
|
|
|
2019-04-19 11:29:49 +03:00
|
|
|
expect(toggleMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteServerMock).toHaveBeenCalledTimes(1);
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledTimes(1);
|
2018-08-24 13:36:14 +03:00
|
|
|
});
|
|
|
|
});
|