@rocketmakers/api-swr
Version:
Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.
98 lines (97 loc) • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const axios_1 = require("axios");
const api_1 = require("./api");
/**
* isAxiosResponse
*/
describe('isAxiosResponse', () => {
test('returns true if response is an AxiosResponse object', () => {
const response = {
data: { message: 'Hello World' },
status: 200,
statusText: 'OK',
headers: {},
config: {},
};
const result = (0, api_1.isAxiosResponse)(response);
expect(result).toBe(true);
});
test('returns false if response is not an AxiosResponse object', () => {
const response = { message: 'Hello World' };
const result = (0, api_1.isAxiosResponse)(response);
expect(result).toBe(false);
});
});
/**
* isAxiosError
*/
describe('isAxiosError', () => {
it('should return true for an AxiosError', () => {
const axiosError = new axios_1.AxiosError('Some error', '404');
expect((0, api_1.isAxiosError)(axiosError)).toBe(true);
});
it('should return false for a non-AxiosError', () => {
const genericError = new Error('Some error');
expect((0, api_1.isAxiosError)(genericError)).toBe(false);
});
it('should return false for undefined', () => {
expect((0, api_1.isAxiosError)(undefined)).toBe(false);
});
it('should return false for null', () => {
expect((0, api_1.isAxiosError)(null)).toBe(false);
});
it('should return false for an object without isAxiosError property', () => {
const customError = { message: 'Custom error' };
expect((0, api_1.isAxiosError)(customError)).toBe(false);
});
it('should return false for a string', () => {
const errorMessage = 'Some error message';
expect((0, api_1.isAxiosError)(errorMessage)).toBe(false);
});
});
/**
* fixGeneratedClient
*/
describe('fixGeneratedClient', () => {
class OriginalClient {
constructor() {
this._privateProp = 'private';
}
publicMethod() {
return 'public';
}
privateMethod() {
return 'private';
}
}
it('should return a new object when executed', () => {
const original = new OriginalClient();
const fixed = (0, api_1.fixGeneratedClient)(original);
expect(fixed).not.toBe(original);
});
it('should correct the scoping issue of public methods when executed', () => {
const original = new OriginalClient();
const fixed = (0, api_1.fixGeneratedClient)(original);
expect(fixed.publicMethod()).toEqual('public');
});
});
/**
* processAxiosPromise
*/
describe('processAxiosPromise', () => {
it('should throw an error if the response status is 400 or higher when executed', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const response = { status: 404, statusText: 'Not Found' };
const mockFunc = jest.fn().mockResolvedValue(Promise.resolve(response));
yield expect((0, api_1.processAxiosPromise)(mockFunc)).rejects.toThrow('Not Found');
expect(mockFunc).toHaveBeenCalled();
}));
it('should return non-axios responses unchanged when executed with a non-axios response', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const data = { foo: 'bar' };
const mockFunc = jest.fn().mockResolvedValue(data);
const result = yield (0, api_1.processAxiosPromise)(mockFunc);
expect(mockFunc).toHaveBeenCalled();
expect(result).toEqual(data);
}));
});