Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"bracketSpacing": true,
"trailingComma": "es5",
"printWidth": 100,
"tabWidth": 2,
"tabWidth": 4,
"useTabs": false,
"endOfLine": "lf"
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"jasmineExplorer.nodeArgv": [
"--no-experimental-strip-types",
"-r",
"ts-node/register",
"-r",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('OAuthClientCredentialsClient', () => {
expect(fakeFormData.append).toHaveBeenCalledWith('grant_type', 'client_credentials');
expect(fakeFormData.append).toHaveBeenCalledWith('client_id', clientId);
expect(fakeFormData.append).toHaveBeenCalledWith('client_secret', clientSecret);
expect(fakeFormData.append).toHaveBeenCalledWith('scope', 'restricted');
expect((sut as any)._fetch).toHaveBeenCalledWith(
jasmine.anything(),
jasmine.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class OAuthClientCredentialsClient implements ApiClient {
body.append('grant_type', 'client_credentials');
body.append('client_id', this._clientId);
body.append('client_secret', this._clientSecret);
body.append('scope', 'restricted');
const request = {
method,
body,
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/table-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { TableDataClient } from './table-data-client/table-data-client';
export { TableDataClient, isSuccessResponse, isErrorResponse, ErrorResponse } from './table-data-client/table-data-client';
export { TableDataRequest } from './table-data-client/table-data-request';
export { TableDataResponse } from './table-data-client/table-data-response';
export { ColumnSortOrder } from './table-data-form-data-builder/column-sort-order';
Expand Down
286 changes: 148 additions & 138 deletions src/common/data/table-data/table-data-client/table-data-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,148 +1,158 @@
import { TableDataClient } from '@common';
import * as TableDataFormDataBuilder from '../table-data-form-data-builder/table-data-form-data-builder';
import { isErrorResponse } from './table-data-client';

describe('TableDataClient', () => {
let dataTableFormDataBuilder;
let url;
let formData;
let bugSplatApiClient;
let response;

let service: TableDataClient;

beforeEach(() => {
url = 'https://woot.com';
formData = { form: 'data!' };
response = [{ Rows: [{ yee: 'ha!' }], PageData: { woo: 'hoo!' } }];
dataTableFormDataBuilder = jasmine.createSpyObj('DataTableFormDataBuilder', [
'withDatabase',
'withColumnGroups',
'withFilterGroups',
'withPage',
'withPageSize',
'withSortColumn',
'withSortOrder',
'build'
]);
dataTableFormDataBuilder.withDatabase.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withColumnGroups.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withFilterGroups.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withPage.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withPageSize.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withSortColumn.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withSortOrder.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.build.and.returnValue(formData);
spyOn(TableDataFormDataBuilder, 'TableDataFormDataBuilder').and.returnValue(dataTableFormDataBuilder);

const json = () => response;
bugSplatApiClient = jasmine.createSpyObj('BugSplatApiClient', ['fetch']);
bugSplatApiClient.fetch.and.resolveTo({ json });

service = new TableDataClient(bugSplatApiClient, url);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

describe('getData', () => {
let database;
let columnGroups;
let filterGroups;
let page;
let pageSize;
let sortColumn;
let sortOrder;
let result;

beforeEach(async () => {
database = 'Black Rifle Coffee';
columnGroups = ['group', 'group again'];
filterGroups = ['filter', 'anotha 1'];
page = 9;
pageSize = 9001;
sortColumn = 'sorty';
sortOrder = 'mcOrder';
const response = await service.postGetData({
database,
columnGroups,
filterGroups,
page,
pageSize,
sortColumn,
sortOrder
});
result = await response.json();
let dataTableFormDataBuilder;
let url;
let formData;
let bugSplatApiClient;
let response;

let service: TableDataClient;

beforeEach(() => {
url = 'https://woot.com';
formData = { form: 'data!' };
response = { rows: [{ yee: 'ha!' }], pageData: { woo: 'hoo!' } };
dataTableFormDataBuilder = jasmine.createSpyObj('DataTableFormDataBuilder', [
'withDatabase',
'withColumnGroups',
'withFilterGroups',
'withPage',
'withPageSize',
'withSortColumn',
'withSortOrder',
'build',
]);
dataTableFormDataBuilder.withDatabase.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withColumnGroups.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withFilterGroups.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withPage.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withPageSize.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withSortColumn.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.withSortOrder.and.returnValue(dataTableFormDataBuilder);
dataTableFormDataBuilder.build.and.returnValue(formData);
spyOn(TableDataFormDataBuilder, 'TableDataFormDataBuilder').and.returnValue(
dataTableFormDataBuilder
);

const json = () => response;
bugSplatApiClient = jasmine.createSpyObj('BugSplatApiClient', ['fetch']);
bugSplatApiClient.fetch.and.resolveTo({ status: 200, json });

service = new TableDataClient(bugSplatApiClient, url);
});

it('should call DataTaableFormDataBuilder with database', () => {
expect(dataTableFormDataBuilder.withDatabase).toHaveBeenCalledWith(database);
it('should be created', () => {
expect(service).toBeTruthy();
});

it('should call DatatableFormDataBuilder with columng groups', () => {
expect(dataTableFormDataBuilder.withColumnGroups).toHaveBeenCalledWith(columnGroups);
describe('getData', () => {
let database;
let columnGroups;
let filterGroups;
let page;
let pageSize;
let sortColumn;
let sortOrder;
let result;

beforeEach(async () => {
database = 'Black Rifle Coffee';
columnGroups = ['group', 'group again'];
filterGroups = ['filter', 'anotha 1'];
page = 9;
pageSize = 9001;
sortColumn = 'sorty';
sortOrder = 'mcOrder';
const response = await service.postGetData({
database,
columnGroups,
filterGroups,
page,
pageSize,
sortColumn,
sortOrder,
});
if (isErrorResponse(response)) {
throw new Error((await response.json()).message);
}
result = await response.json();
});

it('should call DataTaableFormDataBuilder with database', () => {
expect(dataTableFormDataBuilder.withDatabase).toHaveBeenCalledWith(database);
});

it('should call DatatableFormDataBuilder with columng groups', () => {
expect(dataTableFormDataBuilder.withColumnGroups).toHaveBeenCalledWith(columnGroups);
});

it('should call DataTableFormDataBuilder with filter groups', () => {
expect(dataTableFormDataBuilder.withFilterGroups).toHaveBeenCalledWith(filterGroups);
});

it('should call DataTableFormDataBuilder with page', () => {
expect(dataTableFormDataBuilder.withPage).toHaveBeenCalledWith(page);
});

it('should call DataTableFormDataBuilder with page size', () => {
expect(dataTableFormDataBuilder.withPageSize).toHaveBeenCalledWith(pageSize);
});

it('should call DataTableFormDataBuilder with sort column', () => {
expect(dataTableFormDataBuilder.withSortColumn).toHaveBeenCalledWith(sortColumn);
});

it('should call DataTableFormDataBuilder with sort order', () => {
expect(dataTableFormDataBuilder.withSortOrder).toHaveBeenCalledWith(sortOrder);
});

it('should call build on DataTableFormDataBuilder', () => {
expect(dataTableFormDataBuilder.build).toHaveBeenCalled();
});

it('should call fetch with correct url', () => {
expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(url, jasmine.anything());
});

it('should call fetch with form data from DataTableFormDataBuilder', () => {
expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(
jasmine.anything(),
jasmine.objectContaining({
body: formData,
})
);
});

it('should return value from api', () => {
expect(result).toEqual(
jasmine.objectContaining({
rows: response.rows,
pageData: response.pageData,
})
);
});

it('should throw if api returns error response', async () => {
const expectedErrorMessage = 'Internal Server Error';
bugSplatApiClient.fetch.and.resolveTo({
status: 500,
message: expectedErrorMessage,
});
try {
await service.postGetData({
filterGroups,
page,
pageSize,
sortColumn,
sortOrder,
});
} catch (error: any) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe(expectedErrorMessage);
}
});
});

it('should call DataTableFormDataBuilder with filter groups', () => {
expect(dataTableFormDataBuilder.withFilterGroups).toHaveBeenCalledWith(filterGroups);
});

it('should call DataTableFormDataBuilder with page', () => {
expect(dataTableFormDataBuilder.withPage).toHaveBeenCalledWith(page);
});

it('should call DataTableFormDataBuilder with page size', () => {
expect(dataTableFormDataBuilder.withPageSize).toHaveBeenCalledWith(pageSize);
});

it('should call DataTableFormDataBuilder with sort column', () => {
expect(dataTableFormDataBuilder.withSortColumn).toHaveBeenCalledWith(sortColumn);
});

it('should call DataTableFormDataBuilder with sort order', () => {
expect(dataTableFormDataBuilder.withSortOrder).toHaveBeenCalledWith(sortOrder);
});

it('should call build on DataTableFormDataBuilder', () => {
expect(dataTableFormDataBuilder.build).toHaveBeenCalled();
});

it('should call fetch with correct url', () => {
expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(url, jasmine.anything());
});

it('should call fetch with form data from DataTableFormDataBuilder', () => {
expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(
jasmine.anything(),
jasmine.objectContaining({
body: formData
})
);
});

it('should return value from api', () => {
expect(result).toEqual(jasmine.objectContaining({
rows: response[0].Rows,
pageData: response[0].PageData
}));
});

it('should return empty array if api returns null', async () => {
bugSplatApiClient.fetch.and.resolveTo({ json: () => null });

const response = await service.postGetData({
filterGroups,
page,
pageSize,
sortColumn,
sortOrder
});
result = await response.json();

expect(result).toEqual(jasmine.objectContaining({
rows: [],
pageData: {}
}));
});
});
});
Loading