fix: begin writing Lists integration tests. fix: list item response model's id value should be of type string so modified the created with id response model that it extends so that the id property is generically typed and type can be passed in.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import { ListItemRequest } from './../../src';
|
||||
import { OnspringClient } from '../../src';
|
||||
import { expect } from 'chai';
|
||||
import { baseURL, apiKey } from '../mochaRootHooks';
|
||||
|
||||
describe('addOrUpdateListItem', function () {
|
||||
this.timeout(30000);
|
||||
this.retries(3);
|
||||
|
||||
const newListItemIds: string[] = [];
|
||||
|
||||
// Delete the list items that were created during the test
|
||||
after(async function () {
|
||||
for (const id of newListItemIds) {
|
||||
await deleteListItem(id);
|
||||
}
|
||||
});
|
||||
|
||||
it('should add a list item', async function () {
|
||||
const client = new OnspringClient(baseURL, apiKey);
|
||||
|
||||
if (process.env.TEST_LIST_FIELD === undefined) {
|
||||
expect.fail('TEST_LIST_FIELD is not defined');
|
||||
}
|
||||
|
||||
if (process.env.TEST_LIST_ID === undefined) {
|
||||
expect.fail('TEST_LIST_ID is not defined');
|
||||
}
|
||||
|
||||
const request = new ListItemRequest(
|
||||
parseInt(process.env.TEST_LIST_ID),
|
||||
null,
|
||||
`added_list_value_${new Date().getTime()}}`,
|
||||
1,
|
||||
'#000000'
|
||||
);
|
||||
|
||||
const response = await client.addOrUpdateListItem(request);
|
||||
|
||||
expect(response.statusCode).to.equal(201);
|
||||
expect(response.isSuccessful).to.be.true;
|
||||
expect(response.message).to.equal('');
|
||||
expect(response.data).to.not.be.null;
|
||||
|
||||
if (response.data != null) {
|
||||
expect(response.data.id).to.not.be.null;
|
||||
newListItemIds.push(response.data.id);
|
||||
}
|
||||
});
|
||||
|
||||
it('should update a list item', async function () {
|
||||
const client = new OnspringClient(baseURL, apiKey);
|
||||
|
||||
if (process.env.TEST_LIST_FIELD === undefined) {
|
||||
expect.fail('TEST_LIST_FIELD is not defined');
|
||||
}
|
||||
|
||||
if (process.env.TEST_LIST_ID === undefined) {
|
||||
expect.fail('TEST_LIST_ID is not defined');
|
||||
}
|
||||
|
||||
const request = new ListItemRequest(
|
||||
parseInt(process.env.TEST_LIST_ID),
|
||||
null,
|
||||
`added_list_value_${new Date().getTime()}}`,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
const response = await client.addOrUpdateListItem(request);
|
||||
const listItemId = response.data?.id;
|
||||
|
||||
if (listItemId == null) {
|
||||
expect.fail('new listItemId is null');
|
||||
}
|
||||
|
||||
newListItemIds.push(listItemId);
|
||||
|
||||
const updateRequest = new ListItemRequest(
|
||||
parseInt(process.env.TEST_LIST_ID),
|
||||
listItemId,
|
||||
`updated_list_value_${new Date().getTime()}}`,
|
||||
1,
|
||||
'#000000'
|
||||
);
|
||||
|
||||
const updateResponse = await client.addOrUpdateListItem(updateRequest);
|
||||
|
||||
expect(updateResponse.statusCode).to.equal(200);
|
||||
expect(updateResponse.isSuccessful).to.be.true;
|
||||
expect(updateResponse.message).to.equal('');
|
||||
expect(updateResponse.data).to.not.be.null;
|
||||
});
|
||||
|
||||
// it('should return a 401 error when an invalid api key is used', async function () {});
|
||||
|
||||
// it('should return a 403 error when api key does not have access to the list', async function () {});
|
||||
|
||||
// it('should return a 404 error when the list does not exist', async function () {});
|
||||
|
||||
// it('should return a 404 error when the list item does not exist', async function () {});
|
||||
});
|
||||
|
||||
async function deleteListItem(listItemIds: string): Promise<void> {
|
||||
const client = new OnspringClient(baseURL, apiKey);
|
||||
|
||||
if (process.env.TEST_LIST_ID === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
await client.deleteListItemById(
|
||||
parseInt(process.env.TEST_LIST_ID),
|
||||
listItemIds
|
||||
);
|
||||
}
|
||||
@@ -219,17 +219,19 @@ export class ApiResponse<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* @method asCreatedWithIdResponseType - Converts the ApiResponse to an ApiResponse<CreatedWithIdResponse>.
|
||||
* @returns {ApiResponse<CreatedWithIdResponse>} - An ApiResponse<CreatedWithIdResponse>.
|
||||
* @method asCreatedWithIdResponseType - Converts the ApiResponse to an ApiResponse<CreatedWithIdResponse<T>>.
|
||||
* @returns {ApiResponse<CreatedWithIdResponse<T>>} - An ApiResponse<CreatedWithIdResponse<T>>.
|
||||
*/
|
||||
public asCreatedWithIdResponseType(): ApiResponse<CreatedWithIdResponse> {
|
||||
public asCreatedWithIdResponseType<T>(): ApiResponse<
|
||||
CreatedWithIdResponse<T>
|
||||
> {
|
||||
const apiResponse = this as ApiResponse<any>;
|
||||
|
||||
const createdWithIdResponse = new CreatedWithIdResponse(
|
||||
const createdWithIdResponse = new CreatedWithIdResponse<T>(
|
||||
apiResponse.data.id
|
||||
);
|
||||
|
||||
return new ApiResponse<CreatedWithIdResponse>(
|
||||
return new ApiResponse<CreatedWithIdResponse<T>>(
|
||||
apiResponse.statusCode,
|
||||
apiResponse.message,
|
||||
createdWithIdResponse
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
export class CreatedWithIdResponse {
|
||||
export class CreatedWithIdResponse<T> {
|
||||
/**
|
||||
* @property {number} id - The id of the created object.
|
||||
* @property {T} id - The id of the created object.
|
||||
*/
|
||||
public id: number;
|
||||
public id: T;
|
||||
|
||||
/**
|
||||
* @constructor - Creates a new instance of the CreatedWithIdResponse class.
|
||||
* @param id - The id of the created object.
|
||||
* @returns {CreatedWithIdResponse} - A new instance of the CreatedWithIdResponse class.
|
||||
* @returns {CreatedWithIdResponse<T>} - A new instance of the CreatedWithIdResponse class.
|
||||
*/
|
||||
constructor(id: number) {
|
||||
constructor(id: T) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ export class ListItemRequest {
|
||||
public listId: number;
|
||||
|
||||
/**
|
||||
* @property {string} id - The id for the list item.
|
||||
* @property {string | null} id - The id for the list item.
|
||||
*/
|
||||
public id: string;
|
||||
public id: string | null;
|
||||
|
||||
/**
|
||||
* @property {string} name - The name of the list item.
|
||||
@@ -18,30 +18,30 @@ export class ListItemRequest {
|
||||
public name: string;
|
||||
|
||||
/**
|
||||
* @property {number} numericValue - The numeric value of the list item.
|
||||
* @property {number | null} numericValue - The numeric value of the list item.
|
||||
*/
|
||||
public numericValue: number;
|
||||
public numericValue: number | null;
|
||||
|
||||
/**
|
||||
* @property {string} color - The color of the list item.
|
||||
* @property {string | null} color - The color of the list item.
|
||||
*/
|
||||
public color: string;
|
||||
public color: string | null;
|
||||
|
||||
/**
|
||||
* @constructor - Creates a new instance of the ListItemRequest class.
|
||||
* @param {number} listId - The id for the list that the item belongs to.
|
||||
* @param {string} id - The id for the list item.
|
||||
* @param {string | null} id - The id for the list item.
|
||||
* @param {string} name - The name of the list item.
|
||||
* @param {number} numericValue - The numeric value of the list item.
|
||||
* @param {string} color - The color of the list item.
|
||||
* @param {number | null} numericValue - The numeric value of the list item.
|
||||
* @param {string | null} color - The color of the list item.
|
||||
* @returns {ListItemRequest} - A new instance of the ListItemRequest class.
|
||||
*/
|
||||
constructor(
|
||||
listId: number,
|
||||
id: string,
|
||||
id: string | null,
|
||||
name: string,
|
||||
numericValue: number,
|
||||
color: string
|
||||
numericValue: number | null,
|
||||
color: string | null
|
||||
) {
|
||||
this.listId = listId;
|
||||
this.id = id;
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
|
||||
|
||||
/**
|
||||
* @class ListItemResponse - Represents a respons when a list item is created or updated.
|
||||
*/
|
||||
export class ListItemResponse extends CreatedWithIdResponse {
|
||||
export class ListItemResponse extends CreatedWithIdResponse<string> {
|
||||
/**
|
||||
* @constructor - Creates a new instance of ListItemResponse.
|
||||
* @param {number} id - The id of the list item.
|
||||
* @param {string} id - The id of the list item.
|
||||
* @returns {ListItemResponse} - A new instance of ListItemResponse.
|
||||
*/
|
||||
constructor(id: number) {
|
||||
constructor(id: string) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,11 +264,11 @@ export class OnspringClient {
|
||||
/**
|
||||
* @method saveFile - Saves a file to a record in Onspring.
|
||||
* @param {SaveFileRequest} request - The request that will be used to save the file.
|
||||
* @returns {Promise<ApiResponse<CreatedWithIdResponse>>} - A promise that resolves to an ApiResponse of type CreatedWithIdResponse.
|
||||
* @returns {Promise<ApiResponse<CreatedWithIdResponse<number>>>} - A promise that resolves to an ApiResponse of type CreatedWithIdResponse.
|
||||
*/
|
||||
public async saveFile(
|
||||
request: SaveFileRequest
|
||||
): Promise<ApiResponse<CreatedWithIdResponse>> {
|
||||
): Promise<ApiResponse<CreatedWithIdResponse<number>>> {
|
||||
const endpoint = EndpointFactory.getSaveFileEndpoint();
|
||||
const formData = request.asFormData();
|
||||
const apiResponse = await this.post<any>(endpoint, formData, {
|
||||
@@ -279,7 +279,7 @@ export class OnspringClient {
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
return apiResponse.asCreatedWithIdResponseType();
|
||||
return apiResponse.asCreatedWithIdResponseType<number>();
|
||||
}
|
||||
|
||||
public async deleteFileById(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
|
||||
|
||||
export class SaveRecordResponse extends CreatedWithIdResponse {
|
||||
export class SaveRecordResponse extends CreatedWithIdResponse<number> {
|
||||
public warnings: string[];
|
||||
|
||||
constructor(id: number, warnings: string[] = []) {
|
||||
|
||||
@@ -780,7 +780,7 @@ describe('ApiResponse', function () {
|
||||
const createdWithIdResponse = apiResponse.asCreatedWithIdResponseType();
|
||||
|
||||
expect(createdWithIdResponse).to.be.instanceOf(
|
||||
ApiResponse<CreatedWithIdResponse>
|
||||
ApiResponse<CreatedWithIdResponse<any>>
|
||||
);
|
||||
expect(createdWithIdResponse.data).to.be.instanceOf(
|
||||
CreatedWithIdResponse
|
||||
|
||||
@@ -12,7 +12,7 @@ describe('ListItemResponse', function () {
|
||||
});
|
||||
|
||||
it('should have a constructor that takes an id and sets the id property', function () {
|
||||
const id = 1;
|
||||
const id = '3fa85f64-5717-4562-b3fc-2c963f66afa6';
|
||||
const listItemResponse = new ListItemResponse(id);
|
||||
expect(listItemResponse).to.have.property('id', id);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user