fix: work through how i want to return the api response with the data property being of the right type.

This commit is contained in:
StevanFreeborn
2023-02-01 21:21:39 -06:00
parent 5a6c5094e9
commit 4952bf30ec
9 changed files with 164 additions and 39 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ describe('EndpointFactory', function () {
describe('getAppsEndpoint', function () {
it('should return the correct apps endpoint with paging params based on paging request parameter passed', function () {
const result = EndpointFactory.getAppsEndpoint(baseUrl, new PagingRequest(2, 1000));
expect(result).to.equal(`${baseUrl}/Apps?page=2&pageSize=1000`);
expect(result).to.equal(`${baseUrl}/Apps?pageSize=1000&pageNumber=2`);
});
});
+4 -3
View File
@@ -12,17 +12,18 @@ describe('GetPagedAppsResponse', function () {
});
it('should have 4 parameters', function () {
expect(GetPagedAppsResponse).to.have.lengthOf(4);
expect(GetPagedAppsResponse).to.have.lengthOf(5);
});
it('should construct a new instance of GetPagedAppsResponse', function () {
const getPagedAppsResponse = new GetPagedAppsResponse([new App('test', 1, 'test'), new App('test', 1, 'test')], 1, 10, 100);
const getPagedAppsResponse = new GetPagedAppsResponse([new App('test', 1, 'test'), new App('test', 1, 'test')], 1, 10, 100, 100);
expect(getPagedAppsResponse).to.not.be.undefined;
expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse);
expect(getPagedAppsResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(2);
expect(getPagedAppsResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
expect(getPagedAppsResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
expect(getPagedAppsResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
expect(getPagedAppsResponse).to.have.property('totalPages').to.be.a('number').to.equal(100);
expect(getPagedAppsResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100);
});
});
+112 -15
View File
@@ -3,6 +3,8 @@ import { ApiResponse } from '../src/models/ApiResponse';
import axios, { AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev';
@@ -109,22 +111,16 @@ describe('OnspringClient', function () {
it('should return a promise that resolves to a boolean', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const result = await client.canConnect();
expect(result).to.be.a('boolean');
});
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockClient = axios.create({
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockClient, 'get').returns(Promise.resolve({
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
status: 200,
statusText: 'OK',
data: null,
@@ -132,7 +128,32 @@ describe('OnspringClient', function () {
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse));
sinon.stub(client, '_client' as any).value(mockClient);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.canConnect();
expect(result).to.be.a('boolean');
});
it('should return a promise that resolves to true when able to connect to the Onspring API', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
status: 200,
statusText: 'OK',
data: null,
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse));
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.canConnect();
expect(result).to.be.true;
@@ -140,8 +161,8 @@ describe('OnspringClient', function () {
it('should return a promise that resolves to false when unable to connect to the Onspring API', async function () {
const client = new OnspringClient(baseUrl, apiKey);
// sinon.stub(client, 'get' as any).returns(Promise.resolve(new ApiResponse(500, 'Internal Server Error', null)));
const mockClient = axios.create({
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
@@ -149,7 +170,7 @@ describe('OnspringClient', function () {
},
});
sinon.stub(mockClient, 'get').returns(Promise.reject({
sinon.stub(mockAxiosClient, 'get').returns(Promise.resolve({
status: 500,
statusText: 'Internal Server Error',
data: null,
@@ -157,10 +178,86 @@ describe('OnspringClient', function () {
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse));
sinon.stub(client, '_client' as any).value(mockClient);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.canConnect();
expect(result).to.be.false;
});
});
describe('getApps', function () {
it('should be defined', function () {
expect(new OnspringClient(baseUrl, apiKey).getApps).to.not.be.undefined;
});
it('should be a function', function () {
expect(new OnspringClient(baseUrl, apiKey).getApps).to.be.a('function');
});
it('should have 0 parameters', function () {
expect(new OnspringClient(baseUrl, apiKey).getApps).to.have.lengthOf(0);
});
it('should return a promise', function () {
expect(new OnspringClient(baseUrl, apiKey).getApps()).to.be.a('promise');
});
it('should return a promise that resolves to a paged response of apps when request is successful', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'get').returns(
Promise.resolve({
status: 200,
statusText: 'OK',
data: {
pageNumber: 1,
pageSize: 2,
totalPages: 1,
totalRecords: 2,
items: [
{
href: 'https://api.onspring.dev/Apps/id/1',
id: '1',
name: 'Test App 1',
},
{
href: 'https://api.onspring.dev/Apps/id/2',
id: '2',
name: 'Test App 2',
},
],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getApps();
expect(result).to.be.instanceOf(ApiResponse<GetPagedAppsResponse>);
expect(result).to.have.property('statusCode', 200);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data');
expect(result.data).to.be.instanceOf(GetPagedAppsResponse);
expect(result.data).to.have.property('pageNumber', 1);
expect(result.data).to.have.property('pageSize', 2);
expect(result.data).to.have.property('totalPages', 1);
expect(result.data).to.have.property('totalRecords', 2);
expect(result.data).to.have.property('items');
expect(result.data.items).to.be.instanceOf(Array);
expect(result.data.items).to.have.lengthOf(2);
expect(result.data.items[0]).to.be.instanceOf(App);
expect(result.data.items[1]).to.be.instanceOf(App);
});
});
});
+4 -3
View File
@@ -11,17 +11,18 @@ describe('PagedResponse', function () {
});
it('should have 4 parameters', function () {
expect(PagedResponse).to.have.lengthOf(4);
expect(PagedResponse).to.have.lengthOf(5);
});
it('should construct a new instance of PagedResponse', function () {
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100);
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100, 100);
expect(pagedResponse).to.not.be.undefined;
expect(pagedResponse).to.be.instanceOf(PagedResponse);
expect(pagedResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(4);
expect(pagedResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1);
expect(pagedResponse).to.have.property('pageSize').to.be.a('number').to.equal(10);
expect(pagedResponse).to.have.property('totalCount').to.be.a('number').to.equal(100);
expect(pagedResponse).to.have.property('totalPages').to.be.a('number').to.equal(100);
expect(pagedResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100);
});
});