import { fireEvent, render, screen, waitFor } from '@testing-library/react'; 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'; import { SelectedServer } from '../../src/servers/data'; describe('', () => { const listDomains = jest.fn(); const filterDomains = jest.fn(); const setUp = (domainsList: DomainsList) => render( ()} />, ); afterEach(jest.clearAllMocks); it('shows loading message while domains are loading', () => { setUp(Mock.of({ loading: true, filteredDomains: [] })); expect(screen.getByText('Loading...')).toBeInTheDocument(); expect(screen.queryByText('Error loading domains :(')).not.toBeInTheDocument(); }); it.each([ [undefined, 'Error loading domains :('], [Mock.of(), 'Error loading domains :('], [Mock.of({ detail: 'Foo error!!' }), 'Foo error!!'], ])('shows error result when domains loading fails', (errorData, expectedErrorMessage) => { setUp(Mock.of({ loading: false, error: true, errorData, filteredDomains: [] })); expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); expect(screen.getByText(expectedErrorMessage)).toBeInTheDocument(); }); it('filters domains when SearchField changes', async () => { setUp(Mock.of({ loading: false, error: false, filteredDomains: [] })); expect(filterDomains).not.toHaveBeenCalled(); fireEvent.change(screen.getByPlaceholderText('Search...'), { target: { value: 'Foo' } }); await waitFor(() => expect(filterDomains).toHaveBeenCalledTimes(1)); }); it('shows expected headers and one row when list of domains is empty', () => { setUp(Mock.of({ loading: false, error: false, filteredDomains: [] })); expect(screen.getAllByRole('columnheader')).toHaveLength(7); expect(screen.getByText('No results found')).toBeInTheDocument(); }); it('has many rows if multiple domains are provided', () => { const filteredDomains = [ Mock.of({ domain: 'foo' }), Mock.of({ domain: 'bar' }), Mock.of({ domain: 'baz' }), ]; setUp(Mock.of({ loading: false, error: false, filteredDomains })); expect(screen.getAllByRole('row')).toHaveLength(filteredDomains.length + 1); expect(screen.getByText('foo')).toBeInTheDocument(); expect(screen.getByText('bar')).toBeInTheDocument(); expect(screen.getByText('baz')).toBeInTheDocument(); }); });