Skip to content

Commit

Permalink
Merge pull request #410 from ghiscoding/bugfix/refresh-tree-data-filt…
Browse files Browse the repository at this point in the history
…er-only-when-enabled

fix(filter): refreshTreeDataFilters only when Tree is enabled
  • Loading branch information
AnnetteZhang authored Jul 14, 2021
2 parents 85f028e + 5c42996 commit 502cc0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 25 additions & 0 deletions packages/common/src/services/__tests__/filter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,31 @@ describe('FilterService', () => {
expect(preFilterSpy).toHaveBeenCalledWith(dataset, { ...columnFilters, file: { ...columnFilters.file, operator: 'Contains', parsedSearchTerms: ['unknown'], type: 'string' } }); // it will use Contains by default
expect(preFilterSpy).toHaveReturnedWith([]);
});

it('should return False also when called by "updateSingleFilter" and when item is not found in the dataset', async () => {
const pubSubSpy = jest.spyOn(pubSubServiceStub, 'publish');
const preFilterSpy = jest.spyOn(service, 'preFilterTreeData');
jest.spyOn(dataViewStub, 'getItemById').mockReturnValueOnce({ ...dataset[4] as any })
.mockReturnValueOnce(dataset[5])
.mockReturnValueOnce(dataset[6]);

const mockItem1 = { __parentId: 4, id: 5, file: 'unknown.pdf', dateModified: '2015-05-21T10:22:00.123Z', size: 3.1 };

service.init(gridStub);
service.bindLocalOnFilter(gridStub);
gridStub.onHeaderRowCellRendered.notify(mockArgs1 as any, new Slick.EventData(), gridStub);
gridStub.onHeaderRowCellRendered.notify(mockArgs2 as any, new Slick.EventData(), gridStub);

const columnFilters = { file: { columnDef: mockColumn1, columnId: 'file', searchTerms: ['unknown'], type: FieldType.string } } as ColumnFilters;
await service.updateSingleFilter({ columnId: 'file', operator: 'Contains', searchTerms: ['unknown'] }, true, true);
const output = service.customLocalFilter(mockItem1, { dataView: dataViewStub, grid: gridStub, columnFilters });

expect(pubSubSpy).toHaveBeenCalledWith(`onBeforeFilterChange`, [{ columnId: 'file', operator: 'Contains', searchTerms: ['unknown'] }]);
expect(pubSubSpy).toHaveBeenCalledWith(`onFilterChanged`, [{ columnId: 'file', operator: 'Contains', searchTerms: ['unknown'] }]);
expect(output).toBe(false);
expect(preFilterSpy).toHaveBeenCalledWith(dataset, { ...columnFilters, file: { ...columnFilters.file, operator: 'Contains', parsedSearchTerms: ['unknown'], type: 'string' } }); // it will use Contains by default
expect(preFilterSpy).toHaveReturnedWith([]);
});
});
});
});
10 changes: 7 additions & 3 deletions packages/common/src/services/filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,9 @@ export class FilterService {
});

// when we have a Filter Presets on a Tree Data View grid, we need to call the pre-filtering of tree data
this.refreshTreeDataFilters();
if (this._gridOptions?.enableTreeData) {
this.refreshTreeDataFilters();
}
}
return this._columnDefinitions;
}
Expand All @@ -728,7 +730,7 @@ export class FilterService {
* @param {Array<Object>} [items] - optional flat array of parent/child items to use while redoing the full sort & refresh
*/
refreshTreeDataFilters(items?: any[]) {
const inputItems = items ?? this._dataView.getItems() ?? [];
const inputItems = items ?? this._dataView?.getItems?.() ?? [];

if (this._dataView && this._gridOptions?.enableTreeData && inputItems.length > 0) {
this._tmpPreFilteredData = this.preFilterTreeData(inputItems, this._columnFilters);
Expand Down Expand Up @@ -917,7 +919,9 @@ export class FilterService {
});

// when using Tree Data, we also need to refresh the filters because of the tree structure with recursion
this.refreshTreeDataFilters();
if (this._gridOptions?.enableTreeData) {
this.refreshTreeDataFilters();
}

this._dataView.refresh();
}
Expand Down

0 comments on commit 502cc0e

Please sign in to comment.