feat: finish implementing getReportsByAppId method
This commit is contained in:
+47
-16
@@ -19,6 +19,8 @@ import fs from 'fs';
|
||||
import { type AxiosResponse } from 'axios';
|
||||
import path from 'path';
|
||||
import { ListItemResponse } from '../src/models/ListItemResponse';
|
||||
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
|
||||
import { Report } from '../src/models/Report';
|
||||
|
||||
describe('ApiResponse', function () {
|
||||
it('should be defined', function () {
|
||||
@@ -962,25 +964,54 @@ describe('ApiResponse', function () {
|
||||
});
|
||||
|
||||
it('should return an ApiResponse<GetPagedReportsResponse>', function () {
|
||||
const mockResponseData = [
|
||||
{
|
||||
appId: 1,
|
||||
id: 1,
|
||||
name: 'string',
|
||||
description: 'description',
|
||||
},
|
||||
{
|
||||
appId: 2,
|
||||
id: 2,
|
||||
name: 'string',
|
||||
description: 'description'
|
||||
}
|
||||
]
|
||||
const mockResponseData = {
|
||||
pageSize: 2,
|
||||
pageNumber: 1,
|
||||
totalRecords: 2,
|
||||
totalPages: 1,
|
||||
items: [
|
||||
{
|
||||
appId: 1,
|
||||
id: 1,
|
||||
name: 'string',
|
||||
description: 'description',
|
||||
},
|
||||
{
|
||||
appId: 2,
|
||||
id: 2,
|
||||
name: 'string',
|
||||
description: 'description',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
|
||||
const getPagedReportsResponse = apiResponse.asGetPagedReportsResponseType();
|
||||
const getPagedReportsResponse =
|
||||
apiResponse.asGetPagedReportsResponseType();
|
||||
|
||||
|
||||
expect(getPagedReportsResponse).to.be.an.instanceof(
|
||||
ApiResponse<GetPagedReportsResponse>
|
||||
);
|
||||
expect(getPagedReportsResponse.data).to.be.an.instanceof(
|
||||
GetPagedReportsResponse
|
||||
);
|
||||
expect(getPagedReportsResponse.data).to.have.property('pageSize', 2);
|
||||
expect(getPagedReportsResponse.data).to.have.property('pageNumber', 1);
|
||||
expect(getPagedReportsResponse.data).to.have.property('totalRecords', 2);
|
||||
expect(getPagedReportsResponse.data).to.have.property('totalPages', 1);
|
||||
|
||||
expect(getPagedReportsResponse.data)
|
||||
.to.have.property('items')
|
||||
.to.be.an('array')
|
||||
.that.has.lengthOf(2);
|
||||
|
||||
expect(getPagedReportsResponse.data).to.not.be.null;
|
||||
|
||||
if (getPagedReportsResponse.data != null) {
|
||||
getPagedReportsResponse.data.items.forEach((report) => {
|
||||
expect(report).to.be.an.instanceof(Report);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('GetPagedReportsResponse', function () {
|
||||
it('should be defined', function () {
|
||||
expect(GetPagedReportsResponse).to.be.not.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(GetPagedReportsResponse).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have a constructor that takes 5 arguments', function () {
|
||||
expect(GetPagedReportsResponse).to.have.lengthOf(5);
|
||||
});
|
||||
|
||||
it('should create a new instance of GetPagedReportsResponse', function () {
|
||||
expect(new GetPagedReportsResponse([], 1, 1, 1, 1)).to.be.an.instanceOf(
|
||||
GetPagedReportsResponse
|
||||
);
|
||||
});
|
||||
|
||||
it('should set its properties correctly', function () {
|
||||
const response = new GetPagedReportsResponse([], 1, 1, 1, 1);
|
||||
expect(response.items).to.be.an('array');
|
||||
expect(response.pageNumber).to.be.equal(1);
|
||||
expect(response.pageSize).to.be.equal(1);
|
||||
expect(response.totalPages).to.be.equal(1);
|
||||
expect(response.totalRecords).to.be.equal(1);
|
||||
});
|
||||
});
|
||||
@@ -22,6 +22,8 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { ListItemRequest } from '../src/models/ListItemRequest';
|
||||
import { ListItemResponse } from '../src/models/ListItemResponse';
|
||||
import { Report } from '../src/models/Report';
|
||||
import { GetPagedReportsResponse } from '../src/models/GetPagedReportsResponse';
|
||||
|
||||
describe('OnspringClient', function () {
|
||||
const baseUrl = 'https://api.onspring.dev';
|
||||
@@ -2447,4 +2449,186 @@ describe('OnspringClient', function () {
|
||||
expect(result.data).to.be.null;
|
||||
});
|
||||
});
|
||||
|
||||
describe('getReportsByAppId', function () {
|
||||
it('should be defined', function () {
|
||||
expect(OnspringClient.prototype.getReportsByAppId).to.not.be.undefined;
|
||||
});
|
||||
|
||||
it('should be a function', function () {
|
||||
expect(OnspringClient.prototype.getReportsByAppId).to.be.a('function');
|
||||
});
|
||||
|
||||
it('should return a promise', function () {
|
||||
expect(
|
||||
new OnspringClient(baseUrl, apiKey).getReportsByAppId(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: {
|
||||
pageSize: 2,
|
||||
pageNumber: 1,
|
||||
totalRecords: 2,
|
||||
totalPages: 1,
|
||||
items: [
|
||||
{
|
||||
id: 1,
|
||||
appId: 1,
|
||||
name: 'Report 1',
|
||||
description: 'Report 1 Description',
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
appId: 1,
|
||||
name: 'Report 2',
|
||||
description: 'Report 2 Description',
|
||||
},
|
||||
],
|
||||
},
|
||||
headers: {},
|
||||
config: {} as InternalAxiosRequestConfig,
|
||||
} as AxiosResponse)
|
||||
);
|
||||
|
||||
sinon.stub(client, '_client' as any).value(mockAxiosClient);
|
||||
|
||||
const result = await client.getReportsByAppId(1);
|
||||
|
||||
expect(result).to.be.instanceOf(ApiResponse<GetPagedReportsResponse>);
|
||||
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.not.be.null;
|
||||
expect(result.data).to.be.instanceOf(GetPagedReportsResponse);
|
||||
expect(result.data).to.have.property('pageSize', 2);
|
||||
expect(result.data).to.have.property('pageNumber', 1);
|
||||
expect(result.data).to.have.property('totalRecords', 2);
|
||||
expect(result.data).to.have.property('totalPages', 1);
|
||||
expect(result.data)
|
||||
.to.have.property('items')
|
||||
.that.is.an('array')
|
||||
.with.lengthOf(2);
|
||||
|
||||
if (result.data != null) {
|
||||
result.data.items.forEach((item) => {
|
||||
expect(item).to.be.instanceOf(Report);
|
||||
expect(item).to.have.property('id');
|
||||
expect(item).to.have.property('appId');
|
||||
expect(item).to.have.property('name');
|
||||
expect(item).to.have.property('description');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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.getReportsByAppId(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.getReportsByAppId(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.getReportsByAppId(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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Report } from '../src/models/Report';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('Report', function () {
|
||||
it('should be defined', function () {
|
||||
expect(Report).to.be.not.undefined;
|
||||
});
|
||||
|
||||
it('should have a constructor', function () {
|
||||
expect(Report).to.have.property('constructor');
|
||||
});
|
||||
|
||||
it('should have a constructor that takes 4 arguments', function () {
|
||||
expect(Report).to.have.lengthOf(4);
|
||||
});
|
||||
|
||||
it('should create a new instance of Report', function () {
|
||||
expect(
|
||||
new Report(1, 1, 'report', 'report description')
|
||||
).to.be.an.instanceOf(Report);
|
||||
});
|
||||
|
||||
it('should set its properties correctly', function () {
|
||||
const report = new Report(1, 1, 'report', 'report description');
|
||||
expect(report.id).to.be.equal(1);
|
||||
expect(report.appId).to.be.equal(1);
|
||||
expect(report.name).to.be.equal('report');
|
||||
expect(report.description).to.be.equal('report description');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user