mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-09-24 23:01:18 +08:00
* fix: remove circular dependency redux depended on templates/Challenges/redux and vice versa. This meant that import order mattered and confusing bugs could arise. (cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc) * feat: require imports to be in alphabetical order Import order generally does not matter, but there are edge cases (circular imports and css imports, for example) where changing order changes behaviour (cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c) * chore: order imports * fix: lift up challenge description + title comps This brings the classic Show closer to the others as they now all create the description and title components * fix: remove donation-saga/index circular import (cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207) * refactor: extract action-types from settings (cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd) * fix: lint errors * feat: prevent useless renames
152 lines
4.5 KiB
TypeScript
152 lines
4.5 KiB
TypeScript
import type { AxiosError } from 'axios';
|
|
import { isObject } from 'lodash-es';
|
|
|
|
import {
|
|
isHandledError,
|
|
wrapHandledError,
|
|
unwrapHandledError,
|
|
handledErrorSymbol,
|
|
handleAPIError
|
|
} from './handled-error';
|
|
import type { HandledError } from './handled-error';
|
|
|
|
import reportedErrorMessage from './reported-error-message';
|
|
|
|
describe('client/src utilities', () => {
|
|
describe('handled-error.js', () => {
|
|
const mockHandledErrorData = {
|
|
type: 'info',
|
|
message: 'something helpful',
|
|
redirectTo: '/a-path-we-choose'
|
|
};
|
|
|
|
describe('isHandledError', () => {
|
|
it('returns a boolean', () => {
|
|
expect(typeof isHandledError({} as Error)).toEqual('boolean');
|
|
});
|
|
|
|
it('returns false for an unhandled error', () => {
|
|
expect(isHandledError(new Error())).toEqual(false);
|
|
});
|
|
|
|
it('returns true for a handled error', () => {
|
|
const handledError = new Error() as HandledError;
|
|
handledError[handledErrorSymbol] = {};
|
|
|
|
expect(isHandledError(handledError)).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('wrapHandledError', () => {
|
|
// this is testing implementation details 👎
|
|
// we need to make these tests more robust 💪
|
|
it('returns an error with a handledError property', () => {
|
|
const handledError = wrapHandledError(
|
|
new Error() as HandledError,
|
|
mockHandledErrorData
|
|
);
|
|
expect(handledErrorSymbol in handledError).toEqual(true);
|
|
});
|
|
it('assigns error handling details to the handledError property', () => {
|
|
const handledError = wrapHandledError(
|
|
new Error() as HandledError,
|
|
mockHandledErrorData
|
|
);
|
|
expect(handledError[handledErrorSymbol]).toEqual(mockHandledErrorData);
|
|
});
|
|
});
|
|
|
|
describe('unwrapHandledError', () => {
|
|
// this is testing implementation details 👎
|
|
// we need to make these tests more robust 💪
|
|
it('returns an object by default', () => {
|
|
const error = new Error() as HandledError;
|
|
const unwrappedError = unwrapHandledError(error);
|
|
expect(isObject(unwrappedError)).toBe(true);
|
|
});
|
|
|
|
it('returns the data that was wrapped in the error', () => {
|
|
const handledError = new Error() as HandledError;
|
|
handledError[handledErrorSymbol] = mockHandledErrorData;
|
|
const unwrapped = unwrapHandledError(handledError);
|
|
expect(unwrapped).toEqual(mockHandledErrorData);
|
|
});
|
|
});
|
|
|
|
describe('handleAPIError', () => {
|
|
let reportMock: () => void;
|
|
beforeEach(() => {
|
|
reportMock = jest.fn();
|
|
});
|
|
|
|
it('returns handled error data', () => {
|
|
expect.assertions(3);
|
|
const axiosErrorMock = {
|
|
response: {
|
|
status: 400
|
|
}
|
|
} as AxiosError;
|
|
const result = handleAPIError(
|
|
axiosErrorMock,
|
|
{ redirectTo: '/' },
|
|
reportMock
|
|
);
|
|
expect(result).toHaveProperty('type');
|
|
expect(result).toHaveProperty('message');
|
|
expect(result).toHaveProperty('redirectTo');
|
|
});
|
|
|
|
it('does not report 4** errors', () => {
|
|
expect.assertions(1);
|
|
for (let i = 400; i < 500; i++) {
|
|
const axiosErrorMock = {
|
|
response: {
|
|
status: i
|
|
}
|
|
} as AxiosError;
|
|
handleAPIError(axiosErrorMock, { redirectTo: '/' }, reportMock);
|
|
}
|
|
expect(reportMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('reports on 5** errors', () => {
|
|
const axiosErrorMock = {
|
|
response: {
|
|
status: 502
|
|
}
|
|
} as AxiosError;
|
|
handleAPIError(axiosErrorMock, { redirectTo: '/' }, reportMock);
|
|
expect(reportMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('returns a `reportedErrorMessage` for a 5** error', () => {
|
|
const axiosErrorMock = {
|
|
response: {
|
|
status: 502
|
|
}
|
|
} as AxiosError;
|
|
const result = handleAPIError(
|
|
axiosErrorMock,
|
|
{ redirectTo: '/' },
|
|
reportMock
|
|
);
|
|
expect(result).toEqual({ ...reportedErrorMessage, redirectTo: '/' });
|
|
});
|
|
|
|
it('respects a `null` redirectTo', () => {
|
|
const axiosErrorMock = {
|
|
response: {
|
|
status: 400
|
|
}
|
|
} as AxiosError;
|
|
const result = handleAPIError(
|
|
axiosErrorMock,
|
|
{ redirectTo: null },
|
|
reportMock
|
|
);
|
|
expect(result.redirectTo).toBe(null);
|
|
});
|
|
});
|
|
});
|
|
});
|