Skip to content

Commit 399f770

Browse files
committed
fix(tree): calling updateItems should not lose the Tree collapsing icon
- calling an update of an row item that is a parent item (with collapse/expand icon) should keep their icon even after an update, it was losing the icon because the Tree structure gets lost and we need to recreate it
1 parent 5789370 commit 399f770

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

+46
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,52 @@ describe('Grid Service', () => {
518518
// reset mock
519519
jest.spyOn(gridStub, 'getOptions').mockReturnValue({});
520520
});
521+
522+
it('should invalidate and rerender the tree dataset when grid option "enableTreeData" is set when calling "updateItem"', () => {
523+
const mockUpdatedItem = { id: 1, file: 'vacation.txt', size: 2.2, parentId: 0 };
524+
const mockFlatDataset = [{ id: 0, file: 'documents' }, { id: 1, file: 'vacation.txt', parentId: 0 }, mockUpdatedItem];
525+
const mockHierarchical = [{ id: 0, file: 'documents', files: [{ id: 1, file: 'vacation.txt' }, mockUpdatedItem] }];
526+
const mockColumns = [{ id: 'file', field: 'file', }, { id: 'size', field: 'size', }] as Column[];
527+
528+
jest.spyOn(dataviewStub, 'getItems').mockReturnValue(mockFlatDataset);
529+
jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0);
530+
jest.spyOn(treeDataServiceStub, 'convertFlatParentChildToTreeDatasetAndSort').mockReturnValue({ flat: mockFlatDataset as any[], hierarchical: mockHierarchical as any[] });
531+
jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true, enableRowSelection: true, enableTreeData: true } as GridOption);
532+
jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(mockColumns);
533+
const setItemSpy = jest.spyOn(dataviewStub, 'setItems');
534+
const updateSpy = jest.spyOn(dataviewStub, 'updateItem');
535+
const invalidateSpy = jest.spyOn(service, 'invalidateHierarchicalDataset');
536+
537+
service.updateItem(mockUpdatedItem);
538+
539+
expect(updateSpy).toHaveBeenCalledTimes(1);
540+
expect(updateSpy).toHaveBeenCalledWith(mockUpdatedItem.id, mockUpdatedItem);
541+
expect(invalidateSpy).toHaveBeenCalled();
542+
expect(setItemSpy).toHaveBeenCalledWith(mockFlatDataset);
543+
});
544+
545+
it('should invalidate and rerender the tree dataset when grid option "enableTreeData" is set when calling "updateItems"', () => {
546+
const mockUpdatedItem = { id: 1, file: 'vacation.txt', size: 2.2, parentId: 0 };
547+
const mockFlatDataset = [{ id: 0, file: 'documents' }, { id: 1, file: 'vacation.txt', parentId: 0 }, mockUpdatedItem];
548+
const mockHierarchical = [{ id: 0, file: 'documents', files: [{ id: 1, file: 'vacation.txt' }, mockUpdatedItem] }];
549+
const mockColumns = [{ id: 'file', field: 'file', }, { id: 'size', field: 'size', }] as Column[];
550+
551+
jest.spyOn(dataviewStub, 'getItems').mockReturnValue(mockFlatDataset);
552+
jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0);
553+
jest.spyOn(treeDataServiceStub, 'convertFlatParentChildToTreeDatasetAndSort').mockReturnValue({ flat: mockFlatDataset as any[], hierarchical: mockHierarchical as any[] });
554+
jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true, enableRowSelection: true, enableTreeData: true } as GridOption);
555+
jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(mockColumns);
556+
const setItemSpy = jest.spyOn(dataviewStub, 'setItems');
557+
const updateSpy = jest.spyOn(dataviewStub, 'updateItems');
558+
const invalidateSpy = jest.spyOn(service, 'invalidateHierarchicalDataset');
559+
560+
service.updateItems([mockUpdatedItem]);
561+
562+
expect(updateSpy).toHaveBeenCalledTimes(1);
563+
expect(updateSpy).toHaveBeenCalledWith([mockUpdatedItem.id], [mockUpdatedItem]);
564+
expect(invalidateSpy).toHaveBeenCalled();
565+
expect(setItemSpy).toHaveBeenCalledWith(mockFlatDataset);
566+
});
521567
});
522568

523569
describe('addItem methods', () => {

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

+10
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ export class GridService {
777777
// end the bulk transaction since we're all done
778778
this._dataView.endUpdate();
779779

780+
if (this._gridOptions?.enableTreeData) {
781+
// if we add/remove item(s) from the dataset, we need to also refresh our tree data filters
782+
this.invalidateHierarchicalDataset();
783+
}
784+
780785
// only highlight at the end, all at once
781786
// we have to do this because doing highlight 1 by 1 would only re-select the last highlighted row which is wrong behavior
782787
if (options.highlightRow) {
@@ -819,6 +824,11 @@ export class GridService {
819824
this._dataView.updateItem(itemId, item);
820825
this._grid.updateRow(rowNumber);
821826

827+
if (this._gridOptions?.enableTreeData) {
828+
// if we add/remove item(s) from the dataset, we need to also refresh our tree data filters
829+
this.invalidateHierarchicalDataset();
830+
}
831+
822832
// do we want to scroll to the row so that it shows in the Viewport (UI)
823833
if (options.scrollRowIntoView) {
824834
this._grid.scrollRowIntoView(rowNumber);

0 commit comments

Comments
 (0)