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';
|
2023-12-19 01:38:34 +03:00
|
|
|
import { createMemoryHistory } from 'history';
|
|
|
|
import { Router } from 'react-router-dom';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { CreateServerFactory } from '../../src/servers/CreateServer';
|
2023-09-02 20:08:12 +03:00
|
|
|
import type { ServersMap } from '../../src/servers/data';
|
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';
|
2018-08-25 00:19:49 +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();
|
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-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;
|
|
|
|
});
|
2023-09-05 10:08:42 +03:00
|
|
|
const CreateServer = CreateServerFactory(fromPartial({
|
|
|
|
ImportServersBtn: () => <>ImportServersBtn</>,
|
|
|
|
useTimeoutToggle,
|
|
|
|
}));
|
2023-12-19 01:38:34 +03:00
|
|
|
const history = createMemoryHistory({ initialEntries: ['/foo', '/bar'] });
|
|
|
|
|
|
|
|
return {
|
|
|
|
history,
|
|
|
|
...renderWithEvents(
|
|
|
|
<Router location={history.location} navigator={history}>
|
|
|
|
<CreateServer createServers={createServersMock} servers={servers} />
|
|
|
|
</Router>,
|
|
|
|
),
|
|
|
|
};
|
2020-03-15 12:33:23 +03:00
|
|
|
};
|
|
|
|
|
2023-09-30 11:45:52 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
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 () => {
|
2023-12-19 01:38:34 +03:00
|
|
|
const { user, history } = 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
|
|
|
})]);
|
2023-12-19 01:38:34 +03:00
|
|
|
expect(history.location.pathname).toEqual(expect.stringMatching(/^\/server\//));
|
2022-05-29 13:18:21 +03:00
|
|
|
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 () => {
|
2023-12-19 01:38:34 +03:00
|
|
|
const { user, history } = setUp();
|
2022-05-29 13:18:21 +03:00
|
|
|
|
|
|
|
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();
|
2023-12-19 01:38:34 +03:00
|
|
|
expect(history.location.pathname).toEqual('/foo'); // Goes back to first route from history's initialEntries
|
2021-12-30 23:21:30 +03:00
|
|
|
});
|
2018-08-25 00:19:49 +03:00
|
|
|
});
|