2023-08-04 09:56:06 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
import { ShlinkWebComponentContainer as createContainer } from '../../src/common/ShlinkWebComponentContainer';
|
|
|
|
import type { NonReachableServer, NotFoundServer, SelectedServer } from '../../src/servers/data';
|
|
|
|
|
|
|
|
vi.mock('react-router-dom', async () => ({
|
|
|
|
...(await vi.importActual<any>('react-router-dom')),
|
|
|
|
useParams: vi.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('<ShlinkWebComponentContainer />', () => {
|
|
|
|
const ShlinkWebComponentContainer = createContainer(
|
|
|
|
vi.fn().mockReturnValue(fromPartial({})),
|
2023-08-06 14:54:57 +03:00
|
|
|
fromPartial({}),
|
2023-08-04 09:56:06 +03:00
|
|
|
() => <>ShlinkWebComponent</>,
|
|
|
|
() => <>ServerError</>,
|
|
|
|
);
|
|
|
|
const setUp = (selectedServer: SelectedServer) => render(
|
2023-08-06 19:07:03 +03:00
|
|
|
<ShlinkWebComponentContainer selectServer={vi.fn()} selectedServer={selectedServer} settings={{}} />,
|
2023-08-04 09:56:06 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
(useParams as any).mockReturnValue({ serverId: 'abc123' });
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|