2022-05-02 20:34:33 +03:00
|
|
|
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';
|
2022-10-12 11:35:16 +03:00
|
|
|
import { ErrorTypeV2, ErrorTypeV3, InvalidArgumentError, ProblemDetailsError } from '../../src/api/types/errors';
|
2020-12-22 12:05:32 +03:00
|
|
|
|
|
|
|
describe('<ShlinkApiError />', () => {
|
2022-05-02 20:34:33 +03:00
|
|
|
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) => {
|
2022-05-02 20:34:33 +03:00
|
|
|
const { container } = setUp({ errorData, fallbackMessage });
|
2020-12-22 12:05:32 +03:00
|
|
|
|
2022-05-02 20:34:33 +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],
|
2022-10-12 11:35:16 +03:00
|
|
|
[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) => {
|
2022-05-02 20:34:33 +03:00
|
|
|
setUp({ errorData });
|
|
|
|
expect(screen.queryAllByText(/^Invalid elements/)).toHaveLength(expectedElementsCount);
|
2020-12-22 12:05:32 +03:00
|
|
|
});
|
|
|
|
});
|