Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit e10f723

Browse files
committed
fix(backend): able to preset filters on hidden columns & all queried
- the backend services were using SlickGrid `getColumns` to get the column definitions but that returns only the visible columns, however we should be able to preset filters on hidden columns and also expect all columns to be queried (it's not because the column is hidden that we shouldn't be able to filter it and query it)
1 parent 10e756c commit e10f723

File tree

8 files changed

+127
-71
lines changed

8 files changed

+127
-71
lines changed

src/app/modules/angular-slickgrid/components/__tests__/angular-slickgrid-constructor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
11511151
component.ngAfterViewInit();
11521152

11531153
expect(bindBackendSpy).toHaveBeenCalledWith(mockGrid);
1154-
expect(initSpy).toHaveBeenCalledWith(mockGraphqlOptions, mockPagination, mockGrid);
1154+
expect(initSpy).toHaveBeenCalledWith(mockGraphqlOptions, mockPagination, mockGrid, sharedService);
11551155
});
11561156

11571157
it('should call bind backend sorting when "enableSorting" is set', () => {

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
622622
const backendApi = gridOptions.backendServiceApi;
623623

624624
if (backendApi && backendApi.service && backendApi.service.init) {
625-
backendApi.service.init(backendApi.options, gridOptions.pagination, this.grid);
625+
backendApi.service.init(backendApi.options, gridOptions.pagination, this.grid, this.sharedService);
626626
}
627627
}
628628

@@ -1081,6 +1081,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
10811081

10821082
// finally set the new presets columns (including checkbox selector if need be)
10831083
this.grid.setColumns(gridColumns);
1084+
this.sharedService.visibleColumns = gridColumns;
10841085
}
10851086
}
10861087
}

src/app/modules/angular-slickgrid/models/backendService.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
PaginationChangedArgs,
1111
SortChangedArgs,
1212
} from './../models/index';
13+
import { SharedService } from '../services';
1314

1415
export interface BackendService {
1516
/** Backend Service options */
@@ -25,7 +26,7 @@ export interface BackendService {
2526
clearSorters?: () => void;
2627

2728
/** initialize the backend service with certain options */
28-
init?: (serviceOptions?: BackendServiceOption | any, pagination?: Pagination, grid?: any) => void;
29+
init?: (serviceOptions?: BackendServiceOption | any, pagination?: Pagination, grid?: any, sharedService?: SharedService) => void;
2930

3031
/** Get the dataset name */
3132
getDatasetName?: () => string;

src/app/modules/angular-slickgrid/services/__tests__/graphql.service.spec.ts

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ import { GraphqlService } from './../graphql.service';
33
import {
44
Column,
55
ColumnFilter,
6+
ColumnFilters,
67
ColumnSort,
78
CurrentFilter,
9+
CurrentSorter,
10+
FieldType,
811
FilterChangedArgs,
912
GraphqlServiceApi,
1013
GraphqlServiceOption,
1114
GridOption,
1215
MultiColumnSort,
13-
Pagination,
14-
ColumnFilters,
1516
OperatorType,
16-
FieldType,
17-
CurrentSorter,
17+
Pagination,
1818
} from '../../models';
19+
import { SharedService } from '../shared.service';
1920

2021
const DEFAULT_ITEMS_PER_PAGE = 25;
2122
const DEFAULT_PAGE_SIZE = 20;
2223

23-
function removeSpaces(textS) {
24+
function removeSpaces(textS: string) {
2425
return `${textS}`.replace(/\s+/g, '');
2526
}
2627

@@ -33,8 +34,8 @@ const gridOptionMock = {
3334
preProcess: jest.fn(),
3435
process: jest.fn(),
3536
postProcess: jest.fn(),
36-
} as GraphqlServiceApi
37-
} as GridOption;
37+
} as unknown as GraphqlServiceApi
38+
} as unknown as GridOption;
3839

