import { RouterTestingModule } from '@angular/router/testing';
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
+import * as _ from 'lodash';
import { configureTestBed } from '../../../../testing/unit-test-helper';
import { ComponentsModule } from '../../components/components.module';
component.reloadData();
});
+ it('should update selection on refresh - "onChange"', () => {
+ spyOn(component, 'onSelect').and.callThrough();
+ component.data = createFakeData(10);
+ component.selection.selected = [_.clone(component.data[1])];
+ component.updateSelectionOnRefresh = 'onChange';
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalledTimes(0);
+ component.data[1].d = !component.data[1].d;
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalled();
+ });
+
+ it('should update selection on refresh - "always"', () => {
+ spyOn(component, 'onSelect').and.callThrough();
+ component.data = createFakeData(10);
+ component.selection.selected = [_.clone(component.data[1])];
+ component.updateSelectionOnRefresh = 'always';
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalled();
+ component.data[1].d = !component.data[1].d;
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalled();
+ });
+
+ it('should update selection on refresh - "never"', () => {
+ spyOn(component, 'onSelect').and.callThrough();
+ component.data = createFakeData(10);
+ component.selection.selected = [_.clone(component.data[1])];
+ component.updateSelectionOnRefresh = 'never';
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalledTimes(0);
+ component.data[1].d = !component.data[1].d;
+ component.updateSelected();
+ expect(component.onSelect).toHaveBeenCalledTimes(0);
+ });
+
afterEach(() => {
clearLocalStorage();
});
// e.g. 'single' or 'multi'.
@Input()
selectionType: string = undefined;
- // If `true` selected item details will be updated on table refresh
+ // By default selected item details will be updated on table refresh, if data has changed
@Input()
- updateSelectionOnRefresh = true;
+ updateSelectionOnRefresh: 'always' | 'never' | 'onChange' = 'onChange';
@Input()
autoSave = true;
this.updateFilter(true);
}
this.reset();
- if (this.updateSelectionOnRefresh) {
- this.updateSelected();
- }
+ this.updateSelected();
}
/**
* or some selected items may have been removed.
*/
updateSelected() {
+ if (this.updateSelectionOnRefresh === 'never') {
+ return;
+ }
const newSelected = [];
this.selection.selected.forEach((selectedItem) => {
for (const row of this.data) {
}
}
});
+ if (
+ this.updateSelectionOnRefresh === 'onChange' &&
+ _.isEqual(this.selection.selected, newSelected)
+ ) {
+ return;
+ }
this.selection.selected = newSelected;
this.onSelect();
}