shlink-web-client/test/api/ShlinkApiError.test.tsx

30 lines
1.4 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
2020-12-22 12:05:32 +03:00
import { Mock } from 'ts-mockery';
import { ShlinkApiError, ShlinkApiErrorProps } from '../../src/api/ShlinkApiError';
import { ErrorTypeV2, ErrorTypeV3, InvalidArgumentError, ProblemDetailsError } from '../../src/api/types/errors';
2020-12-22 12:05:32 +03:00
describe('<ShlinkApiError />', () => {
const setUp = (props: ShlinkApiErrorProps) => render(<ShlinkApiError {...props} />);
2020-12-22 12:05:32 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[undefined, 'the fallback', 'the fallback'],
[Mock.all<ProblemDetailsError>(), 'the fallback', 'the fallback'],
[Mock.of<ProblemDetailsError>({ detail: 'the detail' }), 'the fallback', 'the detail'],
2020-12-22 12:05:32 +03:00
])('renders proper message', (errorData, fallbackMessage, expectedMessage) => {
const { container } = setUp({ errorData, fallbackMessage });
2020-12-22 12:05:32 +03:00
expect(container.firstChild).toHaveTextContent(expectedMessage);
expect(screen.queryByRole('paragraph')).not.toBeInTheDocument();
2020-12-22 12:05:32 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[undefined, 0],
[Mock.all<ProblemDetailsError>(), 0],
[Mock.of<InvalidArgumentError>({ type: ErrorTypeV2.INVALID_ARGUMENT, invalidElements: [] }), 1],
[Mock.of<InvalidArgumentError>({ type: ErrorTypeV3.INVALID_ARGUMENT, invalidElements: [] }), 1],
2020-12-22 12:05:32 +03:00
])('renders list of invalid elements when provided error is an InvalidError', (errorData, expectedElementsCount) => {
setUp({ errorData });
expect(screen.queryAllByText(/^Invalid elements/)).toHaveLength(expectedElementsCount);
2020-12-22 12:05:32 +03:00
});
});