fix: test formatting

This commit is contained in:
StevanFreeborn
2023-02-01 23:45:14 -06:00
parent f7229b5344
commit e6cde7fbe7
27 changed files with 156 additions and 82 deletions
+3 -13
View File
@@ -2,18 +2,8 @@
"extends": "@istanbuljs/nyc-config-typescript", "extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true, "check-coverage": true,
"all": true, "all": true,
"include": [ "include": ["src/**/*.ts"],
"src/**/*.ts" "exclude": ["src/index.ts", "**/*.spec.ts"],
], "reporter": ["html", "lcov", "text", "text-summary"],
"exclude": [
"src/index.ts",
"**/*.spec.ts"
],
"reporter": [
"html",
"lcov",
"text",
"text-summary"
],
"report-dir": "coverage" "report-dir": "coverage"
} }
+8 -2
View File
@@ -37,7 +37,10 @@ export class OnspringClient {
throw new Error('apiKey cannot be null/empty/whitespace.'); throw new Error('apiKey cannot be null/empty/whitespace.');
} }
this._client = axios.create({ baseURL: baseUrl, headers: { 'x-apikey': apiKey, 'x-api-version': '2',},}); this._client = axios.create({
baseURL: baseUrl,
headers: { 'x-apikey': apiKey, 'x-api-version': '2' },
});
} }
/** /**
@@ -95,7 +98,10 @@ export class OnspringClient {
* @param {string} endpoint - The endpoint that will be used to make the request. * @param {string} endpoint - The endpoint that will be used to make the request.
* @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T. * @returns {Promise<ApiResponse<T>>} - A promise that resolves to an ApiResponse of type T.
*/ */
private async get<T>(endpoint: string, config: AxiosRequestConfig = {}): Promise<ApiResponse<T>> { private async get<T>(
endpoint: string,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.get(endpoint, config); const response = await this._client.get(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response); const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse; return apiResponse;
+5 -2
View File
@@ -1,4 +1,4 @@
import { PagingRequest } from "./PagingRequest"; import { PagingRequest } from './PagingRequest';
/** /**
* @class EndpointFactory - A factory class for creating endpoints. * @class EndpointFactory - A factory class for creating endpoints.
@@ -16,7 +16,10 @@ export class EndpointFactory {
* @param {string} baseUrl - The base url that will be used to create the apps endpoint. * @param {string} baseUrl - The base url that will be used to create the apps endpoint.
* @returns {string} - The apps endpoint. * @returns {string} - The apps endpoint.
*/ */
public static getAppsEndpoint(baseUrl: string, pagingRequest: PagingRequest): string { public static getAppsEndpoint(
baseUrl: string,
pagingRequest: PagingRequest
): string {
return `${baseUrl}/Apps?pageSize=${pagingRequest.pageSize}&pageNumber=${pagingRequest.pageNumber}`; return `${baseUrl}/Apps?pageSize=${pagingRequest.pageSize}&pageNumber=${pagingRequest.pageNumber}`;
} }
+9 -3
View File
@@ -1,8 +1,14 @@
import { PagedResponse } from "./PagedResponse"; import { PagedResponse } from './PagedResponse';
import { App } from "./App"; import { App } from './App';
export class GetPagedAppsResponse extends PagedResponse<App> { export class GetPagedAppsResponse extends PagedResponse<App> {
constructor(items: App[], pageNumber: number, pageSize: number, totalPages: number, totalRecords: number) { constructor(
items: App[],
pageNumber: number,
pageSize: number,
totalPages: number,
totalRecords: number
) {
super(items, pageNumber, pageSize, totalPages, totalRecords); super(items, pageNumber, pageSize, totalPages, totalRecords);
} }
} }
+7 -1
View File
@@ -5,7 +5,13 @@ export class PagedResponse<T> {
public totalPages: number; public totalPages: number;
public totalRecords: number; public totalRecords: number;
constructor(items: T[], pageNumber: number, pageSize: number, totalPages: number, totalRecords: number) { constructor(
items: T[],
pageNumber: number,
pageSize: number,
totalPages: number,
totalRecords: number
) {
this.items = items; this.items = items;
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
this.pageSize = pageSize; this.pageSize = pageSize;
+3 -3
View File
@@ -1,4 +1,4 @@
import { ArgumentValidator } from "./ArgumentValidator"; import { ArgumentValidator } from './ArgumentValidator';
/** /**
* @class PagingRequest - Paging request model * @class PagingRequest - Paging request model
@@ -24,11 +24,11 @@ export class PagingRequest {
*/ */
constructor(pageNumber: number, pageSize: number) { constructor(pageNumber: number, pageSize: number) {
if (ArgumentValidator.isValidPageNumber(pageNumber) === false) { if (ArgumentValidator.isValidPageNumber(pageNumber) === false) {
throw new Error("pageNumber must be greater than 0."); throw new Error('pageNumber must be greater than 0.');
} }
if (ArgumentValidator.isValidPageSize(pageSize) === false) { if (ArgumentValidator.isValidPageSize(pageSize) === false) {
throw new Error("pageSize must be greater than 0 and less than 1001."); throw new Error('pageSize must be greater than 0 and less than 1001.');
} }
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
+7 -5
View File
@@ -59,7 +59,7 @@ describe('ApiResponseFactory', function () {
it('should return an ApiResponse object when request is forbidden and the response contains a message property', function () { it('should return an ApiResponse object when request is forbidden and the response contains a message property', function () {
const response: AxiosResponse = { const response: AxiosResponse = {
data: { data: {
message: 'Does not have permission to access this resource.' message: 'Does not have permission to access this resource.',
}, },
status: 403, status: 403,
statusText: 'Forbidden', statusText: 'Forbidden',
@@ -74,7 +74,9 @@ describe('ApiResponseFactory', function () {
expect(apiResponse).to.have.property('message'); expect(apiResponse).to.have.property('message');
expect(apiResponse).to.have.property('data'); expect(apiResponse).to.have.property('data');
expect(apiResponse.statusCode).to.equal(403); expect(apiResponse.statusCode).to.equal(403);
expect(apiResponse.message).to.equal('Does not have permission to access this resource.'); expect(apiResponse.message).to.equal(
'Does not have permission to access this resource.'
);
expect(apiResponse.data).to.equal(null); expect(apiResponse.data).to.equal(null);
}); });
@@ -101,7 +103,7 @@ describe('ApiResponseFactory', function () {
it('should return an ApiResponse object with a message value when request is not found and the response contains a message property', function () { it('should return an ApiResponse object with a message value when request is not found and the response contains a message property', function () {
const response: AxiosResponse = { const response: AxiosResponse = {
data: { data: {
message: 'Resource not found.' message: 'Resource not found.',
}, },
status: 404, status: 404,
statusText: 'Not Found', statusText: 'Not Found',
@@ -143,7 +145,7 @@ describe('ApiResponseFactory', function () {
it('should return an ApiResponse object with a message when request is unauthorized and the response contains a message property', function () { it('should return an ApiResponse object with a message when request is unauthorized and the response contains a message property', function () {
const response: AxiosResponse = { const response: AxiosResponse = {
data: { data: {
message: 'Unauthorized.' message: 'Unauthorized.',
}, },
status: 401, status: 401,
statusText: 'Unauthorized', statusText: 'Unauthorized',
@@ -165,7 +167,7 @@ describe('ApiResponseFactory', function () {
it('should return an ApiResponse object with a message when request is a bad request', function () { it('should return an ApiResponse object with a message when request is a bad request', function () {
const response: AxiosResponse = { const response: AxiosResponse = {
data: { data: {
field: 'Invalid input.' field: 'Invalid input.',
}, },
status: 400, status: 400,
statusText: 'Bad Request', statusText: 'Bad Request',
+4 -1
View File
@@ -14,7 +14,10 @@ describe('EndpointFactory', function () {
describe('getAppsEndpoint', function () { describe('getAppsEndpoint', function () {
it('should return the correct apps endpoint with paging params based on paging request parameter passed', 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)); const result = EndpointFactory.getAppsEndpoint(
baseUrl,
new PagingRequest(2, 1000)
);
expect(result).to.equal(`${baseUrl}/Apps?pageSize=1000&pageNumber=2`); expect(result).to.equal(`${baseUrl}/Apps?pageSize=1000&pageNumber=2`);
}); });
}); });
+27 -6
View File
@@ -16,14 +16,35 @@ describe('GetPagedAppsResponse', function () {
}); });
it('should construct a new instance of GetPagedAppsResponse', function () { 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, 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.not.be.undefined;
expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse); expect(getPagedAppsResponse).to.be.instanceOf(GetPagedAppsResponse);
expect(getPagedAppsResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(2); expect(getPagedAppsResponse)
expect(getPagedAppsResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1); .to.have.property('items')
expect(getPagedAppsResponse).to.have.property('pageSize').to.be.a('number').to.equal(10); .to.be.an('array')
expect(getPagedAppsResponse).to.have.property('totalPages').to.be.a('number').to.equal(100); .to.have.lengthOf(2);
expect(getPagedAppsResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100); 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('totalPages')
.to.be.a('number')
.to.equal(100);
expect(getPagedAppsResponse)
.to.have.property('totalRecords')
.to.be.a('number')
.to.equal(100);
}); });
}); });
+15 -5
View File
@@ -305,7 +305,10 @@ describe('OnspringClient', function () {
expect(result).to.be.instanceOf(ApiResponse); expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 400); expect(result).to.have.property('statusCode', 400);
expect(result).to.have.property('isSuccessful', false); expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', '{"PageSize":["The field PageSize must be between 0 and 1000."]}'); expect(result).to.have.property(
'message',
'{"PageSize":["The field PageSize must be between 0 and 1000."]}'
);
expect(result).to.have.property('data'); expect(result).to.have.property('data');
expect(result.data).to.be.null; expect(result.data).to.be.null;
}); });
@@ -344,19 +347,26 @@ describe('OnspringClient', function () {
describe('getAppById', function () { describe('getAppById', function () {
it('should be defined', function () { it('should be defined', function () {
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.not.be.undefined; expect(new OnspringClient(baseUrl, apiKey).getAppById).to.not.be
.undefined;
}); });
it('should be a function', function () { it('should be a function', function () {
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.be.a('function'); expect(new OnspringClient(baseUrl, apiKey).getAppById).to.be.a(
'function'
);
}); });
it('should have 1 parameter', function () { it('should have 1 parameter', function () {
expect(new OnspringClient(baseUrl, apiKey).getAppById).to.have.lengthOf(1); expect(new OnspringClient(baseUrl, apiKey).getAppById).to.have.lengthOf(
1
);
}); });
it('should return a promise', function () { it('should return a promise', function () {
expect(new OnspringClient(baseUrl, apiKey).getAppById(1)).to.be.a('promise'); expect(new OnspringClient(baseUrl, apiKey).getAppById(1)).to.be.a(
'promise'
);
}); });
it('should return a promise that resolves to an api response of an app when request is successful', async function () { it('should return a promise that resolves to an api response of an app when request is successful', async function () {
+27 -6
View File
@@ -15,14 +15,35 @@ describe('PagedResponse', function () {
}); });
it('should construct a new instance of PagedResponse', function () { it('should construct a new instance of PagedResponse', function () {
const pagedResponse = new PagedResponse<number>([1, 2, 3, 4], 1, 10, 100, 100); const pagedResponse = new PagedResponse<number>(
[1, 2, 3, 4],
1,
10,
100,
100
);
expect(pagedResponse).to.not.be.undefined; expect(pagedResponse).to.not.be.undefined;
expect(pagedResponse).to.be.instanceOf(PagedResponse); expect(pagedResponse).to.be.instanceOf(PagedResponse);
expect(pagedResponse).to.have.property('items').to.be.an('array').to.have.lengthOf(4); expect(pagedResponse)
expect(pagedResponse).to.have.property('pageNumber').to.be.a('number').to.equal(1); .to.have.property('items')
expect(pagedResponse).to.have.property('pageSize').to.be.a('number').to.equal(10); .to.be.an('array')
expect(pagedResponse).to.have.property('totalPages').to.be.a('number').to.equal(100); .to.have.lengthOf(4);
expect(pagedResponse).to.have.property('totalRecords').to.be.a('number').to.equal(100); 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('totalPages')
.to.be.a('number')
.to.equal(100);
expect(pagedResponse)
.to.have.property('totalRecords')
.to.be.a('number')
.to.equal(100);
}); });
}); });
+9 -3
View File
@@ -15,15 +15,21 @@ describe('PagingRequest', function () {
}); });
it('should throw an error when the pageNumber is less than 1', function () { it('should throw an error when the pageNumber is less than 1', function () {
expect(() => new PagingRequest(0, 1)).to.throw('pageNumber must be greater than 0.'); expect(() => new PagingRequest(0, 1)).to.throw(
'pageNumber must be greater than 0.'
);
}); });
it('should throw an error when the pageSize is less than 1', function () { it('should throw an error when the pageSize is less than 1', function () {
expect(() => new PagingRequest(1, 0)).to.throw('pageSize must be greater than 0 and less than 1001.'); expect(() => new PagingRequest(1, 0)).to.throw(
'pageSize must be greater than 0 and less than 1001.'
);
}); });
it('should throw an error when the pageSize is greater than 1000', function () { it('should throw an error when the pageSize is greater than 1000', function () {
expect(() => new PagingRequest(1, 1001)).to.throw('pageSize must be greater than 0 and less than 1001.'); expect(() => new PagingRequest(1, 1001)).to.throw(
'pageSize must be greater than 0 and less than 1001.'
);
}); });
it('should create a new instance of the PagingRequest class when the pageNumber is greater than 0 and the pageSize is between 1 and 1000', function () { it('should create a new instance of the PagingRequest class when the pageNumber is greater than 0 and the pageSize is between 1 and 1000', function () {