feat: implement getReportById method

This commit is contained in:
StevanFreeborn
2023-02-09 18:23:49 -06:00
parent 61cb4d2929
commit 3b3e210920
10 changed files with 393 additions and 4 deletions
+52
View File
@@ -21,6 +21,8 @@ import path from 'path';
import { ListItemResponse } from '../src/models/ListItemResponse';
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
import { Report } from '../src/models/Report';
import { ReportData } from '../src/models/ReportData';
import { Row } from '../src/models/Row';
describe('ApiResponse', function () {
it('should be defined', function () {
@@ -1014,4 +1016,54 @@ describe('ApiResponse', function () {
}
});
});
describe('asReportDataType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.asReportDataType).to.not.be.undefined;
});
it('should be a function', function () {
expect(ApiResponse.prototype.asReportDataType).to.be.a('function');
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.asReportDataType).to.have.lengthOf(0);
});
it('should return an ApiResponse<ReportData>', function () {
const mockResponseData = {
columns: ['field 1', 'field 2'],
rows: [
{
recordId: 1,
cells: ['field value 1', 'field value 2'],
},
{
recordId: 2,
cells: ['field value 1', 'field value 2'],
},
],
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const reportData = apiResponse.asReportDataType();
expect(reportData).to.be.an.instanceof(ApiResponse<ReportData>);
expect(reportData.data).to.be.an.instanceof(ReportData);
expect(reportData.data).to.have.property('columns').that.is.an('array');
expect(reportData.data).to.have.property('rows').that.is.an('array');
expect(reportData.data).to.not.be.null;
if (reportData.data != null) {
expect(reportData.data.columns).to.have.lengthOf(2);
reportData.data.columns.forEach((column) => {
expect(column).to.be.a('string');
});
expect(reportData.data.rows).to.have.lengthOf(2);
reportData.data.rows.forEach((row) => {
expect(row).to.be.an.instanceOf(Row);
});
}
});
});
});
+10 -2
View File
@@ -1,6 +1,8 @@
import { EndpointFactory } from '../src/models/EndpointFactory';
import { expect } from 'chai';
import { PagingRequest } from '../src/models/PagingRequest';
import { DataFormat } from '../src/enums/DataFormat';
import { ReportDataType } from '../src/enums/ReportDataType';
describe('EndpointFactory', function () {
describe('getPingEndpoint', function () {
@@ -155,8 +157,14 @@ describe('EndpointFactory', function () {
describe('getReportByIdEndpoint', function () {
it('should return the correct report by id endpoint', function () {
const result = EndpointFactory.getReportByIdEndpoint(1);
expect(result).to.equal('/Reports/id/1');
const result = EndpointFactory.getReportByIdEndpoint(
1,
DataFormat.Raw,
ReportDataType.ReportData
);
expect(result).to.equal(
'/Reports/id/1?apiDataFormat=Raw&dataType=ReportData'
);
});
});
+191
View File
@@ -24,6 +24,7 @@ import { ListItemRequest } from '../src/models/ListItemRequest';
import { ListItemResponse } from '../src/models/ListItemResponse';
import { Report } from '../src/models/Report';
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
import { ReportData } from '../src/models/ReportData';
describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev';
@@ -2631,4 +2632,194 @@ describe('OnspringClient', function () {
expect(result).to.have.property('data', null);
});
});
describe('getReportById', function () {
it('should be defined', function () {
expect(OnspringClient.prototype.getReportById).to.not.be.undefined;
});
it('should be a function', function () {
expect(OnspringClient.prototype.getReportById).to.be.a('function');
});
it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).getReportById(1)
).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, 'get').returns(
Promise.resolve({
status: 200,
statusText: 'OK',
data: {
columns: ['field 1', 'field 2'],
rows: [
{
recordId: 1,
cells: ['field value 1', 'field value 2'],
},
{
recordId: 2,
cells: ['field value 1', 'field value 2'],
},
],
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse<ReportData>);
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(ReportData);
expect(result.data).to.have.property('columns').that.is.an('array');
expect(result.data).to.have.property('rows').that.is.an('array');
});
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, 'get').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.getReportById(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, 'get').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.getReportById(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, 'get').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.getReportById(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);
});
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, 'get').returns(
Promise.resolve({
status: 404,
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getReportById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Not Found');
expect(result).to.have.property('data', null);
});
});
});
+50
View File
@@ -0,0 +1,50 @@
import { ReportData } from '../src/models/ReportData';
import { expect } from 'chai';
describe('ReportData', function () {
it('should be defined', function () {
expect(ReportData).to.not.be.undefined;
});
it('should have a constructor with 2 parameters', function () {
expect(ReportData).to.have.property('constructor');
expect(ReportData).to.have.lengthOf(2);
});
it('should have a construct that sets its properties correctly', function () {
const reportData = new ReportData(
['a', 'b'],
[
{
recordId: 1,
cells: [{}, {}],
},
{
recordId: 2,
cells: [{}, {}],
},
]
);
expect(reportData)
.to.have.property('columns')
.that.is.an('array')
.with.lengthOf(2);
expect(reportData)
.to.have.property('rows')
.that.is.an('array')
.with.lengthOf(2);
reportData.columns.forEach((column) => {
expect(column).to.be.a('string');
});
reportData.rows.forEach((row) => {
expect(row).to.have.property('recordId').that.is.a('number');
expect(row)
.to.have.property('cells')
.that.is.an('array')
.with.lengthOf(2);
});
});
});
+19
View File
@@ -0,0 +1,19 @@
import { Row } from '../src/models/Row';
import { expect } from 'chai';
describe('Row', function () {
it('should be defined', function () {
expect(Row).to.not.be.undefined;
});
it('should have a constructor with 2 parameters', function () {
expect(Row).to.have.property('constructor');
expect(Row).to.have.lengthOf(2);
});
it('should have a construct that sets its properties correctly', function () {
const row = new Row(1, [{}, {}]);
expect(row).to.have.property('recordId', 1);
expect(row).to.have.property('cells').that.is.an('array').with.lengthOf(2);
});
});