2022-05-29 13:18:21 +03:00
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
2020-08-22 14:41:54 +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 { CreateServer as createCreateServer } from '../../src/servers/CreateServer';
|
2021-10-17 20:13:06 +03:00
|
|
|
import { ServerWithId } from '../../src/servers/data';
|
2018-08-25 00:19:49 +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-25 00:19:49 +03:00
|
|
|
describe('<CreateServer />', () => {
|
2019-04-19 11:29:49 +03:00
|
|
|
const createServerMock = jest.fn();
|
2022-02-08 00:17:57 +03:00
|
|
|
const navigate = jest.fn();
|
2022-05-29 13:18:21 +03:00
|
|
|
const servers = { foo: Mock.of<ServerWithId>({ url: 'https://existing_url.com', apiKey: 'existing_api_key' }) };
|
|
|
|
const setUp = (serversImported = false, importFailed = false) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
let callCount = 0;
|
|
|
|
const useTimeoutToggle = jest.fn().mockImplementation(() => {
|
|
|
|
const result = [callCount % 2 === 0 ? serversImported : importFailed, () => null];
|
|
|
|
callCount += 1;
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
const CreateServer = createCreateServer(() => <>ImportServersBtn</>, useTimeoutToggle);
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(<CreateServer createServer={createServerMock} servers={servers} />),
|
|
|
|
};
|
2020-03-15 12:33:23 +03:00
|
|
|
};
|
|
|
|
|
2021-12-30 23:21:30 +03:00
|
|
|
beforeEach(jest.clearAllMocks);
|
2018-08-25 00:19:49 +03:00
|
|
|
|
|
|
|
it('shows success message when imported is true', () => {
|
2022-05-29 13:18:21 +03:00
|
|
|
setUp(true);
|
2020-03-15 12:33:23 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
expect(screen.getByText('Servers properly imported. You can now select one from the list :)')).toBeInTheDocument();
|
|
|
|
expect(
|
|
|
|
screen.queryByText('The servers could not be imported. Make sure the format is correct.'),
|
|
|
|
).not.toBeInTheDocument();
|
2020-08-22 18:58:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows error message when import failed', () => {
|
2022-05-29 13:18:21 +03:00
|
|
|
setUp(false, true);
|
2020-08-22 18:58:44 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
expect(
|
|
|
|
screen.queryByText('Servers properly imported. You can now select one from the list :)'),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText('The servers could not be imported. Make sure the format is correct.')).toBeInTheDocument();
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
it('creates server data when form is submitted', async () => {
|
|
|
|
const { user } = setUp();
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
expect(createServerMock).not.toHaveBeenCalled();
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
await user.type(screen.getByLabelText(/^Name/), 'the_name');
|
|
|
|
await user.type(screen.getByLabelText(/^URL/), 'https://the_url.com');
|
|
|
|
await user.type(screen.getByLabelText(/^API key/), 'the_api_key');
|
|
|
|
fireEvent.submit(screen.getByRole('form'));
|
2018-08-25 00:19:49 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
expect(createServerMock).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
name: 'the_name',
|
|
|
|
url: 'https://the_url.com',
|
|
|
|
apiKey: 'the_api_key',
|
|
|
|
}));
|
|
|
|
expect(navigate).toHaveBeenCalledWith(expect.stringMatching(/^\/server\//));
|
|
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
it('displays dialog when trying to create a duplicated server', async () => {
|
|
|
|
const { user } = setUp();
|
|
|
|
|
|
|
|
await user.type(screen.getByLabelText(/^Name/), 'the_name');
|
|
|
|
await user.type(screen.getByLabelText(/^URL/), 'https://existing_url.com');
|
|
|
|
await user.type(screen.getByLabelText(/^API key/), 'existing_api_key');
|
|
|
|
fireEvent.submit(screen.getByRole('form'));
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument());
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Discard' }));
|
2021-12-30 23:21:30 +03:00
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
expect(createServerMock).not.toHaveBeenCalled();
|
2022-02-08 00:17:57 +03:00
|
|
|
expect(navigate).toHaveBeenCalledWith(-1);
|
2021-12-30 23:21:30 +03:00
|
|
|
});
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|