2022-07-10 00:03:21 +03:00
|
|
|
import { screen, waitFor } from '@testing-library/react';
|
2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShlinkDomain } from '../../src/api/types';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ProblemDetailsError } from '../../src/api/types/errors';
|
|
|
|
import { ManageDomains } from '../../src/domains/ManageDomains';
|
|
|
|
import type { DomainsList } from '../../src/domains/reducers/domainsList';
|
2022-07-10 20:44:49 +03:00
|
|
|
import { renderWithEvents } from '../__helpers__/setUpTest';
|
2021-08-24 21:13:54 +03:00
|
|
|
|
|
|
|
describe('<ManageDomains />', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const listDomains = vi.fn();
|
|
|
|
const filterDomains = vi.fn();
|
2022-07-10 00:03:21 +03:00
|
|
|
const setUp = (domainsList: DomainsList) => renderWithEvents(
|
|
|
|
<ManageDomains
|
|
|
|
listDomains={listDomains}
|
|
|
|
filterDomains={filterDomains}
|
2023-05-27 12:57:26 +03:00
|
|
|
editDomainRedirects={vi.fn()}
|
|
|
|
checkDomainHealth={vi.fn()}
|
2022-07-10 00:03:21 +03:00
|
|
|
domainsList={domainsList}
|
2023-04-13 22:48:29 +03:00
|
|
|
selectedServer={fromPartial({})}
|
2022-07-10 00:03:21 +03:00
|
|
|
/>,
|
|
|
|
);
|
2021-08-24 21:13:54 +03:00
|
|
|
|
|
|
|
it('shows loading message while domains are loading', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ loading: true, filteredDomains: [] }));
|
2021-08-24 21:13:54 +03:00
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText('Error loading domains :(')).not.toBeInTheDocument();
|
2021-08-24 21:13:54 +03:00
|
|
|
});
|
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
it.each([
|
|
|
|
[undefined, 'Error loading domains :('],
|
2023-04-13 22:48:29 +03:00
|
|
|
[fromPartial<ProblemDetailsError>({}), 'Error loading domains :('],
|
|
|
|
[fromPartial<ProblemDetailsError>({ detail: 'Foo error!!' }), 'Foo error!!'],
|
2022-05-09 20:23:35 +03:00
|
|
|
])('shows error result when domains loading fails', (errorData, expectedErrorMessage) => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ loading: false, error: true, errorData, filteredDomains: [] }));
|
2021-08-24 21:13:54 +03:00
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByText(expectedErrorMessage)).toBeInTheDocument();
|
2021-08-24 21:13:54 +03:00
|
|
|
});
|
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
it('filters domains when SearchField changes', async () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const { user } = setUp(fromPartial({ loading: false, error: false, filteredDomains: [] }));
|
2021-08-24 21:13:54 +03:00
|
|
|
|
|
|
|
expect(filterDomains).not.toHaveBeenCalled();
|
2022-05-10 21:54:14 +03:00
|
|
|
await user.type(screen.getByPlaceholderText('Search...'), 'Foo');
|
|
|
|
await waitFor(() => expect(filterDomains).toHaveBeenCalledWith('Foo'));
|
2021-08-24 21:13:54 +03:00
|
|
|
});
|
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
it('shows expected headers and one row when list of domains is empty', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ loading: false, error: false, filteredDomains: [] }));
|
2021-08-24 21:13:54 +03:00
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
expect(screen.getAllByRole('columnheader')).toHaveLength(7);
|
|
|
|
expect(screen.getByText('No results found')).toBeInTheDocument();
|
2021-08-24 21:13:54 +03:00
|
|
|
});
|
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
it('has many rows if multiple domains are provided', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
const filteredDomains: ShlinkDomain[] = [
|
|
|
|
fromPartial({ domain: 'foo' }),
|
|
|
|
fromPartial({ domain: 'bar' }),
|
|
|
|
fromPartial({ domain: 'baz' }),
|
2021-08-24 21:13:54 +03:00
|
|
|
];
|
2023-04-13 22:48:29 +03:00
|
|
|
setUp(fromPartial({ loading: false, error: false, filteredDomains }));
|
2021-08-24 21:13:54 +03:00
|
|
|
|
2022-05-09 20:23:35 +03:00
|
|
|
expect(screen.getAllByRole('row')).toHaveLength(filteredDomains.length + 1);
|
|
|
|
expect(screen.getByText('foo')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('bar')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('baz')).toBeInTheDocument();
|
2021-08-24 21:13:54 +03:00
|
|
|
});
|
|
|
|
});
|