Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-slickgrid",
"version": "2.6.0",
"version": "2.7.0",
"description": "Slickgrid components made available in Angular",
"keywords": [
"angular",
Expand Down Expand Up @@ -91,21 +91,21 @@
"devDependencies": {
"@angular-builders/jest": "^7.4.2",
"@angular-devkit/build-angular": "~0.13.1",
"@angular/animations": "^7.2.8",
"@angular/animations": "^7.2.15",
"@angular/cli": "^7.3.1",
"@angular/common": "^7.2.8",
"@angular/compiler": "7.2.8",
"@angular/compiler-cli": "7.2.8",
"@angular/core": "^7.2.8",
"@angular/forms": "^7.2.8",
"@angular/http": "^7.2.8",
"@angular/language-service": "^7.2.8",
"@angular/platform-browser": "^7.2.8",
"@angular/platform-browser-dynamic": "^7.2.8",
"@angular/router": "^7.2.8",
"@angular/common": "^7.2.15",
"@angular/compiler": "7.2.15",
"@angular/compiler-cli": "7.2.15",
"@angular/core": "^7.2.15",
"@angular/forms": "^7.2.15",
"@angular/http": "^7.2.15",
"@angular/language-service": "^7.2.15",
"@angular/platform-browser": "^7.2.15",
"@angular/platform-browser-dynamic": "^7.2.15",
"@angular/router": "^7.2.15",
"@ng-select/ng-select": "^2.15.3",
"@types/flatpickr": "^3.1.2",
"@types/jest": "^24.0.12",
"@types/jest": "^24.0.13",
"@types/jquery": "^3.3.29",
"@types/moment": "^2.13.0",
"@types/node": "^10.12.15",
Expand All @@ -116,11 +116,11 @@
"codelyzer": "~4.5.0",
"copyfiles": "^2.1.0",
"cross-env": "^5.2.0",
"custom-event-polyfill": "^1.0.6",
"custom-event-polyfill": "^1.0.7",
"cypress": "^3.2.0",
"del": "^3.0.0",
"del-cli": "^1.1.0",
"gulp": "^4.0.0",
"gulp": "^4.0.2",
"gulp-bump": "^3.1.3",
"gulp-sass": "^4.0.2",
"gulp-yuidoc": "^0.1.2",
Expand All @@ -131,12 +131,12 @@
"mochawesome": "^3.1.2",
"mochawesome-merge": "^1.0.7",
"mochawesome-report-generator": "^3.1.5",
"ng-packagr": "^4.7.0",
"node-sass": "^4.11.0",
"ng-packagr": "^5.2.0",
"node-sass": "^4.12.0",
"npm-run-all": "^4.1.5",
"postcss-cli": "^6.0.1",
"require-dir": "^1.2.0",
"rimraf": "^2.6.2",
"rimraf": "^2.6.3",
"run-sequence": "^2.2.1",
"ts-node": "~3.3.0",
"tsickle": "^0.34.0",
Expand All @@ -146,4 +146,4 @@
"yargs": "^12.0.5",
"zone.js": "^0.8.29"
}
}
}
6 changes: 3 additions & 3 deletions src/app/examples/grid-additem.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,18 @@ export class GridAddItemComponent implements OnInit {
updateSecondItem() {
const updatedItem = this.angularGrid.gridService.getDataItemByRowNumber(1);
updatedItem.duration = Math.round(Math.random() * 100);
this.angularGrid.gridService.updateDataGridItem(updatedItem);
this.angularGrid.gridService.updateItem(updatedItem);

// OR by id
// this.angularGrid.gridService.updateDataGridItemById(updatedItem.id, updatedItem);
// this.angularGrid.gridService.updateItemById(updatedItem.id, updatedItem);

// OR multiple changes
/*
const updatedItem1 = this.angularGrid.gridService.getDataItemByRowNumber(1);
const updatedItem2 = this.angularGrid.gridService.getDataItemByRowNumber(2);
updatedItem1.duration = Math.round(Math.random() * 100);
updatedItem2.duration = Math.round(Math.random() * 100);
this.angularGrid.gridService.updateDataGridItems([updatedItem1, updatedItem2], true);
this.angularGrid.gridService.updateItems([updatedItem1, updatedItem2], true);
*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { TestBed } from '@angular/core/testing';
import { TranslateService, TranslateModule } from '@ngx-translate/core';
import { GridService, ExtensionService, FilterService, GridStateService, SortService } from '..';
import { GridOption } from '../..';

declare var Slick: any;
const HIGHLIGHT_TIMEOUT = 1500;

const mockSelectionModel = jest.fn().mockImplementation(() => ({
init: jest.fn(),
destroy: jest.fn()
}));

jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
Slick.RowSelectionModel = mockSelectionModel;

let extensionServiceStub = {
} as ExtensionService;

let filterServiceStub = {
} as FilterService;

let gridStateServiceStub = {
} as GridStateService;

let sortServiceStub = {
} as SortService;

const dataviewStub = {
getIdxById: jest.fn(),
getItem: jest.fn(),
getRowById: jest.fn(),
insertItem: jest.fn(),
reSort: jest.fn(),
};

const gridStub = {
getOptions: jest.fn(),
getColumns: jest.fn(),
getSelectionModel: jest.fn(),
setSelectionModel: jest.fn(),
setSelectedRows: jest.fn(),
scrollRowIntoView: jest.fn(),
};

describe('Grid Service', () => {
let service: GridService;
let translate: TranslateService;
const gridSpy = jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true } as GridOption);

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ExtensionService, useValue: extensionServiceStub },
{ provide: FilterService, useValue: filterServiceStub },
{ provide: GridStateService, useValue: gridStateServiceStub },
{ provide: SortService, useValue: sortServiceStub },
GridService,
],
imports: [TranslateModule.forRoot()]
});
translate = TestBed.get(TranslateService);
service = TestBed.get(GridService);
service.init(gridStub, dataviewStub);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should create the service', () => {
expect(service).toBeTruthy();
});

describe('upsertItem method', () => {
it('should throw an error when 1st argument for the item object is missing', () => {
expect(() => service.upsertItem(null)).toThrowError('Calling Upsert of an item requires the item to include an "id" property');
});

it('should expect the service to call the "addItem" when calling "upsertItem" with the item not being found in the grid', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
const serviceSpy = jest.spyOn(service, 'addItem');
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');

service.upsertItem(mockItem);

expect(serviceSpy).toHaveBeenCalledTimes(1);
expect(dataviewSpy).toHaveBeenCalledWith(0);
expect(serviceSpy).toHaveBeenCalledWith(mockItem, true, false, true);
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItem);
});

it('should expect the service to call the "addItem" when calling "upsertItems" with the items not being found in the grid', () => {
const mockItems = [{ id: 0, user: { firstName: 'John', lastName: 'Doe' } }, { id: 5, user: { firstName: 'Jane', lastName: 'Doe' } }];
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0).mockReturnValueOnce(0).mockReturnValueOnce(0).mockReturnValueOnce(1).mockReturnValueOnce(1);
const serviceUpsertSpy = jest.spyOn(service, 'upsertItem');
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');

service.upsertItems(mockItems);

expect(dataviewSpy).toHaveBeenCalledTimes(4); // called 4x times, 2x by the upsert itself and 2x by the addItem
expect(serviceUpsertSpy).toHaveBeenCalledTimes(2);
expect(serviceUpsertSpy).toHaveBeenNthCalledWith(1, mockItems[0], false, false, false);
expect(serviceUpsertSpy).toHaveBeenNthCalledWith(2, mockItems[1], false, false, false);
expect(serviceHighlightSpy).toHaveBeenCalledWith([0, 1]);
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItems);
});

it('should expect the service to call the "upsertItem" when calling "upsertItems" with a single item which is not an array', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById');
const serviceUpsertSpy = jest.spyOn(service, 'upsertItem');
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');

service.upsertItems(mockItem, false, true, false);

expect(dataviewSpy).toHaveBeenCalledTimes(2);
expect(serviceUpsertSpy).toHaveBeenCalledTimes(1);
expect(serviceUpsertSpy).toHaveBeenCalledWith(mockItem, false, true, false);
expect(serviceHighlightSpy).not.toHaveBeenCalled();
expect(rxOnUpsertSpy).not.toHaveBeenCalled();
});

it('should call the "upsertItemById" method and expect it to call the "addItem"', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
expect(() => service.upsertItemById(undefined, mockItem)).toThrowError('Calling Upsert of an item requires the item to include a valid and unique "id" property');
});

it('should call the "upsertItemById" method and expect it to call the "addItem" with default boolean flags', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
const serviceAddItemSpy = jest.spyOn(service, 'addItem');
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');

service.upsertItemById(0, mockItem);

expect(dataviewSpy).toHaveBeenCalledWith(0);
expect(serviceAddItemSpy).toHaveBeenCalled();
expect(serviceAddItemSpy).toHaveBeenCalledWith(mockItem, true, false, true);
expect(serviceHighlightSpy).toHaveBeenCalledWith(0, HIGHLIGHT_TIMEOUT);
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItem);
});

it('should call the "upsertItemById" method and expect it to call the "addItem" with different boolean flag provided as arguments', () => {
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
const serviceAddItemSpy = jest.spyOn(service, 'addItem');
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');

service.upsertItemById(0, mockItem, false, true, false);

expect(dataviewSpy).toHaveBeenCalledWith(0);
expect(serviceAddItemSpy).toHaveBeenCalled();
expect(serviceAddItemSpy).toHaveBeenCalledWith(mockItem, false, true, false);
expect(serviceHighlightSpy).not.toHaveBeenCalled();
expect(rxOnUpsertSpy).not.toHaveBeenCalled();
});
});
});
Loading