Skip to content

Commit

Permalink
fix: Tree Data should work without initial sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Aug 17, 2024
1 parent f7829d0 commit 5afdd5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2247,11 +2247,11 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
expect(treeConvertAndSortSpy).toHaveBeenCalled();
});

it('should change flat dataset and expect "convertFlatParentChildToTreeDataset" being called (without sorting) and other methods as well', () => {
it('should change flat dataset and expect "convertFlatParentChildToTreeDatasetAndSort" being called even without an initial sort defined', () => {
const mockFlatDataset = [{ id: 0, file: 'documents' }, { id: 1, file: 'vacation.txt', parentId: 0 }];
const mockHierarchical = [{ id: 0, file: 'documents', files: [{ id: 1, file: 'vacation.txt' }] }];
const hierarchicalSpy = jest.spyOn(SharedService.prototype, 'hierarchicalDataset', 'set');
const treeConvertSpy = jest.spyOn(treeDataServiceStub, 'convertFlatParentChildToTreeDataset').mockReturnValue(mockHierarchical as any[]);
const treeConvertAndSortSpy = jest.spyOn(treeDataServiceStub, 'convertFlatParentChildToTreeDatasetAndSort').mockReturnValue({ hierarchical: mockHierarchical as any[], flat: mockFlatDataset as any[] });
const refreshTreeSpy = jest.spyOn(filterServiceStub, 'refreshTreeDataFilters');

component.gridOptions = {
Expand All @@ -2265,7 +2265,7 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =

expect(hierarchicalSpy).toHaveBeenCalledWith(mockHierarchical);
expect(refreshTreeSpy).toHaveBeenCalled();
expect(treeConvertSpy).toHaveBeenCalled();
expect(treeConvertAndSortSpy).toHaveBeenCalled();
});

it('should change hierarchical dataset and expect processTreeDataInitialSort being called with other methods', (done) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1454,17 +1454,11 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
sortedDatasetResult = this.treeDataService.sortHierarchicalDataset(this.datasetHierarchical);
flatDatasetOutput = sortedDatasetResult.flat;
} else if (Array.isArray(flatDatasetInput) && flatDatasetInput.length > 0) {
if (this.gridOptions?.treeDataOptions?.initialSort) {
// else we need to first convert the flat dataset to a hierarchical dataset and then sort
sortedDatasetResult = this.treeDataService.convertFlatParentChildToTreeDatasetAndSort(flatDatasetInput, this._columnDefinitions, this.gridOptions);
this.sharedService.hierarchicalDataset = sortedDatasetResult.hierarchical;
flatDatasetOutput = sortedDatasetResult.flat;
} else {
// else we assume that the user provided an array that is already sorted (user's responsability)
// and so we can simply convert the array to a tree structure and we're done, no need to sort
this.sharedService.hierarchicalDataset = this.treeDataService.convertFlatParentChildToTreeDataset(flatDatasetInput, this.gridOptions);
flatDatasetOutput = flatDatasetInput || [];
}
// we need to first convert the flat dataset to a hierarchical dataset and then sort it
// we'll also add props, by mutation, required by the TreeDataService on the flat array like `__hasChildren`, `parentId` and anything else to work properly
sortedDatasetResult = this.treeDataService.convertFlatParentChildToTreeDatasetAndSort(flatDatasetInput, this._columnDefinitions, this.gridOptions);
this.sharedService.hierarchicalDataset = sortedDatasetResult.hierarchical;
flatDatasetOutput = sortedDatasetResult.flat;
}

// if we add/remove item(s) from the dataset, we need to also refresh our tree data filters
Expand Down

0 comments on commit 5afdd5b

Please sign in to comment.