2022-07-10 00:03:21 +03:00
|
|
|
import { fireEvent, screen, waitFor } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
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';
|
2023-09-02 20:08:12 +03:00
|
|
|
import type { ServersMap } from '../../src/servers/data';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2018-08-25 00:19:49 +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
|
|
|
|
2023-09-02 20:08:12 +03:00
|
|
|
type SetUpOptions = {
|
|
|
|
serversImported?: boolean;
|
|
|
|
importFailed?: boolean;
|
|
|
|
servers?: ServersMap;
|
|
|
|
};
|
|
|
|
|
2018-08-25 00:19:49 +03:00
|
|
|
describe('<CreateServer />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const createServersMock = vi.fn();
|
|
|
|
const navigate = vi.fn();
|
2023-09-02 20:08:12 +03:00
|
|
|
const defaultServers: ServersMap = {
|
|
|
|
foo: fromPartial({ url: 'https://existing_url.com', apiKey: 'existing_api_key' }),
|
|
|
|
};
|
|
|
|
const setUp = ({ serversImported = false, importFailed = false, servers = defaultServers }: SetUpOptions = {}) => {
|
2022-02-08 00:17:57 +03:00
|
|
|
(useNavigate as any).mockReturnValue(navigate);
|
|
|
|
|
2022-05-29 13:18:21 +03:00
|
|
|
let callCount = 0;
|
2023-05-27 12:57:26 +03:00
|
|
|
const useTimeoutToggle = vi.fn().mockImplementation(() => {
|
2022-05-29 13:18:21 +03:00
|
|
|
const result = [callCount % 2 === 0 ? serversImported : importFailed, () => null];
|
|
|
|
callCount += 1;
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
const CreateServer = createCreateServer(() => <>ImportServersBtn</>, useTimeoutToggle);
|
|
|
|
|
2022-11-05 11:40:12 +03:00
|
|
|
return renderWithEvents(<CreateServer createServers={createServersMock} servers={servers} />);
|
2020-03-15 12:33:23 +03:00
|
|
|
};
|
|
|
|
|
2018-08-25 00:19:49 +03:00
|
|
|
it('shows success message when imported is true', () => {
|
2023-09-02 20:08:12 +03:00
|
|
|
setUp({ serversImported: 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();
|
2023-09-02 20:08:12 +03:00
|
|
|
expect(screen.queryByText('ImportServersBtn')).not.toBeInTheDocument();
|
2020-08-22 18:58:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('shows error message when import failed', () => {
|
2023-09-02 20:08:12 +03:00
|
|
|
setUp({ importFailed: 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
|
|
|
});
|
|
|
|
|
2023-09-02 20:08:12 +03:00
|
|
|
it('shows import button when no servers exist yet', () => {
|
|
|
|
setUp({ servers: {} });
|
|
|
|
expect(screen.queryByText('ImportServersBtn')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
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-11-05 11:40:12 +03:00
|
|
|
expect(createServersMock).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-11-05 11:40:12 +03:00
|
|
|
expect(createServersMock).toHaveBeenCalledWith([expect.objectContaining({
|
2022-05-29 13:18:21 +03:00
|
|
|
name: 'the_name',
|
|
|
|
url: 'https://the_url.com',
|
|
|
|
apiKey: 'the_api_key',
|
2022-11-05 11:40:12 +03:00
|
|
|
})]);
|
2022-05-29 13:18:21 +03:00
|
|
|
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-11-05 11:40:12 +03:00
|
|
|
expect(createServersMock).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
|
|
|
});
|