shlink-web-client/test/common/ShlinkWebComponentContainer.test.tsx

50 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-08-04 09:56:06 +03:00
import { render, screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
2023-09-05 10:08:42 +03:00
import { ShlinkWebComponentContainerFactory } from '../../src/common/ShlinkWebComponentContainer';
2023-08-04 09:56:06 +03:00
import type { NonReachableServer, NotFoundServer, SelectedServer } from '../../src/servers/data';
2023-09-30 11:20:28 +03:00
import { checkAccessibility } from '../__helpers__/accessibility';
2023-12-19 01:38:34 +03:00
import { MemoryRouterWithParams } from '../__helpers__/MemoryRouterWithParams';
2023-08-04 09:56:06 +03:00
describe('<ShlinkWebComponentContainer />', () => {
2023-09-05 10:08:42 +03:00
const ShlinkWebComponentContainer = ShlinkWebComponentContainerFactory(fromPartial({
buildShlinkApiClient: vi.fn().mockReturnValue(fromPartial({})),
TagColorsStorage: fromPartial({}),
ShlinkWebComponent: () => <>ShlinkWebComponent</>,
ServerError: () => <>ServerError</>,
}));
2023-08-04 09:56:06 +03:00
const setUp = (selectedServer: SelectedServer) => render(
2023-12-19 01:38:34 +03:00
<MemoryRouterWithParams params={{ serverId: 'abc123' }}>
<ShlinkWebComponentContainer selectServer={vi.fn()} selectedServer={selectedServer} settings={{}} />
</MemoryRouterWithParams>,
2023-08-04 09:56:06 +03:00
);
2023-09-30 11:20:28 +03:00
it('passes a11y checks', () => checkAccessibility(setUp(fromPartial({ version: '3.0.0' }))));
2023-08-04 09:56:06 +03:00
it('shows loading indicator while loading server', () => {
setUp(null);
expect(screen.getByText('Loading...')).toBeInTheDocument();
expect(screen.queryByText('ServerError')).not.toBeInTheDocument();
expect(screen.queryByText('ShlinkWebComponent')).not.toBeInTheDocument();
});
it.each([
[fromPartial<NotFoundServer>({ serverNotFound: true })],
[fromPartial<NonReachableServer>({ serverNotReachable: true })],
])('shows error for non reachable servers', (selectedServer) => {
setUp(selectedServer);
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
expect(screen.getByText('ServerError')).toBeInTheDocument();
expect(screen.queryByText('ShlinkWebComponent')).not.toBeInTheDocument();
});
it('renders ShlinkWebComponent for reachable servers', () => {
setUp(fromPartial({ version: '3.0.0' }));
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
expect(screen.queryByText('ServerError')).not.toBeInTheDocument();
expect(screen.getByText('ShlinkWebComponent')).toBeInTheDocument();
});
});