Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 269f1e9

Browse files
authored
fix(helpers): should be able to highlight first row (0) (#701)
- calling `highlightRow(0)` was not highlighting the row when it should
1 parent 61e6599 commit 269f1e9

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/app/modules/angular-slickgrid/services/__tests__/grid.service.spec.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GridService, ExtensionService, FilterService, GridStateService, SortSer
44
import { CellArgs, Column, OnEventArgs, GridOption } from './../../models';
55

66
declare const Slick: any;
7+
jest.useFakeTimers();
78

89
const mockSelectionModel = {
910
init: jest.fn(),
@@ -39,6 +40,7 @@ const dataviewStub = {
3940
deleteItem: jest.fn(),
4041
deleteItems: jest.fn(),
4142
getIdxById: jest.fn(),
43+
getItemMetadata: jest.fn(),
4244
getItem: jest.fn(),
4345
getRowById: jest.fn(),
4446
insertItem: jest.fn(),
@@ -1079,23 +1081,21 @@ describe('Grid Service', () => {
10791081
});
10801082

10811083
describe('highlightRowByMetadata method', () => {
1082-
it('should hightlight a row with a fading start & end delay', (done) => {
1084+
it('should hightlight a row with a fading start & end delay', () => {
10831085
const mockColumn = { id: 'field2', field: 'field2', width: 150, rowClass: 'red' } as Column;
10841086
const getItemSpy = jest.spyOn(dataviewStub, 'getItem').mockReturnValue(mockColumn);
10851087
const getIndexSpy = jest.spyOn(dataviewStub, 'getIdxById').mockReturnValue(0);
10861088
const updateSpy = jest.spyOn(dataviewStub, 'updateItem');
10871089
const renderSpy = jest.spyOn(service, 'renderGrid');
10881090

10891091
service.highlightRowByMetadata(2, 1, 1);
1092+
jest.runAllTimers(); // fast-forward timer
10901093

1091-
setTimeout(() => {
1092-
expect(getItemSpy).toHaveBeenCalledWith(2);
1093-
expect(updateSpy).toHaveBeenCalledTimes(3);
1094-
expect(updateSpy).toHaveBeenCalledWith(mockColumn.id, mockColumn);
1095-
expect(renderSpy).toHaveBeenCalled();
1096-
expect(getIndexSpy).toHaveBeenCalled();
1097-
done();
1098-
}, 5);
1094+
expect(getItemSpy).toHaveBeenCalledWith(2);
1095+
expect(updateSpy).toHaveBeenCalledTimes(3);
1096+
expect(updateSpy).toHaveBeenCalledWith(mockColumn.id, mockColumn);
1097+
expect(renderSpy).toHaveBeenCalled();
1098+
expect(getIndexSpy).toHaveBeenCalled();
10991099
});
11001100
});
11011101

@@ -1492,6 +1492,28 @@ describe('Grid Service', () => {
14921492
});
14931493
});
14941494

1495+
describe('highlightRow method', () => {
1496+
afterEach(() => {
1497+
jest.clearAllMocks();
1498+
});
1499+
1500+
it('should be able to highlight first row at zero index', () => {
1501+
const mockRowMetadata = (rowNumber) => ({ cssClasses: `row-${rowNumber}` });
1502+
const mockItem = { id: 0, firstName: 'John', lastName: 'Doe' };
1503+
jest.spyOn(service, 'getItemRowMetadataToHighlight').mockReturnValue(mockRowMetadata);
1504+
jest.spyOn(dataviewStub, 'getItem').mockReturnValue(mockItem);
1505+
jest.spyOn(dataviewStub, 'getIdxById').mockReturnValue(0);
1506+
const updateSpy = jest.spyOn(dataviewStub, 'updateItem');
1507+
const renderSpy = jest.spyOn(service, 'renderGrid');
1508+
1509+
service.highlightRow(0, 10, 15);
1510+
jest.runAllTimers(); // fast-forward timer
1511+
1512+
expect(updateSpy).toHaveBeenCalledWith(0, mockItem)
1513+
expect(renderSpy).toHaveBeenCalledTimes(3);
1514+
});
1515+
});
1516+
14951517
// --
14961518
// DEPRECATED methods, to be removed eventually
14971519
// ----------------------

src/app/modules/angular-slickgrid/services/grid.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export class GridService {
266266
const item = this._dataView.getItem(rowNumber);
267267
const idPropName = this._gridOptions.datasetIdPropertyName || 'id';
268268

269-
if (item && item[idPropName]) {
269+
if (item && item[idPropName] !== undefined) {
270270
item.rowClass = 'highlight';
271271
this._dataView.updateItem(item[idPropName], item);
272272
this.renderGrid();
@@ -281,7 +281,7 @@ export class GridService {
281281

282282
// delete the row's CSS highlight classes once the delay is passed
283283
setTimeout(() => {
284-
if (item && item[idPropName]) {
284+
if (item && item[idPropName] !== undefined) {
285285
delete item.rowClass;
286286
if (this._dataView.getIdxById(item[idPropName]) !== undefined) {
287287
this._dataView.updateItem(item[idPropName], item);

0 commit comments

Comments
 (0)