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

32 lines
1.6 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiErrorProps } from '../../shlink-web-component/src/common/ShlinkApiError';
import { ShlinkApiError } from '../../shlink-web-component/src/common/ShlinkApiError';
2023-02-18 12:40:37 +03:00
import type { InvalidArgumentError, ProblemDetailsError } from '../../src/api/types/errors';
import { ErrorTypeV2, ErrorTypeV3 } 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'],
[fromPartial<ProblemDetailsError>({}), 'the fallback', 'the fallback'],
[fromPartial<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],
[fromPartial<ProblemDetailsError>({}), 0],
[fromPartial<InvalidArgumentError>({ type: ErrorTypeV2.INVALID_ARGUMENT, invalidElements: [] }), 1],
[fromPartial<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
});
});