Skip to content

Commit

Permalink
fix(frozen): calling setPinning with empty object/null should clear it
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Oct 15, 2021
1 parent ed6bc7c commit 48b11f7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/common/src/services/__tests__/grid.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,26 @@ describe('Grid Service', () => {
expect(setOptionsSpy).toHaveBeenCalledWith({ frozenBottom: false, frozenColumn: -1, frozenRow: -1, enableMouseWheelScrollHandler: false });
});

it('should call "setPinning" which itself calls "clearPinning" when the pinning option input is an empty object', () => {
const mockPinning = {};
const clearPinningSpy = jest.spyOn(service, 'clearPinning');
jest.spyOn(SharedService.prototype, 'slickGrid', 'get').mockReturnValue(gridStub);

service.setPinning(mockPinning);

expect(clearPinningSpy).toHaveBeenCalled();
});

it('should call "setPinning" which itself calls "clearPinning" when the pinning option input is null', () => {
const mockPinning = null;
const clearPinningSpy = jest.spyOn(service, 'clearPinning');
jest.spyOn(SharedService.prototype, 'slickGrid', 'get').mockReturnValue(gridStub);

service.setPinning(mockPinning);

expect(clearPinningSpy).toHaveBeenCalled();
});

it('should call "setPinning" and expect SlickGrid "setOptions" be called with new frozen options and "autosizeColumns" also be called', () => {
const mockPinning = { frozenBottom: true, frozenColumn: 1, frozenRow: 2 };
jest.spyOn(SharedService.prototype, 'slickGrid', 'get').mockReturnValue(gridStub);
Expand Down
23 changes: 23 additions & 0 deletions packages/common/src/services/__tests__/utilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
htmlEncode,
htmlEntityDecode,
isNumber,
isObjectEmpty,
mapMomentDateFormatWithFieldType,
mapFlatpickrDateFormatWithFieldType,
mapOperatorByFieldType,
Expand Down Expand Up @@ -130,6 +131,28 @@ describe('Service/Utilies', () => {
});
});

describe('isObjectEmpty method', () => {
it('should return True when input is undefined', () => {
const result = isObjectEmpty(undefined);
expect(result).toBeTrue();
});

it('should return True when input is null', () => {
const result = isObjectEmpty(null);
expect(result).toBeTrue();
});

it('should return True when input is {} (empty object)', () => {
const result = isObjectEmpty({});
expect(result).toBeTrue();
});

it('should return False when input is an object with at least 1 property', () => {
const result = isObjectEmpty({ name: 'John' });
expect(result).toBeFalse();
});
});

describe('isNumber method', () => {
it('should return True when comparing a number from a number/string variable when strict mode is disable', () => {
const result1 = isNumber(22);
Expand Down
10 changes: 7 additions & 3 deletions packages/common/src/services/grid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
SlickNamespace,
SlickRowSelectionModel,
} from '../interfaces/index';
import { arrayRemoveItemByIndex } from './utilities';
import { arrayRemoveItemByIndex, isObjectEmpty } from './utilities';
import { FilterService } from './filter.service';
import { GridStateService } from './gridState.service';
import { PaginationService } from '../services/pagination.service';
Expand Down Expand Up @@ -96,8 +96,12 @@ export class GridService {
* @param {Boolean} suppressColumnSet - do we want to supress the columns set, via "setColumns()" method? (defaults to false)
*/
setPinning(pinningOptions: CurrentPinning, shouldAutosizeColumns = true, suppressRender = false, suppressColumnSet = true) {
this.sharedService.slickGrid.setOptions(pinningOptions, suppressRender, suppressColumnSet);
this.sharedService.gridOptions = { ...this.sharedService.gridOptions, ...pinningOptions };
if (isObjectEmpty(pinningOptions)) {
this.clearPinning();
} else {
this.sharedService.slickGrid.setOptions(pinningOptions, suppressRender, suppressColumnSet);
this.sharedService.gridOptions = { ...this.sharedService.gridOptions, ...pinningOptions };
}

if (shouldAutosizeColumns) {
this.sharedService.slickGrid.autosizeColumns();
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ export function isNumber(value: any, strict = false) {
return (value === null || value === undefined || value === '') ? false : !isNaN(+value);
}

/** Check if an object is empty, it will also be considered empty when the input is null, undefined or isn't an object */
export function isObjectEmpty(obj: unknown) {
return !obj || (obj && typeof obj === 'object' && Object.keys(obj).length === 0);
}

/**
* Take a number (or a string) and display it as a formatted decimal string with defined minimum and maximum decimals
* @param input
Expand Down
Binary file not shown.

0 comments on commit 48b11f7

Please sign in to comment.