From 0bb9b073d5e20eac46861ead244f33dfd400acbf Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Wed, 1 Feb 2023 21:35:30 -0600 Subject: [PATCH] fix: added test for new api response method --- src/models/ApiResponse.ts | 4 ++ tests/ApiResponse.spec.ts | 86 ++++++++++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/models/ApiResponse.ts b/src/models/ApiResponse.ts index c005b28..87eafbd 100644 --- a/src/models/ApiResponse.ts +++ b/src/models/ApiResponse.ts @@ -39,6 +39,10 @@ export class ApiResponse { this.data = data; } + /** + * @method AsGetPagedAppsResponseType - Converts the ApiResponse to an ApiResponse. + * @returns {ApiResponse} - An ApiResponse. + */ public AsGetPagedAppsResponseType(): ApiResponse { var apiResponse = this as ApiResponse; var apps = apiResponse.data.items.map((item: any) => { diff --git a/tests/ApiResponse.spec.ts b/tests/ApiResponse.spec.ts index ca44cae..3ee8eeb 100644 --- a/tests/ApiResponse.spec.ts +++ b/tests/ApiResponse.spec.ts @@ -1,5 +1,7 @@ import { ApiResponse } from '../src/models/ApiResponse'; import { expect } from 'chai'; +import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse'; +import { App } from '../src/models/App'; describe('ApiResponse', function () { it('should be defined', function () { @@ -19,38 +21,102 @@ describe('ApiResponse', function () { }); it('should have a property named statusCode', function () { - expect(new ApiResponse(200, 'a message', 'some data')).to.have.property('statusCode'); + expect(new ApiResponse(200, 'a message', 'some data')).to.have.property( + 'statusCode' + ); }); it('should have a property named isSuccessful', function () { - expect(new ApiResponse(200, 'a message', 'some data')).to.have.property('isSuccessful'); + expect(new ApiResponse(200, 'a message', 'some data')).to.have.property( + 'isSuccessful' + ); }); it('should have a property named message', function () { - expect(new ApiResponse(200, 'a message', 'some data')).to.have.property('message'); + expect(new ApiResponse(200, 'a message', 'some data')).to.have.property( + 'message' + ); }); it('should have a property named data', function () { - expect(new ApiResponse(200, 'a message', 'some data')).to.have.property('data'); + expect(new ApiResponse(200, 'a message', 'some data')).to.have.property( + 'data' + ); }); it('should set the statusCode property to the value passed to the constructor', function () { - expect(new ApiResponse(200, 'a message', 'some data').statusCode).to.equal(200); + expect(new ApiResponse(200, 'a message', 'some data').statusCode).to.equal( + 200 + ); }); it('should set the isSuccessful property to true when the statusCode is less than 400', function () { - expect(new ApiResponse(200, 'a message', 'some data').isSuccessful).to.be.true; + expect(new ApiResponse(200, 'a message', 'some data').isSuccessful).to.be + .true; }); it('should set the isSuccessful property to false when the statusCode is greater than or equal to 400', function () { - expect(new ApiResponse(400, 'a message', 'some data').isSuccessful).to.be.false; + expect(new ApiResponse(400, 'a message', 'some data').isSuccessful).to.be + .false; }); it('should set the message property to the value passed to the constructor', function () { - expect(new ApiResponse(200, 'a message', 'some data').message).to.equal('a message'); + expect(new ApiResponse(200, 'a message', 'some data').message).to.equal( + 'a message' + ); }); it('should set the data property to the value passed to the constructor', function () { - expect(new ApiResponse(200, 'a message', 'some data').data).to.equal('some data'); + expect(new ApiResponse(200, 'a message', 'some data').data).to.equal( + 'some data' + ); }); -}); \ No newline at end of file + + describe('AsGetPagedAppsResponseType', function () { + it('should be defined', function () { + expect(ApiResponse.prototype.AsGetPagedAppsResponseType).to.not.be + .undefined; + }); + + it('should have no parameters', function () { + expect(ApiResponse.prototype.AsGetPagedAppsResponseType).to.have.lengthOf( + 0 + ); + }); + + it('should return an ApiResponse', function () { + var mockApiResponse = { + pageNumber: 1, + pageSize: 2, + totalPages: 1, + totalRecords: 2, + items: [ + { + href: 'https://api.onspring.dev/apps/id/1', + id: 1, + name: 'Test App', + }, + { + href: 'https://api.onspring.dev/apps/id/1', + id: 2, + name: 'Test App', + }, + ], + }; + + var apiResponse = new ApiResponse(200, 'OK', mockApiResponse); + var appsPagedResponse = apiResponse.AsGetPagedAppsResponseType(); + + expect(appsPagedResponse).to.be.instanceOf(ApiResponse); + expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse); + expect(appsPagedResponse.data.totalPages).to.equal(1); + expect(appsPagedResponse.data.totalRecords).to.equal(2); + expect(appsPagedResponse.data.pageNumber).to.equal(1); + expect(appsPagedResponse.data.pageSize).to.equal(2); + expect(appsPagedResponse.data.items).to.be.instanceOf(Array); + expect(appsPagedResponse.data.items).to.have.lengthOf(2); + expect(appsPagedResponse.data.items[0]).to.be.instanceOf(App); + expect(appsPagedResponse.data.items[1]).to.be.instanceOf(App); + }); + }); +});