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

72 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-12-31 21:59:56 +03:00
import { act, screen, waitFor } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
2023-12-19 01:38:34 +03:00
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
2023-02-18 13:11:01 +03:00
import { DeleteServerModal } from '../../src/servers/DeleteServerModal';
2023-09-30 11:45:52 +03:00
import { checkAccessibility } from '../__helpers__/accessibility';
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
describe('<DeleteServerModal />', () => {
2023-05-27 12:57:26 +03:00
const deleteServerMock = vi.fn();
2018-08-24 13:36:14 +03:00
const serverName = 'the_server_name';
2023-12-31 21:59:56 +03:00
const setUp = async () => {
2023-12-19 01:38:34 +03:00
const history = createMemoryHistory({ initialEntries: ['/foo'] });
2023-12-31 21:59:56 +03:00
const result = await act(() => renderWithEvents(
<Router location={history.location} navigator={history}>
<TestModalWrapper
renderModal={(args) => (
<DeleteServerModal
{...args}
server={fromPartial({ name: serverName })}
deleteServer={deleteServerMock}
/>
)}
/>
</Router>,
));
return { history, ...result };
};
2023-09-30 11:45:52 +03:00
it('passes a11y checks', () => checkAccessibility(setUp()));
2023-12-31 21:59:56 +03:00
it('renders a modal window', async () => {
await setUp();
expect(screen.getByRole('dialog')).toBeInTheDocument();
expect(screen.getByRole('heading')).toHaveTextContent('Remove server');
2018-08-24 13:36:14 +03:00
});
2023-12-31 21:59:56 +03:00
it('displays the name of the server as part of the content', async () => {
await 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) => {
2023-12-31 21:59:56 +03:00
const { user, history } = await setUp();
2023-12-19 01:38:34 +03:00
expect(history.location.pathname).toEqual('/foo');
await user.click(getButton());
2018-08-24 13:36:14 +03:00
expect(deleteServerMock).not.toHaveBeenCalled();
2023-12-19 01:38:34 +03:00
expect(history.location.pathname).toEqual('/foo'); // No navigation happens, keeping initial pathname
2018-08-24 13:36:14 +03:00
});
it('deletes server when clicking accept button', async () => {
2023-12-31 21:59:56 +03:00
const { user, history } = await setUp();
expect(deleteServerMock).not.toHaveBeenCalled();
2023-12-19 01:38:34 +03:00
expect(history.location.pathname).toEqual('/foo');
await user.click(screen.getByRole('button', { name: 'Delete' }));
2018-08-24 13:36:14 +03:00
await waitFor(() => expect(deleteServerMock).toHaveBeenCalledTimes(1));
2023-12-19 01:38:34 +03:00
await waitFor(() => expect(history.location.pathname).toEqual('/'));
2018-08-24 13:36:14 +03:00
});
});