2022-05-10 21:54:14 +03:00
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2021-08-24 21:13:54 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
|
|
|
import { DomainsList } from '../../src/domains/reducers/domainsList';
|
|
|
|
import { ManageDomains } from '../../src/domains/ManageDomains';
|
|
|
|
import { ProblemDetailsError, ShlinkDomain } from '../../src/api/types';
|
2021-12-09 15:44:29 +03:00
|
|
|
import { SelectedServer } from '../../src/servers/data';
|
2021-08-24 21:13:54 +03:00
|
|
|
|
|
|
|
describe('<ManageDomains />', () => {
|
|
|
|
const listDomains = jest.fn();
|
|
|
|
const filterDomains = jest.fn();
|
2022-05-10 21:54:14 +03:00
|
|
|
const setUp = (domainsList: DomainsList) => ({
|
|
|
|
user: userEvent.setup(),
|
|
|
|
...render(
|
|
|
|
<ManageDomains
|
|
|
|
listDomains={listDomains}
|
|
|
|
filterDomains={filterDomains}
|
|
|
|
editDomainRedirects={jest.fn()}
|
|
|
|
checkDomainHealth={jest.fn()}
|
|
|
|
domainsList={domainsList}
|
|
|
|
selectedServer={Mock.all<SelectedServer>()}
|
|
|
|
/>,
|
|
|
|
),
|
|
|
|
});
|
2021-08-24 21:13:54 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
it('shows loading message while domains are loading', () => {
|
2022-05-09 20:23:35 +03:00
|
|
|
setUp(Mock.of<DomainsList>({ 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 :('],
|
|
|
|
[Mock.of<ProblemDetailsError>(), 'Error loading domains :('],
|
|
|
|
[Mock.of<ProblemDetailsError>({ detail: 'Foo error!!' }), 'Foo error!!'],
|
|
|
|
])('shows error result when domains loading fails', (errorData, expectedErrorMessage) => {
|
|
|
|
setUp(Mock.of<DomainsList>({ 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 () => {
|
2022-05-10 21:54:14 +03:00
|
|
|
const { user } = setUp(Mock.of<DomainsList>({ 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', () => {
|
|
|
|
setUp(Mock.of<DomainsList>({ 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', () => {
|
2021-08-24 21:13:54 +03:00
|
|
|
const filteredDomains = [
|
|
|
|
Mock.of<ShlinkDomain>({ domain: 'foo' }),
|
|
|
|
Mock.of<ShlinkDomain>({ domain: 'bar' }),
|
|
|
|
Mock.of<ShlinkDomain>({ domain: 'baz' }),
|
|
|
|
];
|
2022-05-09 20:23:35 +03:00
|
|
|
setUp(Mock.of<DomainsList>({ 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
|
|
|
});
|
|
|
|
});
|