2022-07-10 00:03:21 +03:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2022-12-31 18:42:04 +03:00
|
|
|
import { MemoryRouter, useNavigate } from 'react-router-dom';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { EditServer as editServerConstruct } from '../../src/servers/EditServer';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2020-03-15 15:43:12 +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(),
|
|
|
|
}));
|
2022-02-08 00:17:57 +03:00
|
|
|
|
2020-03-15 15:43:12 +03:00
|
|
|
describe('<EditServer />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const ServerError = vi.fn();
|
|
|
|
const editServerMock = vi.fn();
|
|
|
|
const navigate = vi.fn();
|
2023-04-13 22:48:29 +03:00
|
|
|
const defaultSelectedServer = fromPartial<ReachableServer>({
|
2020-03-15 15:43:12 +03:00
|
|
|
id: 'abc123',
|
2022-05-02 19:47:18 +03:00
|
|
|
name: 'the_name',
|
|
|
|
url: 'the_url',
|
|
|
|
apiKey: 'the_api_key',
|
2020-08-29 21:20:45 +03:00
|
|
|
});
|
2022-02-08 00:17:57 +03:00
|
|
|
const EditServer = editServerConstruct(ServerError);
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (selectedServer: SelectedServer = defaultSelectedServer) => renderWithEvents(
|
2022-12-31 18:42:04 +03:00
|
|
|
<MemoryRouter>
|
2023-05-27 12:57:26 +03:00
|
|
|
<EditServer editServer={editServerMock} selectedServer={selectedServer} selectServer={vi.fn()} />
|
2022-12-31 18:42:04 +03:00
|
|
|
</MemoryRouter>,
|
2022-07-10 00:03:21 +03:00
|
|
|
);
|
2020-03-15 15:43:12 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
2020-03-15 15:43:12 +03:00
|
|
|
});
|
|
|
|
|
2022-05-02 19:47:18 +03:00
|
|
|
it('renders nothing if selected server is not reachable', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial<SelectedServer>({}));
|
2022-05-02 19:47:18 +03:00
|
|
|
|
|
|
|
expect(screen.queryByText('Edit')).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('Cancel')).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('Save')).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders server title', () => {
|
|
|
|
setUp();
|
|
|
|
expect(screen.getByText(`Edit "${defaultSelectedServer.name}"`)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('display the server info in the form components', () => {
|
|
|
|
setUp();
|
|
|
|
|
|
|
|
expect(screen.getByDisplayValue('the_name')).toBeInTheDocument();
|
|
|
|
expect(screen.getByDisplayValue('the_url')).toBeInTheDocument();
|
|
|
|
expect(screen.getByDisplayValue('the_api_key')).toBeInTheDocument();
|
2020-03-15 15:43:12 +03:00
|
|
|
});
|
|
|
|
|
2022-05-10 21:54:14 +03:00
|
|
|
it('edits server and redirects to it when form is submitted', async () => {
|
|
|
|
const { user } = setUp();
|
2020-03-15 15:43:12 +03:00
|
|
|
|
2022-05-10 21:54:14 +03:00
|
|
|
await user.type(screen.getByDisplayValue('the_name'), ' edited');
|
|
|
|
await user.type(screen.getByDisplayValue('the_url'), ' edited');
|
|
|
|
// TODO Using fire event because userEvent.click on the Submit button does not submit the form
|
|
|
|
// await user.click(screen.getByRole('button', { name: 'Save' }));
|
2022-05-02 19:47:18 +03:00
|
|
|
fireEvent.submit(screen.getByRole('form'));
|
2020-03-15 15:43:12 +03:00
|
|
|
|
2022-05-02 19:47:18 +03:00
|
|
|
expect(editServerMock).toHaveBeenCalledWith('abc123', {
|
2022-05-10 21:54:14 +03:00
|
|
|
name: 'the_name edited',
|
|
|
|
url: 'the_url edited',
|
2022-05-02 19:47:18 +03:00
|
|
|
apiKey: 'the_api_key',
|
|
|
|
});
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2020-03-15 15:43:12 +03:00
|
|
|
});
|
|
|
|
});
|