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

Commit 5ce8b84

Browse files
authored
Merge pull request #208 from ghiscoding/feat/upsert
feat(gridService): add "upsertItem" method to Grid Service
2 parents 32291e2 + eb2ec1f commit 5ce8b84

File tree

3 files changed

+339
-36
lines changed

3 files changed

+339
-36
lines changed

src/app/examples/grid-additem.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,18 @@ export class GridAddItemComponent implements OnInit {
225225
updateSecondItem() {
226226
const updatedItem = this.angularGrid.gridService.getDataItemByRowNumber(1);
227227
updatedItem.duration = Math.round(Math.random() * 100);
228-
this.angularGrid.gridService.updateDataGridItem(updatedItem);
228+
this.angularGrid.gridService.updateItem(updatedItem);
229229

230230
// OR by id
231-
// this.angularGrid.gridService.updateDataGridItemById(updatedItem.id, updatedItem);
231+
// this.angularGrid.gridService.updateItemById(updatedItem.id, updatedItem);
232232

233233
// OR multiple changes
234234
/*
235235
const updatedItem1 = this.angularGrid.gridService.getDataItemByRowNumber(1);
236236
const updatedItem2 = this.angularGrid.gridService.getDataItemByRowNumber(2);
237237
updatedItem1.duration = Math.round(Math.random() * 100);
238238
updatedItem2.duration = Math.round(Math.random() * 100);
239-
this.angularGrid.gridService.updateDataGridItems([updatedItem1, updatedItem2], true);
239+
this.angularGrid.gridService.updateItems([updatedItem1, updatedItem2], true);
240240
*/
241241
}
242242
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3+
import { GridService, ExtensionService, FilterService, GridStateService, SortService } from '..';
4+
import { GridOption } from '../..';
5+
6+
declare var Slick: any;
7+
const HIGHLIGHT_TIMEOUT = 1500;
8+
9+
const mockSelectionModel = jest.fn().mockImplementation(() => ({
10+
init: jest.fn(),
11+
destroy: jest.fn()
12+
}));
13+
14+
jest.mock('slickgrid/plugins/slick.rowselectionmodel', () => mockSelectionModel);
15+
Slick.RowSelectionModel = mockSelectionModel;
16+
17+
let extensionServiceStub = {
18+
} as ExtensionService;
19+
20+
let filterServiceStub = {
21+
} as FilterService;
22+
23+
let gridStateServiceStub = {
24+
} as GridStateService;
25+
26+
let sortServiceStub = {
27+
} as SortService;
28+
29+
const dataviewStub = {
30+
getIdxById: jest.fn(),
31+
getItem: jest.fn(),
32+
getRowById: jest.fn(),
33+
insertItem: jest.fn(),
34+
reSort: jest.fn(),
35+
};
36+
37+
const gridStub = {
38+
getOptions: jest.fn(),
39+
getColumns: jest.fn(),
40+
getSelectionModel: jest.fn(),
41+
setSelectionModel: jest.fn(),
42+
setSelectedRows: jest.fn(),
43+
scrollRowIntoView: jest.fn(),
44+
};
45+
46+
describe('Grid Service', () => {
47+
let service: GridService;
48+
let translate: TranslateService;
49+
const gridSpy = jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true } as GridOption);
50+
51+
beforeEach(() => {
52+
TestBed.configureTestingModule({
53+
providers: [
54+
{ provide: ExtensionService, useValue: extensionServiceStub },
55+
{ provide: FilterService, useValue: filterServiceStub },
56+
{ provide: GridStateService, useValue: gridStateServiceStub },
57+
{ provide: SortService, useValue: sortServiceStub },
58+
GridService,
59+
],
60+
imports: [TranslateModule.forRoot()]
61+
});
62+
translate = TestBed.get(TranslateService);
63+
service = TestBed.get(GridService);
64+
service.init(gridStub, dataviewStub);
65+
});
66+
67+
afterEach(() => {
68+
jest.clearAllMocks();
69+
});
70+
71+
it('should create the service', () => {
72+
expect(service).toBeTruthy();
73+
});
74+
75+
describe('upsertItem method', () => {
76+
it('should throw an error when 1st argument for the item object is missing', () => {
77+
expect(() => service.upsertItem(null)).toThrowError('Calling Upsert of an item requires the item to include an "id" property');
78+
});
79+
80+
it('should expect the service to call the "addItem" when calling "upsertItem" with the item not being found in the grid', () => {
81+
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
82+
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
83+
const serviceSpy = jest.spyOn(service, 'addItem');
84+
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');
85+
86+
service.upsertItem(mockItem);
87+
88+
expect(serviceSpy).toHaveBeenCalledTimes(1);
89+
expect(dataviewSpy).toHaveBeenCalledWith(0);
90+
expect(serviceSpy).toHaveBeenCalledWith(mockItem, true, false, true);
91+
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItem);
92+
});
93+
94+
it('should expect the service to call the "addItem" when calling "upsertItems" with the items not being found in the grid', () => {
95+
const mockItems = [{ id: 0, user: { firstName: 'John', lastName: 'Doe' } }, { id: 5, user: { firstName: 'Jane', lastName: 'Doe' } }];
96+
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(0).mockReturnValueOnce(0).mockReturnValueOnce(0).mockReturnValueOnce(1).mockReturnValueOnce(1);
97+
const serviceUpsertSpy = jest.spyOn(service, 'upsertItem');
98+
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
99+
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');
100+
101+
service.upsertItems(mockItems);
102+
103+
expect(dataviewSpy).toHaveBeenCalledTimes(4); // called 4x times, 2x by the upsert itself and 2x by the addItem
104+
expect(serviceUpsertSpy).toHaveBeenCalledTimes(2);
105+
expect(serviceUpsertSpy).toHaveBeenNthCalledWith(1, mockItems[0], false, false, false);
106+
expect(serviceUpsertSpy).toHaveBeenNthCalledWith(2, mockItems[1], false, false, false);
107+
expect(serviceHighlightSpy).toHaveBeenCalledWith([0, 1]);
108+
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItems);
109+
});
110+
111+
it('should expect the service to call the "upsertItem" when calling "upsertItems" with a single item which is not an array', () => {
112+
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
113+
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById');
114+
const serviceUpsertSpy = jest.spyOn(service, 'upsertItem');
115+
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
116+
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');
117+
118+
service.upsertItems(mockItem, false, true, false);
119+
120+
expect(dataviewSpy).toHaveBeenCalledTimes(2);
121+
expect(serviceUpsertSpy).toHaveBeenCalledTimes(1);
122+
expect(serviceUpsertSpy).toHaveBeenCalledWith(mockItem, false, true, false);
123+
expect(serviceHighlightSpy).not.toHaveBeenCalled();
124+
expect(rxOnUpsertSpy).not.toHaveBeenCalled();
125+
});
126+
127+
it('should call the "upsertItemById" method and expect it to call the "addItem"', () => {
128+
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
129+
expect(() => service.upsertItemById(undefined, mockItem)).toThrowError('Calling Upsert of an item requires the item to include a valid and unique "id" property');
130+
});
131+
132+
it('should call the "upsertItemById" method and expect it to call the "addItem" with default boolean flags', () => {
133+
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
134+
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
135+
const serviceAddItemSpy = jest.spyOn(service, 'addItem');
136+
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
137+
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');
138+
139+
service.upsertItemById(0, mockItem);
140+
141+
expect(dataviewSpy).toHaveBeenCalledWith(0);
142+
expect(serviceAddItemSpy).toHaveBeenCalled();
143+
expect(serviceAddItemSpy).toHaveBeenCalledWith(mockItem, true, false, true);
144+
expect(serviceHighlightSpy).toHaveBeenCalledWith(0, HIGHLIGHT_TIMEOUT);
145+
expect(rxOnUpsertSpy).toHaveBeenCalledWith(mockItem);
146+
});
147+
148+
it('should call the "upsertItemById" method and expect it to call the "addItem" with different boolean flag provided as arguments', () => {
149+
const mockItem = { id: 0, user: { firstName: 'John', lastName: 'Doe' } };
150+
const dataviewSpy = jest.spyOn(dataviewStub, 'getRowById').mockReturnValue(undefined);
151+
const serviceAddItemSpy = jest.spyOn(service, 'addItem');
152+
const serviceHighlightSpy = jest.spyOn(service, 'highlightRow');
153+
const rxOnUpsertSpy = jest.spyOn(service.onItemUpserted, 'next');
154+
155+
service.upsertItemById(0, mockItem, false, true, false);
156+
157+
expect(dataviewSpy).toHaveBeenCalledWith(0);
158+
expect(serviceAddItemSpy).toHaveBeenCalled();
159+
expect(serviceAddItemSpy).toHaveBeenCalledWith(mockItem, false, true, false);
160+
expect(serviceHighlightSpy).not.toHaveBeenCalled();
161+
expect(rxOnUpsertSpy).not.toHaveBeenCalled();
162+
});
163+
});
164+
});

0 commit comments

Comments
 (0)