Skip to content

Commit

Permalink
fix(helpers): should be able to highlight first row (0) (#268)
Browse files Browse the repository at this point in the history
- calling `highlightRow(0)` was not highlighting the row when it should
- fixes Aurelia-Slickgrid [issue #527](ghiscoding/aurelia-slickgrid#527)
  • Loading branch information
ghiscoding committed Feb 23, 2021
1 parent 20564a3 commit a58be17
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 23 additions & 0 deletions packages/common/src/services/__tests__/grid.service.spec.ts
Expand Up @@ -49,6 +49,7 @@ const dataviewStub = {
deleteItem: jest.fn(),
deleteItems: jest.fn(),
getIdxById: jest.fn(),
getItemMetadata: jest.fn(),
getItem: jest.fn(),
getRowById: jest.fn(),
insertItem: jest.fn(),
Expand Down Expand Up @@ -1508,4 +1509,26 @@ describe('Grid Service', () => {
expect(gridStateSpy).toHaveBeenCalledWith(mockColumns);
});
});

describe('highlightRow method', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should be able to highlight first row at zero index', () => {
const mockRowMetadata = (rowNumber) => ({ cssClasses: `row-${rowNumber}` });
const mockItem = { id: 0, firstName: 'John', lastName: 'Doe' };
jest.spyOn(service, 'getItemRowMetadataToHighlight').mockReturnValue(mockRowMetadata);
jest.spyOn(dataviewStub, 'getItem').mockReturnValue(mockItem);
jest.spyOn(dataviewStub, 'getIdxById').mockReturnValue(0);
const updateSpy = jest.spyOn(dataviewStub, 'updateItem');
const renderSpy = jest.spyOn(service, 'renderGrid');

service.highlightRow(0, 10, 15);
jest.runAllTimers(); // fast-forward timer

expect(updateSpy).toHaveBeenCalledWith(0, mockItem)
expect(renderSpy).toHaveBeenCalledTimes(3);
});
});
});
8 changes: 4 additions & 4 deletions packages/common/src/services/grid.service.ts
Expand Up @@ -312,7 +312,7 @@ export class GridService {
const item = this._dataView.getItem(rowNumber);
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';

if (item && item[idPropName]) {
if (item?.[idPropName] !== undefined) {
item.rowClass = 'highlight';
this._dataView.updateItem(item[idPropName], item);
this.renderGrid();
Expand All @@ -327,7 +327,7 @@ export class GridService {

// delete the row's CSS highlight classes once the delay is passed
setTimeout(() => {
if (item && item[idPropName]) {
if (item?.[idPropName] !== undefined) {
delete item.rowClass;
if (this._dataView.getIdxById(item[idPropName]) !== undefined) {
this._dataView.updateItem(item[idPropName], item);
Expand Down Expand Up @@ -416,7 +416,7 @@ export class GridService {

// row number in the grid, by default it will be on first row (top is the default)
let rowNumber: number | undefined = 0;
const itemId = item && item[idPropName] || '';
const itemId = item?.[idPropName] ?? '';

// do we want the item to be sorted in the grid, when set to False it will insert on first row (defaults to false)
if (options.resortGrid) {
Expand Down Expand Up @@ -552,7 +552,7 @@ export class GridService {

const itemIds: string[] = [];
items.forEach((item: T) => {
if (item && item[idPropName] !== undefined) {
if (item?.[idPropName] !== undefined) {
itemIds.push(item[idPropName]);
}
});
Expand Down

0 comments on commit a58be17

Please sign in to comment.