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

37 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-12-22 12:05:32 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { Mock } from 'ts-mockery';
import { ShlinkApiError, ShlinkApiErrorProps } from '../../src/api/ShlinkApiError';
import { InvalidArgumentError, ProblemDetailsError } from '../../src/api/types';
describe('<ShlinkApiError />', () => {
2022-03-26 14:17:42 +03:00
let commonWrapper: ShallowWrapper;
2020-12-22 12:05:32 +03:00
const createWrapper = (props: ShlinkApiErrorProps) => {
2022-03-26 14:17:42 +03:00
commonWrapper = shallow(<ShlinkApiError {...props} />);
2020-12-22 12:05:32 +03:00
2022-03-26 14:17:42 +03:00
return commonWrapper;
2020-12-22 12:05:32 +03:00
};
2022-03-26 14:17:42 +03:00
afterEach(() => commonWrapper?.unmount());
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 wrapper = createWrapper({ errorData, fallbackMessage });
expect(wrapper.text()).toContain(expectedMessage);
});
it.each([
2022-03-26 14:17:42 +03:00
[undefined, 0],
[Mock.all<ProblemDetailsError>(), 0],
[Mock.of<InvalidArgumentError>({ type: '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) => {
const wrapper = createWrapper({ errorData });
const p = wrapper.find('p');
expect(p).toHaveLength(expectedElementsCount);
});
});