2022-05-02 19:47:18 +03:00
|
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
2020-08-29 21:20:45 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-02-08 00:17:57 +03:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2020-03-15 15:43:12 +03:00
|
|
|
import { EditServer as editServerConstruct } from '../../src/servers/EditServer';
|
2022-05-02 19:47:18 +03:00
|
|
|
import { ReachableServer, SelectedServer } from '../../src/servers/data';
|
2020-03-15 15:43:12 +03:00
|
|
|
|
2022-02-08 00:17:57 +03:00
|
|
|
jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), useNavigate: jest.fn() }));
|
|
|
|
|
2020-03-15 15:43:12 +03:00
|
|
|
describe('<EditServer />', () => {
|
|
|
|
const ServerError = jest.fn();
|
|
|
|
const editServerMock = jest.fn();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2022-05-02 19:47:18 +03:00
|
|
|
const defaultSelectedServer = Mock.of<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-05-02 19:47:18 +03:00
|
|
|
const setUp = (selectedServer: SelectedServer = defaultSelectedServer) => render(
|
|
|
|
<EditServer editServer={editServerMock} selectedServer={selectedServer} selectServer={jest.fn()} />,
|
|
|
|
);
|
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-02-08 00:17:57 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
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', () => {
|
|
|
|
setUp(Mock.all<SelectedServer>());
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it('edits server and redirects to it when form is submitted', () => {
|
2022-05-02 19:47:18 +03:00
|
|
|
setUp();
|
2020-03-15 15:43:12 +03:00
|
|
|
|
2022-05-02 19:47:18 +03:00
|
|
|
fireEvent.change(screen.getByDisplayValue('the_name'), { target: { value: 'the_new_name' } });
|
|
|
|
fireEvent.change(screen.getByDisplayValue('the_url'), { target: { value: 'the_new_url' } });
|
|
|
|
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', {
|
|
|
|
name: 'the_new_name',
|
|
|
|
url: 'the_new_url',
|
|
|
|
apiKey: 'the_api_key',
|
|
|
|
});
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2020-03-15 15:43:12 +03:00
|
|
|
});
|
|
|
|
});
|