2024-10-31 11:30:14 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2024-11-01 12:27:35 +03:00
|
|
|
import type { ServerData, ServersMap, ServerWithId } from '../../../src/servers/data';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type {
|
|
|
|
ImportServersBtnProps } from '../../../src/servers/helpers/ImportServersBtn';
|
2023-09-05 10:08:42 +03:00
|
|
|
import { ImportServersBtnFactory } from '../../../src/servers/helpers/ImportServersBtn';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ServersImporter } from '../../../src/servers/services/ServersImporter';
|
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-23 17:26:42 +03:00
|
|
|
|
|
|
|
describe('<ImportServersBtn />', () => {
|
2024-10-31 11:30:14 +03:00
|
|
|
const csvFile = new File([''], 'servers.csv', { type: 'text/csv' });
|
2023-05-27 12:57:26 +03:00
|
|
|
const onImportMock = vi.fn();
|
|
|
|
const createServersMock = vi.fn();
|
|
|
|
const importServersFromFile = vi.fn().mockResolvedValue([]);
|
2023-04-13 22:48:29 +03:00
|
|
|
const serversImporterMock = fromPartial<ServersImporter>({ importServersFromFile });
|
2023-09-05 10:08:42 +03:00
|
|
|
const ImportServersBtn = ImportServersBtnFactory(fromPartial({ ServersImporter: serversImporterMock }));
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (props: Partial<ImportServersBtnProps> = {}, servers: ServersMap = {}) => renderWithEvents(
|
|
|
|
<ImportServersBtn
|
|
|
|
servers={servers}
|
|
|
|
{...props}
|
|
|
|
createServers={createServersMock}
|
|
|
|
onImport={onImportMock}
|
|
|
|
/>,
|
|
|
|
);
|
2021-10-22 19:53:00 +03:00
|
|
|
|
2023-09-30 11:45:52 +03:00
|
|
|
it('passes a11y checks', () => checkAccessibility(setUp()));
|
|
|
|
|
2022-06-05 12:06:26 +03:00
|
|
|
it('shows tooltip on button hover', async () => {
|
|
|
|
const { user } = setUp();
|
2021-10-22 19:53:00 +03:00
|
|
|
|
2022-06-05 12:06:26 +03:00
|
|
|
expect(screen.queryByText(/^You can create servers by importing a CSV file/)).not.toBeInTheDocument();
|
|
|
|
await user.hover(screen.getByRole('button'));
|
|
|
|
await waitFor(
|
|
|
|
() => expect(screen.getByText(/^You can create servers by importing a CSV file/)).toBeInTheDocument(),
|
|
|
|
);
|
2018-08-23 17:26:42 +03:00
|
|
|
});
|
|
|
|
|
2021-10-22 19:53:00 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, ''],
|
|
|
|
['foo', 'foo'],
|
|
|
|
['bar', 'bar'],
|
2021-10-22 19:53:00 +03:00
|
|
|
])('allows a class name to be provided', (providedClassName, expectedClassName) => {
|
2022-06-05 12:06:26 +03:00
|
|
|
setUp({ className: providedClassName });
|
|
|
|
expect(screen.getByRole('button')).toHaveAttribute('class', expect.stringContaining(expectedClassName));
|
2021-10-22 19:53:00 +03:00
|
|
|
});
|
|
|
|
|
2021-10-22 20:03:12 +03:00
|
|
|
it.each([
|
2022-06-05 12:06:26 +03:00
|
|
|
[undefined, 'Import from file'],
|
|
|
|
['foo', 'foo'],
|
|
|
|
['bar', 'bar'],
|
|
|
|
])('has expected text', (children, expectedText) => {
|
|
|
|
setUp({ children });
|
|
|
|
expect(screen.getByRole('button')).toHaveTextContent(expectedText);
|
2018-08-23 17:26:42 +03:00
|
|
|
});
|
|
|
|
|
2021-10-31 14:07:38 +03:00
|
|
|
it('imports servers when file input changes', async () => {
|
2024-10-31 11:30:14 +03:00
|
|
|
const { user } = setUp();
|
|
|
|
|
|
|
|
const input = screen.getByTestId('csv-file-input');
|
|
|
|
await user.upload(input, csvFile);
|
2018-08-23 17:26:42 +03:00
|
|
|
|
2021-10-31 14:07:38 +03:00
|
|
|
expect(importServersFromFile).toHaveBeenCalledTimes(1);
|
2024-10-31 11:30:14 +03:00
|
|
|
expect(createServersMock).toHaveBeenCalledTimes(1);
|
2022-01-01 14:20:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2024-11-01 12:27:35 +03:00
|
|
|
{ btnName: 'Save anyway',savesDuplicatedServers: true },
|
|
|
|
{ btnName: 'Discard', savesDuplicatedServers: false },
|
|
|
|
])('creates expected servers depending on selected option in modal', async ({ btnName, savesDuplicatedServers }) => {
|
2024-11-01 14:09:13 +03:00
|
|
|
const existingServerData: ServerData = {
|
2024-11-01 12:27:35 +03:00
|
|
|
name: 'existingServer',
|
|
|
|
url: 'http://s.test/existingUrl',
|
|
|
|
apiKey: 'existingApiKey',
|
|
|
|
};
|
2024-11-01 14:09:13 +03:00
|
|
|
const existingServer: ServerWithId = {
|
|
|
|
...existingServerData,
|
|
|
|
id: 'existingserver-s.test',
|
|
|
|
};
|
2024-11-01 12:27:35 +03:00
|
|
|
const newServer: ServerData = { name: 'newServer', url: 'http://s.test/newUrl', apiKey: 'newApiKey' };
|
|
|
|
const { user } = setUp({}, { [existingServer.id]: existingServer });
|
2024-10-31 11:30:14 +03:00
|
|
|
|
2022-06-05 12:06:26 +03:00
|
|
|
importServersFromFile.mockResolvedValue([existingServer, newServer]);
|
|
|
|
|
|
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
2024-11-01 12:27:35 +03:00
|
|
|
await user.upload(screen.getByTestId('csv-file-input'), csvFile);
|
2024-10-31 11:30:14 +03:00
|
|
|
expect(screen.getByRole('dialog')).toBeInTheDocument();
|
2022-06-05 12:06:26 +03:00
|
|
|
await user.click(screen.getByRole('button', { name: btnName }));
|
|
|
|
|
2024-11-01 12:27:35 +03:00
|
|
|
expect(createServersMock).toHaveBeenCalledWith(
|
|
|
|
savesDuplicatedServers
|
2024-11-01 14:09:13 +03:00
|
|
|
? [expect.objectContaining(existingServerData), expect.objectContaining(newServer)]
|
2024-11-01 12:27:35 +03:00
|
|
|
: [expect.objectContaining(newServer)],
|
|
|
|
);
|
2021-10-31 14:07:38 +03:00
|
|
|
expect(onImportMock).toHaveBeenCalledTimes(1);
|
2018-08-23 17:26:42 +03:00
|
|
|
});
|
|
|
|
});
|