shlink-web-client/test/servers/helpers/ImportServersBtn.test.tsx

86 lines
3.3 KiB
TypeScript
Raw Normal View History

import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Mock } from 'ts-mockery';
2022-05-28 11:47:39 +03:00
import {
ImportServersBtn as createImportServersBtn,
ImportServersBtnProps,
} from '../../../src/servers/helpers/ImportServersBtn';
import { ServersImporter } from '../../../src/servers/services/ServersImporter';
import { ServersMap, ServerWithId } from '../../../src/servers/data';
2018-08-23 17:26:42 +03:00
describe('<ImportServersBtn />', () => {
const onImportMock = jest.fn();
const createServersMock = jest.fn();
2021-10-31 14:07:38 +03:00
const importServersFromFile = jest.fn().mockResolvedValue([]);
const serversImporterMock = Mock.of<ServersImporter>({ importServersFromFile });
2022-05-28 11:47:39 +03:00
const ImportServersBtn = createImportServersBtn(serversImporterMock);
const setUp = (props: Partial<ImportServersBtnProps> = {}, servers: ServersMap = {}) => ({
user: userEvent.setup(),
...render(
2021-10-22 19:53:00 +03:00
<ImportServersBtn
servers={servers}
{...props}
createServers={createServersMock}
2021-10-22 19:53:00 +03:00
onImport={onImportMock}
/>,
),
});
2021-10-22 19:53:00 +03:00
afterEach(jest.clearAllMocks);
2018-08-23 17:26:42 +03:00
it('shows tooltip on button hover', async () => {
const { user } = setUp();
2021-10-22 19:53:00 +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) => {
setUp({ className: providedClassName });
expect(screen.getByRole('button')).toHaveAttribute('class', expect.stringContaining(expectedClassName));
2021-10-22 19:53:00 +03:00
});
it.each([
[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 () => {
const { container } = setUp();
const input = container.querySelector('[type=file]');
2018-08-23 17:26:42 +03:00
input && fireEvent.change(input, { target: { files: [''] } });
2021-10-31 14:07:38 +03:00
expect(importServersFromFile).toHaveBeenCalledTimes(1);
});
it.each([
['Save anyway', true],
['Discard', false],
])('creates expected servers depending on selected option in modal', async (btnName, savesDuplicatedServers) => {
const existingServer = Mock.of<ServerWithId>({ id: 'abc', url: 'existingUrl', apiKey: 'existingApiKey' });
const newServer = Mock.of<ServerWithId>({ url: 'newUrl', apiKey: 'newApiKey' });
const { container, user } = setUp({}, { abc: existingServer });
const input = container.querySelector('[type=file]');
importServersFromFile.mockResolvedValue([existingServer, newServer]);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
input && fireEvent.change(input, { target: { files: [''] } });
await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument());
await user.click(screen.getByRole('button', { name: btnName }));
expect(createServersMock).toHaveBeenCalledWith(savesDuplicatedServers ? [existingServer, newServer] : [newServer]);
2021-10-31 14:07:38 +03:00
expect(onImportMock).toHaveBeenCalledTimes(1);
2018-08-23 17:26:42 +03:00
});
});