feat: finish writing tests for getRecordsByIds method

This commit is contained in:
StevanFreeborn
2023-02-12 15:04:08 -06:00
parent 827ad9c5a1
commit abf571f415
4 changed files with 306 additions and 1 deletions
+26
View File
@@ -392,6 +392,32 @@ export class ApiResponse<T> {
); );
} }
public asRecordCollectionType(): ApiResponse<CollectionResponse<Record>> {
const apiResponse = this as ApiResponse<any>;
const records = apiResponse.data.items.map((item: any) => {
const recordValues = item.fieldData.map((fieldData: any) => {
return new RecordValue(
fieldData.type,
fieldData.fieldId,
fieldData.value
);
});
return new Record(item.appId, item.recordId, recordValues);
});
const collectionResponse = new CollectionResponse<Record>(
apiResponse.data.count,
records
);
return new ApiResponse<CollectionResponse<Record>>(
apiResponse.statusCode,
apiResponse.message,
collectionResponse
);
}
/** /**
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type. * @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
* @param {any} fieldItem - The field item to convert. * @param {any} fieldItem - The field item to convert.
+13 -1
View File
@@ -357,10 +357,22 @@ export class OnspringClient {
return apiResponse.asRecordType(); return apiResponse.asRecordType();
} }
/**
* @method getRecordsByIds - Gets records by their ids.
* @param {GetRecordsRequest} request - The request that will be used to get the records.
* @returns {Promise<ApiResponse<CollectionResponse<Record>>>} - A promise that resolves to an ApiResponse of type CollectionResponse of type Record.
*/
public async getRecordsByIds( public async getRecordsByIds(
request: GetRecordsRequest request: GetRecordsRequest
): Promise<ApiResponse<CollectionResponse<Record>>> { ): Promise<ApiResponse<CollectionResponse<Record>>> {
throw new Error('Not implemented'); const endpoint = EndpointFactory.getRecordsByIdsEndpoint();
const apiResponse = await this.post<any>(endpoint, request);
if (apiResponse.isSuccessful === false) {
return apiResponse;
}
return apiResponse.asRecordCollectionType();
} }
/** /**
+69
View File
@@ -1254,4 +1254,73 @@ describe('ApiResponse', function () {
} }
}); });
}); });
describe('asRecordCollectionType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.asRecordCollectionType).to.not.be.undefined;
});
it('should be a function', function () {
expect(ApiResponse.prototype.asRecordCollectionType).to.be.a('function');
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.asRecordCollectionType).to.have.lengthOf(0);
});
it('should return an ApiResponse<CollectionResponse<Record>>', function () {
const mockResponseData = {
count: 1,
items: [
{
appId: 1,
recordId: 1,
fieldData: [
{
type: 'Text',
fieldId: 'field 1',
value: 'field value 1',
},
],
},
],
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const recordCollection = apiResponse.asRecordCollectionType();
expect(recordCollection).to.be.an.instanceof(
ApiResponse<CollectionResponse<Record>>
);
expect(recordCollection.data).to.be.an.instanceof(
CollectionResponse<Record>
);
expect(recordCollection.data).to.have.property('count', 1);
expect(recordCollection.data)
.to.have.property('items')
.to.be.an('array')
.that.has.lengthOf(1);
expect(recordCollection.data).to.not.be.null;
if (recordCollection.data != null) {
recordCollection.data.items.forEach((record) => {
expect(record).to.be.an.instanceof(Record);
expect(record).to.have.property('appId', 1);
expect(record).to.have.property('recordId', 1);
expect(record).to.have.property('fieldData').that.is.an('array');
expect(record).to.not.be.null;
if (record != null) {
record.fieldData.forEach((recordValue) => {
expect(recordValue).to.be.an.instanceof(RecordValue);
expect(recordValue).to.have.property('type');
expect(recordValue).to.have.property('fieldId');
expect(recordValue).to.have.property('value');
});
}
});
}
});
});
}); });
+198
View File
@@ -30,6 +30,7 @@ import { GetPagedRecordsResponse } from '../src/models/GetPagedRecordsResponse';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import * as sinon from 'sinon'; import * as sinon from 'sinon';
import { GetRecordsRequest } from '../src/models/GetRecordsRequest';
describe('OnspringClient', function () { describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev'; const baseUrl = 'https://api.onspring.dev';
@@ -3207,4 +3208,201 @@ describe('OnspringClient', function () {
expect(result).to.have.property('data', null); expect(result).to.have.property('data', null);
}); });
}); });
describe('getRecordsByIds', function () {
it('should be defined', function () {
expect(OnspringClient.prototype.getRecordsByIds).to.not.be.undefined;
});
it('should be a function', function () {
expect(OnspringClient.prototype.getRecordsByIds).to.be.a('function');
});
it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).getRecordsByIds(
new GetRecordsRequest(1, [1, 2, 3])
)
).to.be.instanceOf(Promise);
});
it('should return a promise that resolves to an api response 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, 'post').returns(
Promise.resolve({
status: 200,
statusText: 'OK',
data: {
count: 1,
items: [
{
appId: 1,
recordId: 1,
fieldData: [
{
type: 'text',
fieldId: 1,
value: 'Test',
},
{
type: 'text',
fieldId: 2,
value: 'Test',
},
],
},
],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getRecordsByIds(
new GetRecordsRequest(1, [1, 2, 3])
);
expect(result).to.be.instanceOf(ApiResponse);
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');
if (result.data != null) {
expect(result.data).to.have.property('items');
expect(result.data.items).to.be.an('array').that.has.lengthOf(1);
result.data.items.forEach((record) => {
expect(record).to.be.instanceOf(Record);
expect(record).to.have.property('appId', 1);
expect(record).to.have.property('recordId', 1);
expect(record).to.have.property('fieldData');
expect(record.fieldData).to.not.be.null;
if (record.fieldData != null) {
expect(record.fieldData).to.be.an('array').that.has.lengthOf(2);
record.fieldData.forEach((recordValue) => {
expect(recordValue).to.be.instanceOf(RecordValue);
expect(recordValue).to.have.property('type');
expect(recordValue).to.have.property('fieldId');
expect(recordValue).to.have.property('value');
});
}
});
}
});
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: { message: 'Bad Request' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getRecordsByIds(
new GetRecordsRequest(1, [1])
);
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', '{"message":"Bad Request"}');
expect(result).to.have.property('data', 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 result = await client.getRecordsByIds(
new GetRecordsRequest(1, [1])
);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', undefined);
expect(result).to.have.property('data', 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: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getRecordsByIds(
new GetRecordsRequest(1, [1])
);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Forbidden');
expect(result).to.have.property('data', null);
});
});
}); });