From 3eee3f74b9a88d243b420984579b386daab3f4e3 Mon Sep 17 00:00:00 2001 From: Ghislain Beaulac Date: Tue, 21 May 2019 09:56:55 -0400 Subject: [PATCH 1/2] fix(selection): syncGridSelection and preserveHidden, fixes #191 --- .../components/angular-slickgrid.component.ts | 11 ++++++++--- .../angular-slickgrid/models/gridOption.interface.ts | 7 ++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts index 831574828..f7ff3d538 100644 --- a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts +++ b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts @@ -281,10 +281,15 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn this.dataView.setItems(this._dataset, this.gridOptions.datasetIdPropertyName); this.dataView.endUpdate(); - // if you don't want the items that are not visible (due to being filtered out - // or being on a different page) to stay selected, pass 'false' to the second arg + // if you don't want the items that are not visible (due to being filtered out or being on a different page) + // to stay selected, pass 'false' to the second arg if (this.gridOptions && this.gridOptions.dataView && this.gridOptions.dataView.hasOwnProperty('syncGridSelection')) { - this.dataView.syncGridSelection(this.grid, this.gridOptions.dataView.syncGridSelection); + const syncGridSelection = this.gridOptions.dataView.syncGridSelection; + if (typeof syncGridSelection === 'boolean') { + this.dataView.syncGridSelection(this.grid, this.gridOptions.dataView.syncGridSelection); + } else { + this.dataView.syncGridSelection(this.grid, syncGridSelection.preserveHidden, syncGridSelection.preserveHiddenOnSelectionChange); + } } } diff --git a/src/app/modules/angular-slickgrid/models/gridOption.interface.ts b/src/app/modules/angular-slickgrid/models/gridOption.interface.ts index d8b7d7b05..eafda4913 100644 --- a/src/app/modules/angular-slickgrid/models/gridOption.interface.ts +++ b/src/app/modules/angular-slickgrid/models/gridOption.interface.ts @@ -97,12 +97,13 @@ export interface GridOption { /** Unique property name on the dataset used by Slick.Data.DataView */ datasetIdPropertyName?: string; + /** Some of the SlickGrid DataView options */ dataView?: { /** - * if you don't want the items that are not visible (due to being filtered out - * or being on a different page) to stay selected, the set this property as 'false' + * If you don't want the items that are not visible (due to being filtered out or being on a different page) + * to stay selected, the set this property as 'false'. You can also set any of the preserve options instead of a boolean value. */ - syncGridSelection?: boolean; + syncGridSelection?: boolean | { preserveHidden: boolean; preserveHiddenOnSelectionChange: boolean; }; }; /** Default column width, is set to 80 when null */ From a753471f932b80450c110769fa187e4d419b08df Mon Sep 17 00:00:00 2001 From: Ghislain Beaulac Date: Wed, 22 May 2019 16:50:02 -0400 Subject: [PATCH 2/2] feat(tests): add tests for the dataView syncGridSelection option --- .../angular-slickgrid.component.spec.ts | 76 ++++++++++++++++++- .../components/angular-slickgrid.component.ts | 2 +- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts index 346517eef..a63991f6d 100644 --- a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts +++ b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.spec.ts @@ -26,15 +26,38 @@ import { CellExternalCopyManagerExtension, CheckboxSelectorExtension, ColumnPickerExtension, + CurrentSorter, DraggableGroupingExtension, GridMenuExtension, + GridOption, GroupItemMetaProviderExtension, HeaderButtonExtension, HeaderMenuExtension, RowDetailViewExtension, RowMoveManagerExtension, - RowSelectionExtension + RowSelectionExtension, } from '..'; +import { Subject } from 'rxjs'; + +const sharedServiceStub = {} as SharedService; +Object.defineProperty(sharedServiceStub, 'dataView', { + get: jest.fn(() => 'bar'), + set: jest.fn() +}); + +const sortServiceStub = { + attachLocalOnSort: jest.fn(), + dispose: jest.fn(), + loadLocalPresets: jest.fn(), + onSortChanged: new Subject(), + onSortCleared: new Subject() +}; + +// jest.mock('slickgrid/slick.dataview', () => ({ +// Slick: { +// Data: mockDataView +// } +// })); describe('App Component', () => { let fixture: ComponentFixture; @@ -60,7 +83,6 @@ describe('App Component', () => { GroupingAndColspanService, ResizerService, SharedService, - SortService, TranslateService, AutoTooltipExtension, CellExternalCopyManagerExtension, @@ -75,6 +97,7 @@ describe('App Component', () => { RowMoveManagerExtension, RowSelectionExtension, SlickgridConfig, + { provide: SortService, useValue: sortServiceStub }, ], imports: [ RouterTestingModule, @@ -114,4 +137,53 @@ describe('App Component', () => { expect(() => fixture.detectChanges()).toThrowError('[Angular-Slickgrid] requires a "grid-height" or the "enableAutoResize"'); }); + + describe('dataView options', () => { + let dataView; + let dataViewSpy; + + it('should call the onDataviewCreated emitter', () => { + const spy = jest.spyOn(component.onDataviewCreated, 'emit'); + fixture.detectChanges(); + expect(spy).toHaveBeenCalled(); + }); + + it('should call the "executeAfterDataviewCreated" and "loadLocalPresets" methods and Sorter Presets are provided in the Grid Options', () => { + const compSpy = jest.spyOn(component, 'executeAfterDataviewCreated'); + const sortSpy = jest.spyOn(sortServiceStub, 'loadLocalPresets'); + + component.gridOptions = { presets: { sorters: [{ columnId: 'field1', direction: 'DESC' }] } } as GridOption; + fixture.detectChanges(); + + expect(compSpy).toHaveBeenCalled(); + expect(sortSpy).toHaveBeenCalled(); + }); + + it('should call the DataView syncGridSelection method with 2nd argument as True when the "dataView" grid option is a boolean and is set to True', () => { + component.onDataviewCreated.subscribe((internalDataView) => { + dataView = internalDataView; + dataViewSpy = jest.spyOn(internalDataView, 'syncGridSelection'); + }); + component.gridOptions = { dataView: { syncGridSelection: true }, enableRowSelection: true } as GridOption; + fixture.detectChanges(); + + expect(dataView).toBeTruthy(); + expect(dataViewSpy).toHaveBeenCalledWith(component.grid, true); + }); + + it('should call the DataView syncGridSelection method with 3 arguments when the "dataView" grid option is provided as an object', () => { + component.onDataviewCreated.subscribe((internalDataView) => { + dataView = internalDataView; + dataViewSpy = jest.spyOn(internalDataView, 'syncGridSelection'); + }); + component.gridOptions = { + dataView: { syncGridSelection: { preserveHidden: true, preserveHiddenOnSelectionChange: false } }, + enableRowSelection: true + } as GridOption; + fixture.detectChanges(); + + expect(dataView).toBeTruthy(); + expect(dataViewSpy).toHaveBeenCalledWith(component.grid, true, false); + }); + }); }); diff --git a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts index f7ff3d538..628abac42 100644 --- a/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts +++ b/src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts @@ -189,7 +189,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn destroy(emptyDomElementContainer = false) { const gridContainerId = this.gridOptions && this.gridOptions.gridContainerId; - this.dataView = []; + this.dataView = undefined; this.gridOptions = {}; this.extensionService.dispose(); this.filterService.dispose();