Files
onspring-api-sdk-javascript/tests/ApiResponse.spec.ts
T

183 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-01-26 22:40:13 -06:00
import { ApiResponse } from '../src/models/ApiResponse';
import { expect } from 'chai';
2023-02-01 21:35:30 -06:00
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
2023-01-26 22:40:13 -06:00
describe('ApiResponse', function () {
it('should be defined', function () {
expect(ApiResponse).to.not.be.undefined;
});
it('should have a constructor', function () {
expect(ApiResponse).to.have.property('constructor');
});
it('should have 3 parameters', function () {
expect(ApiResponse).to.have.lengthOf(3);
});
it('should create a new instance of the ApiResponse class', function () {
expect(() => new ApiResponse(200, 'a message', 'some data')).to.not.throw();
});
it('should have a property named statusCode', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data')).to.have.property(
'statusCode'
);
2023-01-26 22:40:13 -06:00
});
it('should have a property named isSuccessful', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data')).to.have.property(
'isSuccessful'
);
2023-01-26 22:40:13 -06:00
});
it('should have a property named message', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data')).to.have.property(
'message'
);
2023-01-26 22:40:13 -06:00
});
it('should have a property named data', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data')).to.have.property(
'data'
);
2023-01-26 22:40:13 -06:00
});
it('should set the statusCode property to the value passed to the constructor', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data').statusCode).to.equal(
200
);
2023-01-26 22:40:13 -06:00
});
it('should set the isSuccessful property to true when the statusCode is less than 400', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data').isSuccessful).to.be
.true;
2023-01-26 22:40:13 -06:00
});
it('should set the isSuccessful property to false when the statusCode is greater than or equal to 400', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(400, 'a message', 'some data').isSuccessful).to.be
.false;
2023-01-26 22:40:13 -06:00
});
it('should set the message property to the value passed to the constructor', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data').message).to.equal(
'a message'
);
2023-01-26 22:40:13 -06:00
});
it('should set the data property to the value passed to the constructor', function () {
2023-02-01 21:35:30 -06:00
expect(new ApiResponse(200, 'a message', 'some data').data).to.equal(
'some data'
);
2023-01-26 22:40:13 -06:00
});
2023-02-01 21:35:30 -06:00
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<GetPagedAppsResponse> when data contains app items', function () {
2023-02-01 21:35:30 -06:00
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<any>(200, 'OK', mockApiResponse);
var appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
expect(appsPagedResponse.data).to.not.be.null;
if (appsPagedResponse.data != null) {
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);
}
2023-02-01 21:35:30 -06:00
});
it('should return an ApiResponse<GetPagedAppsResponse> when data contains app items', function () {
var mockApiResponse = {
pageNumber: 0,
pageSize: 0,
totalPages: 0,
totalRecords: 0,
items: [],
};
var apiResponse = new ApiResponse<any>(200, 'OK', mockApiResponse);
var appsPagedResponse = apiResponse.AsGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
expect(appsPagedResponse.data).to.not.be.null;
if (appsPagedResponse.data != null) {
expect(appsPagedResponse.data.totalPages).to.equal(0);
expect(appsPagedResponse.data.totalRecords).to.equal(0);
expect(appsPagedResponse.data.pageNumber).to.equal(0);
expect(appsPagedResponse.data.pageSize).to.equal(0);
expect(appsPagedResponse.data.items).to.be.instanceOf(Array);
expect(appsPagedResponse.data.items).to.have.lengthOf(0);
}
});
2023-02-01 21:35:30 -06:00
});
describe('AsAppType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.AsAppType).to.not.be.undefined;
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.AsAppType).to.have.lengthOf(0);
});
it('should return an ApiResponse<App> when data contain an app', function () {
var mockApiResponse = {
href: 'https://api.onspring.dev/apps/id/1',
id: 1,
name: 'Test App',
};
var apiResponse = new ApiResponse<any>(200, 'OK', mockApiResponse);
var appResponse = apiResponse.AsAppType();
expect(appResponse).to.be.instanceOf(ApiResponse);
expect(appResponse.data).to.be.instanceOf(App);
expect(appResponse.data).to.not.be.null;
if (appResponse.data != null) {
expect(appResponse.data.id).to.equal(1);
expect(appResponse.data.name).to.equal('Test App');
expect(appResponse.data.href).to.equal(
'https://api.onspring.dev/apps/id/1'
);
}
});
});
2023-02-01 21:35:30 -06:00
});