Skip to content

Commit

Permalink
fix(gridService): addItem/updatedItemById pass array to setSelectedRo…
Browse files Browse the repository at this point in the history
…ws (#234)
  • Loading branch information
ghiscoding committed Oct 12, 2019
1 parent 017f93d commit 96d04cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
39 changes: 37 additions & 2 deletions src/aurelia-slickgrid/services/__tests__/grid.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'jest-extended';
import { EventAggregator } from 'aurelia-event-aggregator';
import { GridService, ExtensionService, FilterService, GridStateService, SortService } from '..';
import { GridOption, CellArgs, Column, OnEventArgs } from '../..';

import { GridService, ExtensionService, FilterService, GridStateService, SortService } from '../index';
import { GridOption, CellArgs, Column, OnEventArgs } from '../../models';

declare var Slick: any;

Expand Down Expand Up @@ -268,6 +270,21 @@ describe('Grid Service', () => {
expect(eaSpy).toHaveBeenLastCalledWith(`asg:on-item-updated`, mockItem);
});

it('should expect the service to call the "updateItemById" when calling "updateItem" and setting the "selecRow" flag and the grid option "enableRowSelection" is set', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const getRowIdSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(mockItem.id);
const getRowIndexSpy = jest.spyOn(dataviewStub, 'getIdxById').mockReturnValue(mockItem.id);
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
const eaSpy = jest.spyOn(ea, 'publish');

service.updateItemById(0, mockItem, { selectRow: true });

expect(getRowIdSpy).toHaveBeenCalledWith(0);
expect(getRowIndexSpy).toHaveBeenCalledWith(0);
expect(serviceHighlightSpy).toHaveBeenCalledWith(0);
expect(eaSpy).toHaveBeenLastCalledWith(`asg:on-item-updated`, mockItem);
});

it('should expect the service to call the "updateItemById" multiple times when calling "updateItems" with an array of items', () => {
const mockItems = [{ id: 0, user: { firstName: 'John', lastName: 'Doe' } }, { id: 5, user: { firstName: 'Jane', lastName: 'Doe' } }];
const getRowIdSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0).mockReturnValueOnce(0).mockReturnValueOnce(1);
Expand Down Expand Up @@ -373,17 +390,35 @@ describe('Grid Service', () => {
it('should expect the service to call the DataView "insertItem" when calling "addItem" with an item', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const addSpy = jest.spyOn(dataviewStub, 'insertItem');
const selectSpy = jest.spyOn(gridStub, 'setSelectedRows');
const scrollSpy = jest.spyOn(gridStub, 'scrollRowIntoView');
const eaSpy = jest.spyOn(ea, 'publish');

service.addItem(mockItem);

expect(addSpy).toHaveBeenCalledTimes(1);
expect(addSpy).toHaveBeenCalledWith(0, mockItem);
expect(selectSpy).not.toHaveBeenCalled();
expect(scrollSpy).toHaveBeenCalledWith(0);
expect(eaSpy).toHaveBeenLastCalledWith(`asg:on-item-added`, mockItem);
});

it('should expect the row to be selected when calling "addItem" with an item when setting the "selecRow" flag and the grid option "enableRowSelection" is set', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0);
jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true, enableRowSelection: true } as GridOption);
const addSpy = jest.spyOn(dataviewStub, 'insertItem');
const selectSpy = jest.spyOn(gridStub, 'setSelectedRows');
const eaSpy = jest.spyOn(ea, 'publish');

service.addItem(mockItem, { selectRow: true });

expect(addSpy).toHaveBeenCalledTimes(1);
expect(addSpy).toHaveBeenCalledWith(0, mockItem);
expect(selectSpy).toHaveBeenCalledWith([0]);
expect(eaSpy).toHaveBeenLastCalledWith(`asg:on-item-added`, mockItem);
});

it('should expect the service to call the DataView "addItem" when calling "addItem" with an item and the option "position" set to "bottom"', () => {
const expectationNewRowPosition = 1000;
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
Expand Down
4 changes: 2 additions & 2 deletions src/aurelia-slickgrid/services/grid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class GridService {

// if row selection (checkbox selector) is enabled, we'll select the row in the grid
if (options.selectRow && this._gridOptions && (this._gridOptions.enableCheckboxSelector || this._gridOptions.enableRowSelection)) {
this._grid.setSelectedRows(rowNumber);
this._grid.setSelectedRows([rowNumber]);
}

// do we want to trigger an event after adding the item
Expand Down Expand Up @@ -601,7 +601,7 @@ export class GridService {

// select the row in the grid
if (options.selectRow && this._gridOptions && (this._gridOptions.enableCheckboxSelector || this._gridOptions.enableRowSelection)) {
this._grid.setSelectedRows(rowNumber);
this._grid.setSelectedRows([rowNumber]);
}

// do we want to trigger an event after updating the item
Expand Down

0 comments on commit 96d04cc

Please sign in to comment.