shlink-web-client/test/servers/DeleteServerModal.test.tsx

70 lines
2.2 KiB
TypeScript
Raw Normal View History

import { screen, waitFor } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
import { useNavigate } from 'react-router-dom';
2023-02-18 13:11:01 +03:00
import { DeleteServerModal } from '../../src/servers/DeleteServerModal';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2022-11-22 22:18:11 +03:00
import { TestModalWrapper } from '../__helpers__/TestModalWrapper';
2018-08-24 13:36:14 +03:00
2023-05-27 12:57:26 +03:00
vi.mock('react-router-dom', async () => ({
...(await vi.importActual<any>('react-router-dom')),
useNavigate: vi.fn(),
}));
2018-08-24 13:36:14 +03:00
describe('<DeleteServerModal />', () => {
2023-05-27 12:57:26 +03:00
const deleteServerMock = vi.fn();
const navigate = vi.fn();
2018-08-24 13:36:14 +03:00
const serverName = 'the_server_name';
const setUp = () => {
(useNavigate as any).mockReturnValue(navigate);
return renderWithEvents(
2022-11-22 22:18:11 +03:00
<TestModalWrapper
renderModal={(args) => (
<DeleteServerModal
{...args}
server={fromPartial({ name: serverName })}
2022-11-22 22:18:11 +03:00
deleteServer={deleteServerMock}
/>
)}
/>,
);
};
2018-08-24 13:36:14 +03:00
it('renders a modal window', () => {
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', () => {
setUp();
expect(screen.getByText(/^Are you sure you want to remove/)).toBeInTheDocument();
expect(screen.getByText(serverName)).toBeInTheDocument();
2018-08-24 13:36:14 +03:00
});
it.each([
[() => screen.getByRole('button', { name: 'Cancel' })],
[() => screen.getByLabelText('Close')],
])('toggles when clicking cancel button', async (getButton) => {
const { user } = setUp();
await user.click(getButton());
2018-08-24 13:36:14 +03:00
expect(deleteServerMock).not.toHaveBeenCalled();
expect(navigate).not.toHaveBeenCalled();
2018-08-24 13:36:14 +03:00
});
it('deletes server when clicking accept button', async () => {
const { user } = setUp();
expect(deleteServerMock).not.toHaveBeenCalled();
expect(navigate).not.toHaveBeenCalled();
await user.click(screen.getByRole('button', { name: 'Delete' }));
2018-08-24 13:36:14 +03:00
await waitFor(() => expect(deleteServerMock).toHaveBeenCalledTimes(1));
await waitFor(() => expect(navigate).toHaveBeenCalledTimes(1));
2018-08-24 13:36:14 +03:00
});
});