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

Commit 9b4d265

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(tests): add query tests after calling clearFilters/clearSorters
1 parent 270655c commit 9b4d265

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,23 @@ describe('GraphqlService', () => {
848848

849849
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
850850
});
851+
852+
it('should return a query without any sorting after clearFilters was called', () => {
853+
const expectation = `query{ users(first:10,offset:0) { totalCount,nodes{id, company, gender,name} }}`;
854+
const mockColumn = { id: 'gender', field: 'gender' } as Column;
855+
const mockColumnFilters = {
856+
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: ['female'], operator: 'EQ' },
857+
} as ColumnFilters;
858+
859+
service.init(serviceOptions, paginationOptions, gridStub);
860+
service.updateFilters(mockColumnFilters, false);
861+
service.clearFilters();
862+
const currentFilters = service.getCurrentFilters();
863+
const query = service.buildQuery();
864+
865+
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
866+
expect(currentFilters).toEqual([]);
867+
});
851868
});
852869

853870
describe('updateSorters method', () => {
@@ -927,7 +944,6 @@ describe('GraphqlService', () => {
927944
expect(currentSorters).toEqual([{ columnId: 'gender', direction: 'DESC' }, { columnId: 'name', direction: 'ASC' }]);
928945
});
929946

930-
931947
it('should return a query without the field sorter when its field property is missing', () => {
932948
const expectation = `query { users(first:10, offset:0, orderBy:[{field:gender, direction:DESC}]) {
933949
totalCount, nodes { id,company,gender,name }}}`;
@@ -944,5 +960,23 @@ describe('GraphqlService', () => {
944960
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
945961
expect(currentSorters).toEqual([{ columnId: 'gender', direction: 'DESC' }, { columnId: 'firstName', direction: 'ASC' }]);
946962
});
963+
964+
it('should return a query without any sorting after clearSorters was called', () => {
965+
const expectation = `query { users(first:10, offset:0) {
966+
totalCount, nodes { id,company,gender,name }}}`;
967+
const mockColumnSort = [
968+
{ columnId: 'gender', sortCol: { id: 'gender', field: 'gender' }, sortAsc: false },
969+
{ columnId: 'firstName', sortCol: { id: 'firstName', field: 'firstName' }, sortAsc: true }
970+
] as ColumnSort[];
971+
972+
service.init(serviceOptions, paginationOptions, gridStub);
973+
service.updateSorters(mockColumnSort);
974+
service.clearSorters();
975+
const query = service.buildQuery();
976+
const currentSorters = service.getCurrentSorters();
977+
978+
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
979+
expect(currentSorters).toEqual([]);
980+
});
947981
});
948982
});

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ describe('GridOdataService', () => {
152152

153153
describe('clearFilters method', () => {
154154
it('should call "updateOptions" to clear all filters', () => {
155-
const spy = jest.spyOn(service, 'updateOptions');
155+
const spy = jest.spyOn(service, 'updateFilters');
156156
service.clearFilters();
157-
expect(spy).toHaveBeenCalledWith({ filteringOptions: [] });
157+
expect(spy).toHaveBeenCalledWith([]);
158158
});
159159
});
160160

161161
describe('clearSorters method', () => {
162162
it('should call "updateOptions" to clear all sorting', () => {
163-
const spy = jest.spyOn(service, 'updateOptions');
163+
const spy = jest.spyOn(service, 'updateSorters');
164164
service.clearSorters();
165-
expect(spy).toHaveBeenCalledWith({ sortingOptions: [] });
165+
expect(spy).toHaveBeenCalledWith([]);
166166
});
167167
});
168168

@@ -658,6 +658,23 @@ describe('GridOdataService', () => {
658658

659659
expect(query).toBe(expectation);
660660
});
661+
662+
it('should return a query without any sorting after clearFilters was called', () => {
663+
const expectation = `$top=10`;
664+
const mockColumn = { id: 'gender', field: 'gender' } as Column;
665+
const mockColumnFilters = {
666+
gender: { columnId: 'gender', columnDef: mockColumn, searchTerms: ['female'], operator: 'EQ' },
667+
} as ColumnFilters;
668+
669+
service.init(serviceOptions, paginationOptions, gridStub);
670+
service.updateFilters(mockColumnFilters, false);
671+
service.clearFilters();
672+
const currentFilters = service.getCurrentFilters();
673+
const query = service.buildQuery();
674+
675+
expect(query).toBe(expectation);
676+
expect(currentFilters).toEqual([]);
677+
});
661678
});
662679

663680
describe('updateSorters method', () => {
@@ -744,6 +761,23 @@ describe('GridOdataService', () => {
744761
expect(query).toBe(expectation);
745762
expect(currentSorters).toEqual([{ columnId: 'Gender', direction: 'desc' }, { columnId: 'FirstName', direction: 'asc' }]);
746763
});
764+
765+
it('should return a query without any sorting after clearSorters was called', () => {
766+
const expectation = `$top=10`;
767+
const mockColumnSort = [
768+
{ columnId: 'gender', sortCol: { id: 'gender', field: 'gender' }, sortAsc: false },
769+
{ columnId: 'firstName', sortCol: { id: 'firstName', field: 'firstName' }, sortAsc: true }
770+
] as ColumnSort[];
771+
772+
service.init(serviceOptions, paginationOptions, gridStub);
773+
service.updateSorters(mockColumnSort);
774+
service.clearSorters();
775+
const query = service.buildQuery();
776+
const currentSorters = service.getCurrentSorters();
777+
778+
expect(query).toBe(expectation);
779+
expect(currentSorters).toEqual([]);
780+
});
747781
});
748782

749783
describe('mapOdataOperator method', () => {

src/app/modules/angular-slickgrid/services/grid-odata.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ export class GridOdataService implements BackendService {
9090

9191
clearFilters() {
9292
this._currentFilters = [];
93-
this.updateOptions({ filteringOptions: [] });
93+
this.updateFilters([]);
9494
}
9595

9696
clearSorters() {
9797
this._currentSorters = [];
98-
this.updateOptions({ sortingOptions: [] });
98+
this.updateSorters([]);
9999
}
100100

101101
updateOptions(serviceOptions?: OdataOption) {
@@ -218,7 +218,7 @@ export class GridOdataService implements BackendService {
218218
}
219219

220220
/**
221-
* loop through all columns to inspect filters & update backend service filteringOptions
221+
* loop through all columns to inspect filters & update backend service filters
222222
* @param columnFilters
223223
*/
224224
updateFilters(columnFilters: ColumnFilters | CurrentFilter[], isUpdatedByPreset?: boolean) {
@@ -475,7 +475,6 @@ export class GridOdataService implements BackendService {
475475

476476
// keep current Sorters and update the service options with the new sorting
477477
this._currentSorters = currentSorters;
478-
// this.updateOptions({ sortingOptions: odataSorters });
479478

480479
// build the OData query which we will use in the WebAPI callback
481480
return this._odataService.buildQuery();

src/app/modules/angular-slickgrid/services/odata.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class OdataService {
3535
if (this._odataOptions.orderBy) {
3636
let argument = '';
3737
if (Array.isArray(this._odataOptions.orderBy)) {
38-
argument = this._odataOptions.orderBy.join(','); // csv, that will form a query example like: $orderby=RoleName asc, Id desc
38+
argument = this._odataOptions.orderBy.join(','); // csv, that will form a query, for example: $orderby=RoleName asc, Id desc
3939
} else {
4040
argument = this._odataOptions.orderBy;
4141
}

0 commit comments

Comments
 (0)