2022-07-10 00:03:21 +03:00
|
|
|
import { screen } from '@testing-library/react';
|
2021-12-30 23:40:08 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-01-01 14:20:09 +03:00
|
|
|
import { DuplicatedServersModal } from '../../../src/servers/helpers/DuplicatedServersModal';
|
2021-12-30 23:40:08 +03:00
|
|
|
import { ServerData } from '../../../src/servers/data';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
2021-12-30 23:40:08 +03:00
|
|
|
|
2022-01-01 14:20:09 +03:00
|
|
|
describe('<DuplicatedServersModal />', () => {
|
2021-12-30 23:40:08 +03:00
|
|
|
const onDiscard = jest.fn();
|
|
|
|
const onSave = jest.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (duplicatedServers: ServerData[] = []) => renderWithEvents(
|
|
|
|
<DuplicatedServersModal isOpen duplicatedServers={duplicatedServers} onDiscard={onDiscard} onSave={onSave} />,
|
|
|
|
);
|
2021-12-30 23:40:08 +03:00
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
2022-01-01 14:35:06 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[[], 0],
|
|
|
|
[[Mock.all<ServerData>()], 2],
|
|
|
|
[[Mock.all<ServerData>(), Mock.all<ServerData>()], 2],
|
|
|
|
[[Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>()], 3],
|
|
|
|
[[Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>(), Mock.all<ServerData>()], 4],
|
2022-01-01 14:35:06 +03:00
|
|
|
])('renders expected amount of items', (duplicatedServers, expectedItems) => {
|
2022-06-04 19:54:34 +03:00
|
|
|
setUp(duplicatedServers);
|
|
|
|
expect(screen.queryAllByRole('listitem')).toHaveLength(expectedItems);
|
2022-01-01 14:35:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[
|
2022-03-26 14:17:42 +03:00
|
|
|
[Mock.all<ServerData>()],
|
2022-01-01 14:35:06 +03:00
|
|
|
{
|
|
|
|
header: 'Duplicated server',
|
|
|
|
firstParagraph: 'There is already a server with:',
|
|
|
|
lastParagraph: 'Do you want to save this server anyway?',
|
|
|
|
discardBtn: 'Discard',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
2022-03-26 14:17:42 +03:00
|
|
|
[Mock.all<ServerData>(), Mock.all<ServerData>()],
|
2022-01-01 14:35:06 +03:00
|
|
|
{
|
|
|
|
header: 'Duplicated servers',
|
|
|
|
firstParagraph: 'The next servers already exist:',
|
|
|
|
lastParagraph: 'Do you want to ignore duplicated servers?',
|
|
|
|
discardBtn: 'Ignore duplicated',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
])('renders expected texts based on amount of servers', (duplicatedServers, assertions) => {
|
2022-06-04 19:54:34 +03:00
|
|
|
setUp(duplicatedServers);
|
2022-01-01 14:35:06 +03:00
|
|
|
|
2022-06-04 19:54:34 +03:00
|
|
|
expect(screen.getByRole('heading')).toHaveTextContent(assertions.header);
|
|
|
|
expect(screen.getByText(assertions.firstParagraph)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(assertions.lastParagraph)).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('button', { name: assertions.discardBtn })).toBeInTheDocument();
|
2022-01-01 14:35:06 +03:00
|
|
|
});
|
|
|
|
|
2021-12-30 23:40:08 +03:00
|
|
|
it.each([
|
2022-01-01 14:20:09 +03:00
|
|
|
[[]],
|
2022-03-26 14:17:42 +03:00
|
|
|
[[Mock.of<ServerData>({ url: 'url', apiKey: 'apiKey' })]],
|
2022-06-04 19:54:34 +03:00
|
|
|
[[
|
|
|
|
Mock.of<ServerData>({ url: 'url_1', apiKey: 'apiKey_1' }),
|
|
|
|
Mock.of<ServerData>({ url: 'url_2', apiKey: 'apiKey_2' }),
|
|
|
|
]],
|
2022-01-01 14:20:09 +03:00
|
|
|
])('displays provided server data', (duplicatedServers) => {
|
2022-06-04 19:54:34 +03:00
|
|
|
setUp(duplicatedServers);
|
2021-12-30 23:40:08 +03:00
|
|
|
|
2022-01-01 14:20:09 +03:00
|
|
|
if (duplicatedServers.length === 0) {
|
2022-06-04 19:54:34 +03:00
|
|
|
expect(screen.queryByRole('listitem')).not.toBeInTheDocument();
|
2022-01-01 14:35:06 +03:00
|
|
|
} else if (duplicatedServers.length === 1) {
|
2022-06-04 19:54:34 +03:00
|
|
|
const [firstItem, secondItem] = screen.getAllByRole('listitem');
|
|
|
|
|
|
|
|
expect(firstItem).toHaveTextContent(`URL: ${duplicatedServers[0].url}`);
|
|
|
|
expect(secondItem).toHaveTextContent(`API key: ${duplicatedServers[0].apiKey}`);
|
2022-01-01 14:35:06 +03:00
|
|
|
} else {
|
|
|
|
expect.assertions(duplicatedServers.length);
|
2022-06-04 19:54:34 +03:00
|
|
|
screen.getAllByRole('listitem').forEach((item, index) => {
|
2022-01-01 14:35:06 +03:00
|
|
|
const server = duplicatedServers[index];
|
2022-06-04 19:54:34 +03:00
|
|
|
expect(item).toHaveTextContent(`${server.url} - ${server.apiKey}`);
|
2022-01-01 14:35:06 +03:00
|
|
|
});
|
2022-01-01 14:20:09 +03:00
|
|
|
}
|
2021-12-30 23:40:08 +03:00
|
|
|
});
|
|
|
|
|
2022-06-04 19:54:34 +03:00
|
|
|
it('invokes onDiscard when appropriate button is clicked', async () => {
|
|
|
|
const { user } = setUp();
|
2021-12-30 23:40:08 +03:00
|
|
|
|
2022-06-04 19:54:34 +03:00
|
|
|
expect(onDiscard).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Discard' }));
|
2021-12-30 23:40:08 +03:00
|
|
|
expect(onDiscard).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-06-04 19:54:34 +03:00
|
|
|
it('invokes onSave when appropriate button is clicked', async () => {
|
|
|
|
const { user } = setUp();
|
2021-12-30 23:40:08 +03:00
|
|
|
|
2022-06-04 19:54:34 +03:00
|
|
|
expect(onSave).not.toHaveBeenCalled();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Save anyway' }));
|
2021-12-30 23:40:08 +03:00
|
|
|
expect(onSave).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|