From 9ab72bd37caf195b9b76aa7d3e49d74d236e0bb8 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Thu, 26 Jan 2023 22:40:13 -0600 Subject: [PATCH] fix: add missing tests --- package.json | 2 +- src/OnspringClient.ts | 4 +-- src/models/App.ts | 15 +++++++++- tests/ApiResponse.spec.ts | 56 ++++++++++++++++++++++++++++++++++++ tests/App.spec.ts | 20 +++++++++++++ tests/OnspringClient.spec.ts | 32 +++++++++++---------- tests/PagingRequest.spec.ts | 32 +++++++++++++++++++++ 7 files changed, 142 insertions(+), 19 deletions(-) create mode 100644 tests/ApiResponse.spec.ts create mode 100644 tests/App.spec.ts create mode 100644 tests/PagingRequest.spec.ts diff --git a/package.json b/package.json index 4b092ab..c03d5c1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "dist/index.d.ts", "scripts": { "build": "npm tests && tsc", - "tests": "mocha -r ts-node/register ./tests/**/*.spec.ts", + "tests": "mocha -R progress -r ts-node/register ./tests/**/*.spec.ts", "test": "mocha -r ts-node/register", "test-coverage": "nyc npm run tests" }, diff --git a/src/OnspringClient.ts b/src/OnspringClient.ts index cfbc786..e425cb0 100644 --- a/src/OnspringClient.ts +++ b/src/OnspringClient.ts @@ -9,7 +9,7 @@ export class OnspringClient { /** * @readonly {AxiosInstance} client - The axios instance that will be used to make requests to the Onspring API. */ - protected readonly client: AxiosInstance; + private readonly _client: AxiosInstance; /** * @constructor - Creates a new instance of the OnspringClient class. @@ -31,7 +31,7 @@ export class OnspringClient { throw new Error('apiKey cannot be null/empty/whitespace.'); } - this.client = axios.create({ + this._client = axios.create({ baseURL: baseUrl, headers: { 'x-apikey': apiKey, diff --git a/src/models/App.ts b/src/models/App.ts index 384c48c..90e05cb 100644 --- a/src/models/App.ts +++ b/src/models/App.ts @@ -1,7 +1,7 @@ /** * @class App - Model for the App object */ -class App { +export class App { /** * @property {string} href - The URL to the app */ @@ -16,4 +16,17 @@ class App { * @property {string} name - The name of the app */ name: string; + + /** + * @constructor - Creates a new instance of the App class. + * @param {string} href - The URL to the app + * @param {number} id - The id of the app + * @param {string} name - The name of the app + * @returns {App} - A new instance of the App class. + */ + constructor(href: string, id: number, name: string) { + this.href = href; + this.id = id; + this.name = name; + } } diff --git a/tests/ApiResponse.spec.ts b/tests/ApiResponse.spec.ts new file mode 100644 index 0000000..ca44cae --- /dev/null +++ b/tests/ApiResponse.spec.ts @@ -0,0 +1,56 @@ +import { ApiResponse } from '../src/models/ApiResponse'; +import { expect } from 'chai'; + +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 () { + 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'); + }); + + it('should have a property named message', function () { + 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'); + }); + + 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); + }); + + 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; + }); + + 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; + }); + + 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'); + }); + + 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'); + }); +}); \ No newline at end of file diff --git a/tests/App.spec.ts b/tests/App.spec.ts new file mode 100644 index 0000000..a333b92 --- /dev/null +++ b/tests/App.spec.ts @@ -0,0 +1,20 @@ +import { App } from '../src/models/App'; +import { expect } from 'chai'; + +describe('App', function () { + it('should be defined', function () { + expect(App).to.not.be.undefined; + }); + + it('should have a constructor', function () { + expect(App).to.have.property('constructor'); + }); + + it('should have 3 parameters', function () { + expect(App).to.have.lengthOf(3); + }); + + it('should create a new instance of the App class', function () { + expect(() => new App('href', 1, 'name')).to.not.throw(); + }); +}); \ No newline at end of file diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index 39af4d5..9299cd8 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -1,7 +1,10 @@ import { OnspringClient } from '../src/OnspringClient'; import { expect } from 'chai'; -describe('OnspringClient Tests', function () { +describe('OnspringClient', function () { + const baseUrl = 'https://api.onspring.com'; + const apiKey = 'apiKey'; + it('should be defined', function () { expect(OnspringClient).to.not.be.undefined; }); @@ -15,73 +18,72 @@ describe('OnspringClient Tests', function () { }); it('should throw an error when the baseUrl is null', function () { - expect(() => new OnspringClient(null, 'apiKey')).to.throw( + expect(() => new OnspringClient(null, apiKey)).to.throw( 'baseUrl must be an absolute and well-formed URI.' ); }); it('should throw an error when the baseUrl is undefined', function () { - expect(() => new OnspringClient(undefined, 'apiKey')).to.throw( + expect(() => new OnspringClient(undefined, apiKey)).to.throw( 'baseUrl must be an absolute and well-formed URI.' ); }); it('should throw an error when the baseUrl is an empty string', function () { - expect(() => new OnspringClient('', 'apiKey')).to.throw( + expect(() => new OnspringClient('', apiKey)).to.throw( 'baseUrl must be an absolute and well-formed URI.' ); }); it('should throw an error when the baseUrl is a string with only spaces', function () { - expect(() => new OnspringClient(' ', 'apiKey')).to.throw( + expect(() => new OnspringClient(' ', apiKey)).to.throw( 'baseUrl must be an absolute and well-formed URI.' ); }); it('should throw an error when the baseUrl is not a valid url', function () { - expect(() => new OnspringClient('api.onspring.com', 'apiKey')).to.throw( + expect(() => new OnspringClient('api.onspring.com', apiKey)).to.throw( 'baseUrl must be an absolute and well-formed URI.' ); }); it('should throw an error when the apiKey is null', function () { - expect(() => new OnspringClient('https://api.onspring.com', null)).to.throw( + expect(() => new OnspringClient(baseUrl, null)).to.throw( 'apiKey cannot be null/empty/whitespace.' ); }); it('should throw an error when the apiKey is undefined', function () { - expect(() => new OnspringClient('https://api.onspring.com', undefined)).to.throw( + expect(() => new OnspringClient(baseUrl, undefined)).to.throw( 'apiKey cannot be null/empty/whitespace.' ); }); it('should throw an error when the apiKey is an empty string', function () { - expect(() => new OnspringClient('https://api.onspring.com', '')).to.throw( + expect(() => new OnspringClient(baseUrl, '')).to.throw( 'apiKey cannot be null/empty/whitespace.' ); }); it('should throw an error when the apiKey is a string with only spaces', function () { - expect(() => new OnspringClient('https://api.onspring.com', ' ')).to.throw( + expect(() => new OnspringClient(baseUrl, ' ')).to.throw( 'apiKey cannot be null/empty/whitespace.' ); }); it('should not throw an error when the baseUrl is a valid url', function () { - expect(() => new OnspringClient('https://api.onspring.com', 'apiKey')).to.not.throw(); + expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw(); }); it('should not throw an error when the baseUrl is a valid url', function () { - expect(() => new OnspringClient('http://api.onspring.com', 'apiKey')).to.not.throw(); + expect(() => new OnspringClient('http://api.onspring.com', apiKey)).to.not.throw(); }); it('should not throw an error when the apiKey is a valid string', function () { - expect(() => new OnspringClient('https://api.onspring.com', 'apiKey')).to.not.throw(); + expect(() => new OnspringClient(baseUrl, apiKey)).to.not.throw(); }); it('should have a client property', function () { - const client = new OnspringClient('https://api.onspring.com', 'apiKey'); - expect(client).to.have.property('client'); + expect(new OnspringClient(baseUrl, apiKey)).to.have.property('_client'); }); }); diff --git a/tests/PagingRequest.spec.ts b/tests/PagingRequest.spec.ts new file mode 100644 index 0000000..23fe659 --- /dev/null +++ b/tests/PagingRequest.spec.ts @@ -0,0 +1,32 @@ +import { PagingRequest } from '../src/models/PagingRequest'; +import { expect } from 'chai'; + +describe('PagingRequest', function () { + it('should be defined', function () { + expect(PagingRequest).to.not.be.undefined; + }); + + it('should have a constructor', function () { + expect(PagingRequest).to.have.property('constructor'); + }); + + it('should have 2 parameters', function () { + expect(PagingRequest).to.have.lengthOf(2); + }); + + 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.'); + }); + + 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.'); + }); + + 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.'); + }); + + 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 () { + expect(() => new PagingRequest(1, 1)).to.not.throw(); + }); +}); \ No newline at end of file