Skip to content

Commit

Permalink
feat(tests): add queryFieldNameGetterFn callback unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Apr 24, 2020
1 parent 6d8955c commit 6426793
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/common/src/services/__tests__/filter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,42 @@ describe('FilterService', () => {

// expect(output).toBe(true);
// });

it('should execute "queryFieldNameGetterFn()" callback and return True when input value matches the full name', () => {
const mockColumn1 = { id: 'name', field: 'name', filterable: true, queryFieldNameGetterFn: (dataContext) => 'fullName' } as Column;
jest.spyOn(gridStub, 'getColumns').mockReturnValue([mockColumn1]);
jest.spyOn(dataViewStub, 'getIdxById').mockReturnValue(0);

service.init(gridStub);
const columnFilters = { name: { columnDef: mockColumn1, columnId: 'name', operator: 'EQ', searchTerms: ['John Doe'] } };
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });

expect(output).toBe(true);
});

it('should execute "queryFieldNameGetterFn()" callback and return False when input value is not fullName but just the firstName', () => {
const mockColumn1 = { id: 'name', field: 'name', filterable: true, queryFieldNameGetterFn: (dataContext) => 'fullName' } as Column;
jest.spyOn(gridStub, 'getColumns').mockReturnValue([mockColumn1]);
jest.spyOn(dataViewStub, 'getIdxById').mockReturnValue(0);

service.init(gridStub);
const columnFilters = { name: { columnDef: mockColumn1, columnId: 'name', operator: 'EQ', searchTerms: ['John'] } };
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });

expect(output).toBe(false);
});

it('should execute "queryFieldNameGetterFn()" callback and return False when input value is not fullName but just the lastName', () => {
const mockColumn1 = { id: 'name', field: 'name', filterable: true, queryFieldNameGetterFn: (dataContext) => 'fullName' } as Column;
jest.spyOn(gridStub, 'getColumns').mockReturnValue([mockColumn1]);
jest.spyOn(dataViewStub, 'getIdxById').mockReturnValue(0);

service.init(gridStub);
const columnFilters = { name: { columnDef: mockColumn1, columnId: 'name', operator: 'EQ', searchTerms: ['Doe'] } };
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });

expect(output).toBe(false);
});
});

describe('onBackendFilterChange method', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/common/src/services/__tests__/sort.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,24 @@ describe('SortService', () => {
]);
});

it('should sort the data with 2 sorters which the second is by executing the "queryFieldNameGetterFn()" callback and sort by the field returned by it', () => {
const mockSortedCols = [
{ sortCol: { id: 'address', field: 'address', queryField: 'lastName' }, sortAsc: true },
{ sortCol: { id: 'random', field: 'random', queryFieldNameGetterFn: (dataContext) => 'zip' }, sortAsc: false },
] as ColumnSort[];

dataset.sort((row1, row2) => service.sortComparers(mockSortedCols, row1, row2));

expect(dataset).toEqual([
{ firstName: 'John', lastName: 'Doe', age: 22, address: { zip: 123456 } },
{ firstName: 'Jane', lastName: 'Doe', age: 27, address: { zip: 123456 } },
{ firstName: 'Christopher', lastName: 'McDonald', age: 40, address: { zip: 555555 } },
{ firstName: 'Erla', lastName: 'Richard', age: 101, address: { zip: 444444 } },
{ firstName: 'Barbara', lastName: 'Smith', age: 1, address: { zip: 222222 } },
{ firstName: 'Jane', lastName: 'Smith', age: 40, address: { zip: 333333 } },
]);
});

it('should sort the data with a sorter that is a complex object (following the dot notation in its field name)', () => {
const mockSortedCols = [
{ sortCol: { id: 'address', field: 'address.zip' }, sortAsc: true },
Expand Down

0 comments on commit 6426793

Please sign in to comment.