3940
const gridStub = {
4041
autosizeColumns: jest.fn(),
@@ -53,9 +54,11 @@ describe('GraphqlService', () => {
5354
let service: GraphqlService;
5455
let paginationOptions: Pagination;
5556
let serviceOptions: GraphqlServiceOption;
57+
let sharedService: SharedService;
5658

5759
beforeEach(() => {
5860
mockColumns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
61+
sharedService = new SharedService();
5962
service = new GraphqlService();
6063
serviceOptions = {
6164
columnDefinitions: mockColumns,
@@ -67,7 +70,7 @@ describe('GraphqlService', () => {
6770
pageSize: 10,
6871
totalItems: 100
6972
};
70-
gridOptionMock.backendServiceApi.service = service;
73+
gridOptionMock.backendServiceApi!.service = service;
7174
});
7275

7376
afterEach(() => {
@@ -107,7 +110,7 @@ describe('GraphqlService', () => {
107110
});
108111

109112
it('should throw an error when no dataset is provided in the service options after service init', () => {
110-
service.init({ datasetName: undefined });
113+
service.init({ datasetName: undefined as any });
111114
expect(() => service.buildQuery()).toThrow('GraphQL Service requires the "datasetName" property to properly build the GraphQL query');
112115
});
113116

@@ -148,7 +151,7 @@ describe('GraphqlService', () => {
148151

149152
it('should return a simple query with pagination set and nodes that includes at least "id" when the column definitions is an empty array', () => {
150153
const expectation = `query{ users(first:10, offset:0){ totalCount, nodes{ id }}}`;
151-
const columns = [];
154+
const columns: Column[] = [];
152155

153156
service.init({ datasetName: 'users', columnDefinitions: columns }, paginationOptions, gridStub);
154157
const query = service.buildQuery();
@@ -188,7 +191,7 @@ describe('GraphqlService', () => {
188191

189192
it('should return a simple query with pagination set and nodes that includes at least "id" when the column definitions is an empty array when using cursor', () => {
190193
const expectation = `query{users(first:20) { totalCount, nodes{id}, pageInfo{ hasNextPage,hasPreviousPage,endCursor,startCursor }, edges{ cursor }}}`;
191-
const columns = [];
194+
const columns: Column[] = [];
192195

193196
service.init({ datasetName: 'users', columnDefinitions: columns, isWithCursor: true }, paginationOptions, gridStub);
194197
service.updatePagination(3, 20);
@@ -442,7 +445,7 @@ describe('GraphqlService', () => {
442445
});
443446

444447
it('should return empty string when dataset name is undefined', () => {
445-
service.init({ datasetName: undefined, columnDefinitions: [] });
448+
service.init({ datasetName: undefined as any, columnDefinitions: [] });
446449
const output = service.getDatasetName();
447450
expect(output).toBe('');
448451
});
@@ -504,7 +507,7 @@ describe('GraphqlService', () => {
504507
} as FilterChangedArgs;
505508

506509
service.init(serviceOptions, paginationOptions, gridStub);
507-
const query = service.processOnFilterChanged(null, mockFilterChangedArgs);
510+
const query = service.processOnFilterChanged(null as any, mockFilterChangedArgs);
508511
const currentFilters = service.getCurrentFilters();
509512

510513
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
@@ -534,7 +537,7 @@ describe('GraphqlService', () => {
534537
} as FilterChangedArgs;
535538

536539
service.init(serviceOptions, paginationOptions, gridStub);
537-
const query = service.processOnFilterChanged(null, mockFilterChangedArgs);
540+
const query = service.processOnFilterChanged(null as any, mockFilterChangedArgs);
538541
const currentFilters = service.getCurrentFilters();
539542

540543
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
@@ -553,7 +556,7 @@ describe('GraphqlService', () => {
553556
const querySpy = jest.spyOn(service, 'buildQuery');
554557

555558
service.init(serviceOptions, paginationOptions, gridStub);
556-
const query = service.processOnPaginationChanged(null, { newPage: 3, pageSize: 20 });
559+
const query = service.processOnPaginationChanged(null as any, { newPage: 3, pageSize: 20 });
557560
const currentPagination = service.getCurrentPagination();
558561

559562
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
@@ -567,7 +570,7 @@ describe('GraphqlService', () => {
567570

568571
service.init(serviceOptions, paginationOptions, gridStub);
569572
// @ts-ignore
570-
const query = service.processOnPaginationChanged(null, { newPage: 3 });
573+
const query = service.processOnPaginationChanged(null as any, { newPage: 3 });
571574
const currentPagination = service.getCurrentPagination();
572575

573576
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
@@ -581,7 +584,7 @@ describe('GraphqlService', () => {
581584

582585
service.init(serviceOptions, undefined, gridStub);
583586
// @ts-ignore
584-
const query = service.processOnPaginationChanged(null, { newPage: 3 });
587+
const query = service.processOnPaginationChanged(null as any, { newPage: 3 });
585588
const currentPagination = service.getCurrentPagination();
586589

587590
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
@@ -598,7 +601,7 @@ describe('GraphqlService', () => {
598601
const mockSortChangedArgs = { columnId: 'gender', sortCol: mockColumn, sortAsc: false, multiColumnSort: false } as ColumnSort;
599602

600603
service.init(serviceOptions, paginationOptions, gridStub);
601-
const query = service.processOnSortChanged(null, mockSortChangedArgs);
604+
const query = service.processOnSortChanged(null as any, mockSortChangedArgs);
602605

603606
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
604607
expect(querySpy).toHaveBeenCalled();
@@ -616,7 +619,7 @@ describe('GraphqlService', () => {
616619
const mockSortChangedArgs = { sortCols: [mockColumnSort, mockColumnSortName], multiColumnSort: true, grid: gridStub } as MultiColumnSort;
617620

618621
service.init(serviceOptions, paginationOptions, gridStub);
619-
const query = service.processOnSortChanged(null, mockSortChangedArgs);
622+
const query = service.processOnSortChanged(null as any, mockSortChangedArgs);
620623

621624
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
622625
expect(querySpy).toHaveBeenCalled();
@@ -1050,7 +1053,7 @@ describe('GraphqlService', () => {
10501053
const expectation = `query{users(first:10, offset:0, filterBy:[{field:gender, operator:EQ, value:""}]) { totalCount,nodes{ id,company,gender,name } }}`;
10511054
const mockColumn = { id: 'gender', field: 'gender' } as Column;
10521055
const mockColumnFilters = {
1053-
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: [undefined], operator: 'EQ', type: FieldType.string },
1056+
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: [undefined as any], operator: 'EQ', type: FieldType.string },
10541057
} as ColumnFilters;
10551058

10561059
service.init(serviceOptions, paginationOptions, gridStub);
@@ -1121,16 +1124,38 @@ describe('GraphqlService', () => {
11211124
});
11221125

11231126
describe('presets', () => {
1127+
let mockColumns: Column[] = [];
1128+
11241129
beforeEach(() => {
11251130
serviceOptions.columnDefinitions = [{ id: 'company', field: 'company' }, { id: 'gender', field: 'gender' }, { id: 'duration', field: 'duration', type: FieldType.number }, { id: 'startDate', field: 'startDate' }];
11261131
});
11271132

11281133
it('should return a query with search having a range of exclusive numbers when the search value contains 2 dots (..) to represent a range of numbers', () => {
11291134
const expectation = `query{users(first:10, offset:0, filterBy:[{field:duration, operator:GE, value:"2"}, {field:duration, operator:LE, value:"33"}]) {
11301135
totalCount,nodes{ id,company,gender,duration,startDate } }}`;
1131-
const presetFilters = [
1132-
{ columnId: 'duration', searchTerms: ['2..33'] },
1133-
] as CurrentFilter[];
1136+
const presetFilters = [{ columnId: 'duration', searchTerms: ['2..33'] }] as CurrentFilter[];
1137+
1138+
service.init(serviceOptions, paginationOptions, gridStub);
1139+
service.updateFilters(presetFilters, true);
1140+
const query = service.buildQuery();
1141+
const currentFilters = service.getCurrentFilters();
1142+
1143+
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
1144+
expect(currentFilters).toEqual(presetFilters);
1145+
});
1146+
1147+
it('should return a query with all columns and search even when having hidden columns (basically when it is not part of the `getColumns()` return) when all passed are passed with the shared service', () => {
1148+
const expectation = `query{users(first:10, offset:0, filterBy:[{field:duration, operator:GE, value:"2"}, {field:duration, operator:LE, value:"33"}]) {
1149+
totalCount,nodes{ id,company,gender,duration,startDate } }}`;
1150+
const presetFilters = [{ columnId: 'duration', searchTerms: ['2..33'] }] as CurrentFilter[];
1151+
const mockColumnsCopy = [...mockColumns];
1152+
1153+
// remove "Gender" column from `getColumns` (to simulate hidden field)
1154+
mockColumnsCopy.splice(1, 1);
1155+
jest.spyOn(gridStub, 'getColumns').mockReturnValue(mockColumnsCopy);
1156+
1157+
// but still pass all columns to the service init
1158+
jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(mockColumns);
11341159

11351160
service.init(serviceOptions, paginationOptions, gridStub);
11361161
service.updateFilters(presetFilters, true);

0 commit comments

Comments
 (0)