fix: add api response factory tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { AxiosResponse } from "axios";
|
||||
import { HttpStatusCode } from "../enums/HttpStatusCode";
|
||||
import { ApiResponse } from "./ApiResponse";
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { HttpStatusCode } from '../enums/HttpStatusCode';
|
||||
import { ApiResponse } from './ApiResponse';
|
||||
|
||||
/**
|
||||
* @class ApiResponseFactory - Factory class for creating ApiResponse objects
|
||||
@@ -14,8 +14,7 @@ export class ApiResponseFactory {
|
||||
public static getApiResponse<T>(response: AxiosResponse): ApiResponse<T> {
|
||||
const message = this.TryToGetMessage(response);
|
||||
|
||||
if (this.isSuccessStatusCode(response.status) === true)
|
||||
{
|
||||
if (this.isSuccessStatusCode(response.status) === true) {
|
||||
const data = response.data as T;
|
||||
return new ApiResponse<T>(response.status, message, data);
|
||||
}
|
||||
@@ -29,22 +28,19 @@ export class ApiResponseFactory {
|
||||
* @returns {string} - The message from the response data
|
||||
*/
|
||||
private static TryToGetMessage(response: AxiosResponse): string {
|
||||
|
||||
if (
|
||||
response.status == HttpStatusCode.Unauthorized ||
|
||||
response.status == HttpStatusCode.Forbidden ||
|
||||
response.status == HttpStatusCode.NotFound
|
||||
)
|
||||
{
|
||||
) {
|
||||
return response.data?.message;
|
||||
}
|
||||
|
||||
if (this.isSuccessStatusCode(response.status) === false)
|
||||
{
|
||||
if (this.isSuccessStatusCode(response.status) === false) {
|
||||
return JSON.stringify(response.data);
|
||||
}
|
||||
|
||||
return "";
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { ApiResponseFactory } from '../src/models/ApiResponseFactory';
|
||||
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('ApiResponseFactory', function () {
|
||||
it('should be defined', function () {
|
||||
expect(ApiResponseFactory).to.not.be.undefined;
|
||||
});
|
||||
|
||||
describe('getApiResponse', function () {
|
||||
it('should be defined', function () {
|
||||
expect(ApiResponseFactory.getApiResponse).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should have 1 parameter', function () {
|
||||
expect(ApiResponseFactory.getApiResponse).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('should return an ApiResponse object', function () {
|
||||
const response: AxiosResponse = {
|
||||
data: null,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
};
|
||||
|
||||
const apiResponse = ApiResponseFactory.getApiResponse(response);
|
||||
|
||||
expect(apiResponse).to.not.be.undefined;
|
||||
expect(apiResponse).to.have.property('statusCode');
|
||||
expect(apiResponse).to.have.property('message');
|
||||
expect(apiResponse).to.have.property('data');
|
||||
expect(apiResponse.statusCode).to.equal(200);
|
||||
expect(apiResponse.message).to.equal('');
|
||||
expect(apiResponse.data).to.equal(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user