fix: properly implement saveFile method and write test

This commit is contained in:
StevanFreeborn
2023-02-03 22:32:57 -06:00
parent 3dee5b690c
commit ed68b5f9d9
9 changed files with 669 additions and 58 deletions
+4 -4
View File
@@ -1,10 +1,10 @@
{ {
"extends": "@istanbuljs/nyc-config-typescript", "extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true, "check-coverage": true,
"branches": 80, "branches": 100,
"lines": 80, "lines": 100,
"functions": 80, "functions": 100,
"statements": 80, "statements": 100,
"all": true, "all": true,
"include": ["src/**/*.ts"], "include": ["src/**/*.ts"],
"exclude": ["src/index.ts", "**/*.spec.ts"], "exclude": ["src/index.ts", "**/*.spec.ts"],
+13 -13
View File
@@ -44,10 +44,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsGetPagedAppsResponseType - Converts the ApiResponse to an ApiResponse<GetPagedAppsResponse>. * @method asGetPagedAppsResponseType - Converts the ApiResponse to an ApiResponse<GetPagedAppsResponse>.
* @returns {ApiResponse<GetPagedAppsResponse>} - An ApiResponse<GetPagedAppsResponse>. * @returns {ApiResponse<GetPagedAppsResponse>} - An ApiResponse<GetPagedAppsResponse>.
*/ */
public AsGetPagedAppsResponseType(): ApiResponse<GetPagedAppsResponse> { public asGetPagedAppsResponseType(): ApiResponse<GetPagedAppsResponse> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const apps = apiResponse.data.items.map((item: any) => { const apps = apiResponse.data.items.map((item: any) => {
@@ -70,10 +70,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsAppType - Converts the ApiResponse to an ApiResponse<App>. * @method asAppType - Converts the ApiResponse to an ApiResponse<App>.
* @returns {ApiResponse<App>} - An ApiResponse<App>. * @returns {ApiResponse<App>} - An ApiResponse<App>.
*/ */
public AsAppType(): ApiResponse<App> { public asAppType(): ApiResponse<App> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const app = new App( const app = new App(
@@ -90,10 +90,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsAppCollectionType - Converts the ApiResponse to an ApiResponse<CollectionResponse<App>>. * @method asAppCollectionType - Converts the ApiResponse to an ApiResponse<CollectionResponse<App>>.
* @returns {ApiResponse<CollectionResponse<App>>} - An ApiResponse<CollectionResponse<App>>. * @returns {ApiResponse<CollectionResponse<App>>} - An ApiResponse<CollectionResponse<App>>.
*/ */
public AsAppCollectionType(): ApiResponse<CollectionResponse<App>> { public asAppCollectionType(): ApiResponse<CollectionResponse<App>> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const apps = apiResponse.data.items.map((item: any) => { const apps = apiResponse.data.items.map((item: any) => {
@@ -113,10 +113,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsFieldType - Converts the ApiResponse to an ApiResponse<Field>. * @method asFieldType - Converts the ApiResponse to an ApiResponse<Field>.
* @returns {ApiResponse<Field>} - An ApiResponse<Field>. * @returns {ApiResponse<Field>} - An ApiResponse<Field>.
*/ */
public AsFieldType(): ApiResponse<Field> { public asFieldType(): ApiResponse<Field> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const field = new Field( const field = new Field(
@@ -137,10 +137,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsFieldCollectionType - Converts the ApiResponse to an ApiResponse<CollectionResponse<Field>>. * @method asFieldCollectionType - Converts the ApiResponse to an ApiResponse<CollectionResponse<Field>>.
* @returns {ApiResponse<CollectionResponse<Field>>} - An ApiResponse<CollectionResponse<Field>>. * @returns {ApiResponse<CollectionResponse<Field>>} - An ApiResponse<CollectionResponse<Field>>.
*/ */
public AsFieldCollectionType(): ApiResponse<CollectionResponse<Field>> { public asFieldCollectionType(): ApiResponse<CollectionResponse<Field>> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const fields = apiResponse.data.items.map((item: any) => { const fields = apiResponse.data.items.map((item: any) => {
@@ -168,10 +168,10 @@ export class ApiResponse<T> {
} }
/** /**
* @method AsGetPagedFieldsResponseType - Converts the ApiResponse to an ApiResponse<GetPagedFieldsResponse>. * @method asGetPagedFieldsResponseType - Converts the ApiResponse to an ApiResponse<GetPagedFieldsResponse>.
* @returns {ApiResponse<GetPagedFieldsResponse>} - An ApiResponse<GetPagedFieldsResponse>. * @returns {ApiResponse<GetPagedFieldsResponse>} - An ApiResponse<GetPagedFieldsResponse>.
*/ */
public AsGetPagedFieldsResponseType(): ApiResponse<GetPagedFieldsResponse> { public asGetPagedFieldsResponseType(): ApiResponse<GetPagedFieldsResponse> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const fields = apiResponse.data.items.map((item: any) => { const fields = apiResponse.data.items.map((item: any) => {
@@ -201,7 +201,7 @@ export class ApiResponse<T> {
); );
} }
public AsCreatedWithIdResponseType(): ApiResponse<CreatedWithIdResponse> { public asCreatedWithIdResponseType(): ApiResponse<CreatedWithIdResponse> {
const apiResponse = this as ApiResponse<any>; const apiResponse = this as ApiResponse<any>;
const createdWithIdResponse = new CreatedWithIdResponse( const createdWithIdResponse = new CreatedWithIdResponse(
+9 -9
View File
@@ -75,7 +75,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsGetPagedAppsResponseType(); return apiResponse.asGetPagedAppsResponseType();
} }
/** /**
@@ -92,7 +92,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsAppType(); return apiResponse.asAppType();
} }
/** /**
@@ -110,7 +110,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsAppCollectionType(); return apiResponse.asAppCollectionType();
} }
/** /**
@@ -126,7 +126,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsFieldType(); return apiResponse.asFieldType();
} }
/** /**
@@ -145,7 +145,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsFieldCollectionType(); return apiResponse.asFieldCollectionType();
} }
/** /**
@@ -168,7 +168,7 @@ export class OnspringClient {
return apiResponse; return apiResponse;
} }
return apiResponse.AsGetPagedFieldsResponseType(); return apiResponse.asGetPagedFieldsResponseType();
} }
/** /**
@@ -180,16 +180,16 @@ export class OnspringClient {
request: SaveFileRequest request: SaveFileRequest
): Promise<ApiResponse<CreatedWithIdResponse>> { ): Promise<ApiResponse<CreatedWithIdResponse>> {
const endpoint = EndpointFactory.getSaveFileEndpoint(); const endpoint = EndpointFactory.getSaveFileEndpoint();
const formData = request.AsFormData(); const formData = request.asFormData();
const apiResponse = await this.post<any>(endpoint, formData, { const apiResponse = await this.post<any>(endpoint, formData, {
headers: formData.getHeaders(), headers: { 'Content-Type': 'multipart/form-data' },
}); });
if (apiResponse.isSuccessful === false) { if (apiResponse.isSuccessful === false) {
return apiResponse; return apiResponse;
} }
return apiResponse.AsCreatedWithIdResponseType(); return apiResponse.asCreatedWithIdResponseType();
} }
/** /**
+6 -5
View File
@@ -1,3 +1,4 @@
import { type Readable } from 'stream';
import FormData = require('form-data'); import FormData = require('form-data');
/** /**
@@ -35,9 +36,9 @@ export class SaveFileRequest {
public contentType: string; public contentType: string;
/** /**
* @property {ReadableStream} fileStream - The file stream. * @property {Readable} fileStream - The file stream.
*/ */
public fileStream: ReadableStream; public fileStream: Readable;
/** /**
* @constructor - Creates a new SaveFileRequest. * @constructor - Creates a new SaveFileRequest.
@@ -47,7 +48,7 @@ export class SaveFileRequest {
* @param {Date} modifiedDate - The modified date for the file. * @param {Date} modifiedDate - The modified date for the file.
* @param {string} fileName - The name of the file. * @param {string} fileName - The name of the file.
* @param {string} contentType - The content type of the file. * @param {string} contentType - The content type of the file.
* @param {ReadableStream} fileStream - The file stream. * @param {Readable} fileStream - The file stream.
* @returns {SaveFileRequest} - A new SaveFileRequest. * @returns {SaveFileRequest} - A new SaveFileRequest.
*/ */
constructor( constructor(
@@ -57,7 +58,7 @@ export class SaveFileRequest {
modifiedDate: Date, modifiedDate: Date,
fileName: string, fileName: string,
contentType: string, contentType: string,
fileStream: ReadableStream fileStream: Readable
) { ) {
this.recordId = recordId; this.recordId = recordId;
this.fieldId = fieldId; this.fieldId = fieldId;
@@ -72,7 +73,7 @@ export class SaveFileRequest {
* @method AsFormData - Converts the SaveFileRequest to a FormData object. * @method AsFormData - Converts the SaveFileRequest to a FormData object.
* @returns {FormData} - The SaveFileRequest as a FormData object. * @returns {FormData} - The SaveFileRequest as a FormData object.
*/ */
public AsFormData(): FormData { public asFormData(): FormData {
const formData = new FormData(); const formData = new FormData();
formData.append('recordId', this.recordId.toString()); formData.append('recordId', this.recordId.toString());
formData.append('fieldId', this.fieldId.toString()); formData.append('fieldId', this.fieldId.toString());
+60 -26
View File
@@ -7,6 +7,7 @@ import { Field } from '../src/models/Field';
import { FieldStatus } from '../src/enums/FieldStatus'; import { FieldStatus } from '../src/enums/FieldStatus';
import { FieldType } from '../src/enums/FieldType'; import { FieldType } from '../src/enums/FieldType';
import { GetPagedFieldsResponse } from '../src/models/GetPagedFieldsResponse'; import { GetPagedFieldsResponse } from '../src/models/GetPagedFieldsResponse';
import { CreatedWithIdResponse } from '../src/models/CreatedWithIdResponse';
describe('ApiResponse', function () { describe('ApiResponse', function () {
it('should be defined', function () { it('should be defined', function () {
@@ -77,14 +78,14 @@ describe('ApiResponse', function () {
); );
}); });
describe('AsGetPagedAppsResponseType', function () { describe('asGetPagedAppsResponseType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsGetPagedAppsResponseType).to.not.be expect(ApiResponse.prototype.asGetPagedAppsResponseType).to.not.be
.undefined; .undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect(ApiResponse.prototype.AsGetPagedAppsResponseType).to.have.lengthOf( expect(ApiResponse.prototype.asGetPagedAppsResponseType).to.have.lengthOf(
0 0
); );
}); });
@@ -110,7 +111,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType(); const appsPagedResponse = apiResponse.asGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse); expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse); expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
@@ -137,7 +138,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appsPagedResponse = apiResponse.AsGetPagedAppsResponseType(); const appsPagedResponse = apiResponse.asGetPagedAppsResponseType();
expect(appsPagedResponse).to.be.instanceOf(ApiResponse); expect(appsPagedResponse).to.be.instanceOf(ApiResponse);
expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse); expect(appsPagedResponse.data).to.be.instanceOf(GetPagedAppsResponse);
@@ -153,13 +154,13 @@ describe('ApiResponse', function () {
}); });
}); });
describe('AsAppType', function () { describe('asAppType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsAppType).to.not.be.undefined; expect(ApiResponse.prototype.asAppType).to.not.be.undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect(ApiResponse.prototype.AsAppType).to.have.lengthOf(0); expect(ApiResponse.prototype.asAppType).to.have.lengthOf(0);
}); });
it('should return an ApiResponse<App> when data contain an app', function () { it('should return an ApiResponse<App> when data contain an app', function () {
@@ -170,7 +171,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appResponse = apiResponse.AsAppType(); const appResponse = apiResponse.asAppType();
expect(appResponse).to.be.instanceOf(ApiResponse); expect(appResponse).to.be.instanceOf(ApiResponse);
expect(appResponse.data).to.be.instanceOf(App); expect(appResponse.data).to.be.instanceOf(App);
@@ -185,13 +186,13 @@ describe('ApiResponse', function () {
}); });
}); });
describe('AsAppCollectionType', function () { describe('asAppCollectionType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsAppCollectionType).to.not.be.undefined; expect(ApiResponse.prototype.asAppCollectionType).to.not.be.undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect(ApiResponse.prototype.AsAppCollectionType).to.have.lengthOf(0); expect(ApiResponse.prototype.asAppCollectionType).to.have.lengthOf(0);
}); });
it('should return an ApiResponse<CollectionResponse<App[]>> when data contains app items', function () { it('should return an ApiResponse<CollectionResponse<App[]>> when data contains app items', function () {
@@ -212,7 +213,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const appCollectionResponse = apiResponse.AsAppCollectionType(); const appCollectionResponse = apiResponse.asAppCollectionType();
expect(appCollectionResponse).to.be.instanceOf( expect(appCollectionResponse).to.be.instanceOf(
ApiResponse<CollectionResponse<App>> ApiResponse<CollectionResponse<App>>
@@ -237,13 +238,13 @@ describe('ApiResponse', function () {
}); });
}); });
describe('AsFieldType', function () { describe('asFieldType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsFieldType).to.not.be.undefined; expect(ApiResponse.prototype.asFieldType).to.not.be.undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect(ApiResponse.prototype.AsFieldType).to.have.lengthOf(0); expect(ApiResponse.prototype.asFieldType).to.have.lengthOf(0);
}); });
it('should return an ApiResponse<Field> when data contain a field', function () { it('should return an ApiResponse<Field> when data contain a field', function () {
@@ -258,7 +259,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.AsFieldType(); const fieldResponse = apiResponse.asFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse); expect(fieldResponse).to.be.instanceOf(ApiResponse);
expect(fieldResponse.data).to.be.instanceOf(Field); expect(fieldResponse.data).to.be.instanceOf(Field);
@@ -275,13 +276,13 @@ describe('ApiResponse', function () {
}); });
}); });
describe('AsFieldCollectionType', function () { describe('asFieldCollectionType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsFieldCollectionType).to.not.be.undefined; expect(ApiResponse.prototype.asFieldCollectionType).to.not.be.undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect(ApiResponse.prototype.AsFieldCollectionType).to.have.lengthOf(0); expect(ApiResponse.prototype.asFieldCollectionType).to.have.lengthOf(0);
}); });
it('should return an ApiResponse<CollectionResponse<Field>> when data contains field items', function () { it('should return an ApiResponse<CollectionResponse<Field>> when data contains field items', function () {
@@ -310,7 +311,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldCollectionResponse = apiResponse.AsFieldCollectionType(); const fieldCollectionResponse = apiResponse.asFieldCollectionType();
expect(fieldCollectionResponse).to.be.instanceOf( expect(fieldCollectionResponse).to.be.instanceOf(
ApiResponse<CollectionResponse<Field>> ApiResponse<CollectionResponse<Field>>
@@ -336,15 +337,15 @@ describe('ApiResponse', function () {
}); });
}); });
describe('AsGetPagedFieldsResponseType', function () { describe('asGetPagedFieldsResponseType', function () {
it('should be defined', function () { it('should be defined', function () {
expect(ApiResponse.prototype.AsGetPagedFieldsResponseType).to.not.be expect(ApiResponse.prototype.asGetPagedFieldsResponseType).to.not.be
.undefined; .undefined;
}); });
it('should have no parameters', function () { it('should have no parameters', function () {
expect( expect(
ApiResponse.prototype.AsGetPagedFieldsResponseType ApiResponse.prototype.asGetPagedFieldsResponseType
).to.have.lengthOf(0); ).to.have.lengthOf(0);
}); });
@@ -377,7 +378,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const getPagedFieldsResponse = apiResponse.AsGetPagedFieldsResponseType(); const getPagedFieldsResponse = apiResponse.asGetPagedFieldsResponseType();
expect(getPagedFieldsResponse).to.be.instanceOf( expect(getPagedFieldsResponse).to.be.instanceOf(
ApiResponse<GetPagedFieldsResponse> ApiResponse<GetPagedFieldsResponse>
@@ -417,7 +418,7 @@ describe('ApiResponse', function () {
}; };
const apiResponse = new ApiResponse(200, 'OK', mockResponseData); const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const getPagedFieldsResponse = apiResponse.AsGetPagedFieldsResponseType(); const getPagedFieldsResponse = apiResponse.asGetPagedFieldsResponseType();
expect(getPagedFieldsResponse).to.be.instanceOf( expect(getPagedFieldsResponse).to.be.instanceOf(
ApiResponse<GetPagedFieldsResponse> ApiResponse<GetPagedFieldsResponse>
@@ -437,4 +438,37 @@ describe('ApiResponse', function () {
} }
}); });
}); });
describe('asCreatedWithIdResponseType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.asCreatedWithIdResponseType).to.not.be
.undefined;
});
it('should have no parameters', function () {
expect(
ApiResponse.prototype.asCreatedWithIdResponseType
).to.have.lengthOf(0);
});
it('should return an ApiResponse<CreatedWithIdResponse> when data contains an id', function () {
const mockResponseData = {
id: 1,
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const createdWithIdResponse = apiResponse.asCreatedWithIdResponseType();
expect(createdWithIdResponse).to.be.instanceOf(
ApiResponse<CreatedWithIdResponse>
);
expect(createdWithIdResponse.data).to.be.instanceOf(
CreatedWithIdResponse
);
expect(createdWithIdResponse.data).to.not.be.null;
if (createdWithIdResponse.data != null) {
expect(createdWithIdResponse.data.id).to.equal(1);
}
});
});
}); });
+28
View File
@@ -0,0 +1,28 @@
import { expect } from 'chai';
import { CreatedWithIdResponse } from '../src/models/CreatedWithIdResponse';
describe('CreatedWithIdResponse', function () {
it('should be defined', function () {
expect(CreatedWithIdResponse).to.not.be.undefined;
});
it('should have a constructor', function () {
expect(CreatedWithIdResponse).to.have.property('constructor');
});
it('should have 1 parameters', function () {
expect(CreatedWithIdResponse).to.have.lengthOf(1);
});
it('should create a new instance of the CreatedWithIdResponse class', function () {
expect(() => new CreatedWithIdResponse(1)).to.not.throw();
});
it('should have a property named id', function () {
expect(new CreatedWithIdResponse(1)).to.have.property('id');
});
it('should set the id property to the value passed to the constructor', function () {
expect(new CreatedWithIdResponse(1).id).to.equal(1);
});
});
+290
View File
@@ -13,6 +13,9 @@ import { Field } from '../src/models/Field';
import { FieldStatus } from '../src/enums/FieldStatus'; import { FieldStatus } from '../src/enums/FieldStatus';
import { FieldType } from '../src/enums/FieldType'; import { FieldType } from '../src/enums/FieldType';
import { GetPagedFieldsResponse } from '../src/models/GetPagedFieldsResponse'; import { GetPagedFieldsResponse } from '../src/models/GetPagedFieldsResponse';
import { SaveFileRequest } from '../src/models/SaveFileRequest';
import { Readable } from 'stream';
import { CreatedWithIdResponse } from '../src/models/CreatedWithIdResponse';
describe('OnspringClient', function () { describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev'; const baseUrl = 'https://api.onspring.dev';
@@ -1205,4 +1208,291 @@ describe('OnspringClient', function () {
expect(result.data).to.be.null; expect(result.data).to.be.null;
}); });
}); });
describe('saveFile', function () {
it('should be a function', function () {
expect(new OnspringClient(baseUrl, apiKey).saveFile).to.be.a('function');
});
it('should return a promise', function () {
const client = new OnspringClient(baseUrl, apiKey);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
expect(client.saveFile(saveFileRequest)).to.be.instanceOf(Promise);
});
it('should return a promise that resolves to an api response of with a created id when the 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, 'post').returns(
Promise.resolve({
status: 201,
statusText: 'Created',
data: {
id: 1,
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 201);
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(CreatedWithIdResponse);
expect(result.data).to.have.property('id', 1);
});
it('should return a promise that resolves to an api response when request receives a 400 response', 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, 'post').returns(
Promise.resolve({
status: 400,
statusText: 'Bad Request',
data: {
File: ['The File field is required.'],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 400);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property(
'message',
'{"File":["The File field is required."]}'
);
expect(result).to.have.property('data');
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 401 response', 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, 'post').returns(
Promise.resolve({
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 403 response', 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, 'post').returns(
Promise.resolve({
status: 403,
statusText: 'Forbidden',
data: {
message:
'The user does not have permission to access this resource.',
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.equal(
'The user does not have permission to access this resource.'
);
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 404 response', 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, 'post').returns(
Promise.resolve({
status: 404,
statusText: 'Not Found',
data: {
message: 'The requested resource was not found.',
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.equal('The requested resource was not found.');
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 500 response', 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, 'post').returns(
Promise.resolve({
status: 500,
statusText: 'Internal Server Error',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const saveFileRequest = new SaveFileRequest(
1,
1,
'notes',
new Date(),
'file name',
'content type',
new Readable()
);
const result = await client.saveFile(saveFileRequest);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 500);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
});
}); });
+257
View File
@@ -0,0 +1,257 @@
import { expect } from 'chai';
import { SaveFileRequest } from '../src/models/SaveFileRequest';
import FormData from 'form-data';
import { Readable } from 'stream';
describe('SaveFileRequest', function () {
it('should be defined', function () {
expect(SaveFileRequest).to.not.be.undefined;
});
it('should have a constructor', function () {
expect(SaveFileRequest).to.have.property('constructor');
});
it('should have 7 parameters', function () {
expect(SaveFileRequest).to.have.lengthOf(7);
});
it('should create a new instance of the SaveFileRequest class', function () {
expect(
() =>
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.not.throw();
});
it('should have a property named recordId', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('recordId');
});
it('should set the recordId property to the value passed to the constructor', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).recordId
).to.equal(1);
});
it('should have a property named fieldId', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('fieldId');
});
it('should set the fieldId property to the value passed to the constructor', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).fieldId
).to.equal(1);
});
it('should have a property named notes', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('notes');
});
it('should set the notes property to the value passed to the constructor', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).notes
).to.equal('note');
});
it('should have a property named modifiedDate', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('modifiedDate');
});
it('should set the modifiedDate property to the value passed to the constructor', function () {
const modifiedDate = new Date();
expect(
new SaveFileRequest(
1,
1,
'note',
modifiedDate,
'file',
'contentType',
new Readable()
).modifiedDate
).to.deep.equal(modifiedDate);
});
it('should have a property named fileName', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('fileName');
});
it('should set the fileName property to the value passed to the constructor', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).fileName
).to.equal('file');
});
it('should have a property named contentType', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('contentType');
});
it('should set the contentType property to the value passed to the constructor', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).contentType
).to.equal('contentType');
});
it('should have a property named fileStream', function () {
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
)
).to.have.property('fileStream');
});
it('should set the fileStream property to the value passed to the constructor', function () {
const stream = new Readable();
expect(
new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
stream
).fileStream
).to.deep.equal(stream);
});
describe('asFormData', function () {
it('should be defined', function () {
expect(SaveFileRequest.prototype.asFormData).to.not.be.undefined;
});
it('should have 0 parameters', function () {
expect(SaveFileRequest.prototype.asFormData).to.have.lengthOf(0);
});
it('should return a FormData object', function () {
const formData = new SaveFileRequest(
1,
1,
'note',
new Date(),
'file',
'contentType',
new Readable()
).asFormData();
expect(formData).to.be.instanceof(FormData);
});
});
});
+2 -1
View File
@@ -4,7 +4,8 @@
"module": "commonjs", "module": "commonjs",
"target": "es2015", "target": "es2015",
"declaration": true, "declaration": true,
"outDir": "./dist" "outDir": "./dist",
"esModuleInterop": true
}, },
"include": ["src/**/*"] "include": ["src/**/*"]
} }