feat: implement getFieldById method

This commit is contained in:
StevanFreeborn
2023-02-02 21:37:40 -06:00
parent 8b61dfa36a
commit a1d3683603
6 changed files with 340 additions and 2 deletions
+41
View File
@@ -3,6 +3,9 @@ import { expect } from 'chai';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
import { CollectionResponse } from '../src/models/CollectionResponse';
import { Field } from '../src/models/Field';
import { FieldStatus } from '../src/enums/FieldStatus';
import { FieldType } from '../src/enums/FieldType';
describe('ApiResponse', function () {
it('should be defined', function () {
@@ -228,4 +231,42 @@ describe('ApiResponse', function () {
}
});
});
describe('AsFieldType', function () {
it('should be defined', function () {
expect(ApiResponse.prototype.AsFieldType).to.not.be.undefined;
});
it('should have no parameters', function () {
expect(ApiResponse.prototype.AsFieldType).to.have.lengthOf(0);
});
it('should return an ApiResponse<Field> when data contain a field', function () {
const mockResponseData = {
id: 1,
appId: 1,
name: 'Text Field',
type: 'Text',
status: 'Enabled',
isRequired: false,
isUnique: false,
};
const apiResponse = new ApiResponse<any>(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.AsFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse);
expect(fieldResponse.data).to.be.instanceOf(Field);
expect(fieldResponse.data).to.not.be.null;
if (fieldResponse.data != null) {
expect(fieldResponse.data.id).to.equal(1);
expect(fieldResponse.data.appId).to.equal(1);
expect(fieldResponse.data.name).to.equal('Text Field');
expect(fieldResponse.data.type).to.equal(FieldType.Text);
expect(fieldResponse.data.status).to.equal(FieldStatus.Enabled);
expect(fieldResponse.data.isRequired).to.equal(false);
expect(fieldResponse.data.isUnique).to.equal(false);
}
});
});
});
+53
View File
@@ -0,0 +1,53 @@
import { expect } from 'chai';
import { Field } from '../src/models/Field';
describe('Field', function () {
it('should be defined', function () {
expect(Field).to.not.be.undefined;
});
it('should have a constructor', function () {
expect(Field).to.have.property('constructor');
});
it('should have 7 parameters', function () {
expect(Field).to.have.lengthOf(7);
});
it('should create a new instance of the Field class', function () {
expect(
() => new Field(1, 1, 'Text Field', 'Text', 'Enabled', true, false)
).to.not.throw();
});
it('should create a new instance of the Field class with the correct properties and values', function () {
const field = new Field(1, 1, 'Text Field', 'Text', 'Enabled', true, false);
expect(field).to.have.property('id');
expect(field).to.have.property('appId');
expect(field).to.have.property('name');
expect(field).to.have.property('type');
expect(field).to.have.property('status');
expect(field).to.have.property('isRequired');
expect(field).to.have.property('isUnique');
expect(field.id).to.equal(1);
expect(field.appId).to.equal(1);
expect(field.name).to.equal('Text Field');
expect(field.type).to.equal('Text');
expect(field.status).to.equal('Enabled');
expect(field.isRequired).to.equal(true);
expect(field.isUnique).to.equal(false);
});
it('should throw an error when the type parameter is not a valid value', function () {
expect(
() => new Field(1, 1, 'Text Field', 'Invalid', 'Enabled', true, false)
).to.throw();
});
it('should throw an error when the status parameter is not a valid value', function () {
expect(
() => new Field(1, 1, 'Text Field', 'Text', 'Unabled', true, false)
).to.throw();
});
});
+173
View File
@@ -6,6 +6,9 @@ import * as sinon from 'sinon';
import { GetPagedAppsResponse } from '../src/models/GetPagedAppsResponse';
import { App } from '../src/models/App';
import { CollectionResponse } from '../src/models/CollectionResponse';
import { Field } from '../src/models/Field';
import { FieldStatus } from '../src/enums/FieldStatus';
import { FieldType } from '../src/enums/FieldType';
describe('OnspringClient', function () {
const baseUrl = 'https://api.onspring.dev';
@@ -658,4 +661,174 @@ describe('OnspringClient', function () {
expect(result.data).to.be.null;
});
});
describe('getFieldById', function () {
it('should be defined', function () {
expect(new OnspringClient(baseUrl, apiKey).getFieldById).to.not.be
.undefined;
});
it('should be a function', function () {
expect(new OnspringClient(baseUrl, apiKey).getFieldById).to.be.a(
'function'
);
});
it('should have 1 parameter', function () {
expect(new OnspringClient(baseUrl, apiKey).getFieldById).to.have.lengthOf(
1
);
});
it('should return a promise', function () {
expect(new OnspringClient(baseUrl, apiKey).getFieldById(1)).to.be.a(
'promise'
);
});
it('should return a promise that resolves to an api response of a field 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: {
id: 1,
appId: 1,
name: 'Text Field',
type: 'Text',
status: 'Enabled',
isRequired: false,
isUnique: false,
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getFieldById(1);
expect(result).to.be.instanceOf(ApiResponse<Field>);
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.be.instanceOf(Field);
expect(result.data).to.have.property('id', 1);
expect(result.data).to.have.property('appId', 1);
expect(result.data).to.have.property('name', 'Text Field');
expect(result.data).to.have.property('type', FieldType.Text);
expect(result.data).to.have.property('status', FieldStatus.Enabled);
expect(result.data).to.have.property('isRequired', false);
expect(result.data).to.have.property('isUnique', false);
}
});
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.getFieldById(1);
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, 'get').returns(
Promise.resolve({
status: 403,
statusText: 'Forbidden',
data: {
message: 'Client does not have access to read field: 1',
},
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getFieldById(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',
'Client does not have access to read field: 1'
);
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, 'get').returns(
Promise.resolve({
status: 404,
statusText: 'Not Found',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.getFieldById(1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
});
});