@zeedhi/teknisa-components-common 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/.package.json +4 -1
  2. package/dist/teknisa-components-common.js +3722 -32
  3. package/dist/teknisa-components-common.min.js +3722 -32
  4. package/dist/types/components/index.d.ts +5 -0
  5. package/dist/types/components/tek-datasource/index.d.ts +3 -0
  6. package/dist/types/components/tek-datasource/interfaces.d.ts +16 -0
  7. package/dist/types/components/tek-datasource/tek-memory-datasource.d.ts +93 -0
  8. package/dist/types/components/tek-datasource/tek-rest-datasource.d.ts +95 -0
  9. package/dist/types/components/tek-grid/columns-searcher.d.ts +5 -0
  10. package/dist/types/components/tek-grid/dynamic-filter-datasource-factory.d.ts +6 -0
  11. package/dist/types/components/tek-grid/filter-helper.d.ts +7 -0
  12. package/dist/types/components/tek-grid/grid-filter-button.d.ts +29 -0
  13. package/dist/types/components/tek-grid/grouped-data-manager.d.ts +82 -0
  14. package/dist/types/components/tek-grid/grouped-data-selector.d.ts +7 -0
  15. package/dist/types/components/tek-grid/grouped-view-navigator.d.ts +14 -0
  16. package/dist/types/components/tek-grid/index.d.ts +18 -0
  17. package/dist/types/components/tek-grid/interfaces.d.ts +259 -0
  18. package/dist/types/components/tek-grid/keymap-grouped.d.ts +6 -0
  19. package/dist/types/components/tek-grid/layout-options.d.ts +39 -0
  20. package/dist/types/components/tek-grid/tek-grid-column.d.ts +42 -0
  21. package/dist/types/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button-controller.d.ts +8 -0
  22. package/dist/types/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button.d.ts +13 -0
  23. package/dist/types/components/tek-grid/tek-grid-controller.d.ts +31 -0
  24. package/dist/types/components/tek-grid/tek-grid-events.d.ts +31 -0
  25. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/button-option.d.ts +17 -0
  26. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/index.d.ts +3 -0
  27. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/interfaces.d.ts +5 -0
  28. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/export-options/multi-option.d.ts +12 -0
  29. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/index.d.ts +2 -0
  30. package/dist/types/components/tek-grid/tek-grid-toolbar-provider/tek-grid-toolbar-provider.d.ts +22 -0
  31. package/dist/types/components/tek-grid/tek-grid.d.ts +216 -0
  32. package/dist/types/components/tek-user-info/TekUserInfoController.d.ts +22 -0
  33. package/dist/types/components/tek-user-info/interfaces.d.ts +27 -0
  34. package/dist/types/components/tek-user-info/tek-user-info-list.d.ts +32 -0
  35. package/dist/types/components/tek-user-info/tek-user-info.d.ts +37 -0
  36. package/dist/types/error/tek-grid-delete-rows-error.d.ts +7 -0
  37. package/dist/types/error/teknisa-common-error.d.ts +6 -0
  38. package/dist/types/index.d.ts +1 -0
  39. package/dist/types/utils/config/config.d.ts +7 -0
  40. package/dist/types/utils/index.d.ts +3 -0
  41. package/dist/types/utils/is-filled-object/is-filled-object.d.ts +2 -0
  42. package/dist/types/utils/is-nil.d.ts +1 -0
  43. package/package.json +2 -2
  44. package/src/components/index.ts +5 -12
  45. package/src/components/tek-datasource/index.ts +3 -0
  46. package/src/components/tek-datasource/interfaces.ts +36 -0
  47. package/src/components/tek-datasource/tek-memory-datasource.ts +314 -0
  48. package/src/components/tek-datasource/tek-rest-datasource.ts +224 -0
  49. package/src/components/tek-grid/columns-searcher.ts +22 -0
  50. package/src/components/tek-grid/dynamic-filter-datasource-factory.ts +20 -0
  51. package/src/components/tek-grid/filter-helper.ts +20 -0
  52. package/src/components/tek-grid/grid-filter-button.ts +419 -0
  53. package/src/components/tek-grid/grouped-data-manager.ts +448 -0
  54. package/src/components/tek-grid/grouped-data-selector.ts +40 -0
  55. package/src/components/tek-grid/grouped-view-navigator.ts +84 -0
  56. package/src/components/tek-grid/index.ts +18 -0
  57. package/src/components/tek-grid/interfaces.ts +329 -0
  58. package/src/components/tek-grid/keymap-grouped.ts +20 -0
  59. package/src/components/tek-grid/layout-options.ts +248 -0
  60. package/src/components/tek-grid/tek-grid-column.ts +193 -0
  61. package/src/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button-controller.ts +28 -0
  62. package/src/components/tek-grid/tek-grid-columns-button/tek-grid-columns-button.ts +38 -0
  63. package/src/components/tek-grid/tek-grid-controller.ts +140 -0
  64. package/src/components/tek-grid/tek-grid-events.ts +105 -0
  65. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/button-option.ts +26 -0
  66. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/index.ts +3 -0
  67. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/interfaces.ts +6 -0
  68. package/src/components/tek-grid/tek-grid-toolbar-provider/export-options/multi-option.ts +85 -0
  69. package/src/components/tek-grid/tek-grid-toolbar-provider/index.ts +2 -0
  70. package/src/components/tek-grid/tek-grid-toolbar-provider/tek-grid-toolbar-provider.ts +365 -0
  71. package/src/components/tek-grid/tek-grid.ts +1118 -0
  72. package/src/components/tek-user-info/TekUserInfoController.ts +87 -0
  73. package/src/components/tek-user-info/interfaces.ts +21 -0
  74. package/src/components/tek-user-info/tek-user-info-list.ts +64 -0
  75. package/src/components/tek-user-info/tek-user-info.ts +337 -0
  76. package/src/error/tek-grid-delete-rows-error.ts +15 -0
  77. package/src/error/teknisa-common-error.ts +8 -0
  78. package/src/index.ts +1 -0
  79. package/src/utils/config/config.ts +8 -0
  80. package/src/utils/index.ts +3 -0
  81. package/src/utils/is-filled-object/is-filled-object.ts +5 -0
  82. package/src/utils/is-nil.ts +3 -0
  83. package/tests/unit/components/tek-grid/button-option.spec.ts +49 -0
  84. package/tests/unit/components/tek-grid/columns-searcher.spec.ts +112 -0
  85. package/tests/unit/components/tek-grid/dynamic-filter-datasource-factory.spec.ts +90 -0
  86. package/tests/unit/components/tek-grid/filter-helper.spec.ts +34 -130
  87. package/tests/unit/components/tek-grid/grid-filter-button.spec.ts +110 -241
  88. package/tests/unit/components/tek-grid/grouped-data-manager.spec.ts +593 -0
  89. package/tests/unit/components/tek-grid/grouped-data-selector.spec.ts +136 -0
  90. package/tests/unit/components/tek-grid/grouped-view-navigator.spec.ts +244 -0
  91. package/tests/unit/components/tek-grid/keymap-grouped.spec.ts +20 -0
  92. package/tests/unit/components/tek-grid/{layout_options.spec.ts → layout-options.spec.ts} +77 -35
  93. package/tests/unit/components/tek-grid/multi-option.spec.ts +139 -0
  94. package/tests/unit/components/tek-grid/{grid-column.spec.ts → tek-grid-column.spec.ts} +48 -6
  95. package/tests/unit/components/tek-grid/{grid-columns-button.spec.ts → tek-grid-columns-button.spec.ts} +42 -9
  96. package/tests/unit/components/tek-grid/tek-grid-controller.spec.ts +253 -0
  97. package/tests/unit/components/tek-grid/tek-grid-events.spec.ts +186 -0
  98. package/tests/unit/components/tek-grid/tek-grid-toolbar-provider.spec.ts +34 -0
  99. package/tests/unit/components/tek-grid/tek-grid.spec.ts +895 -0
  100. package/tests/unit/components/tek-grid/tek-memory-datasource.spec.ts +482 -0
  101. package/tests/unit/components/tek-grid/tek-rest-datasource.spec.ts +422 -0
  102. package/src/error/delete-rows-error.ts +0 -11
  103. package/tests/unit/components/tek-grid/grid.spec.ts +0 -2701
@@ -1,2701 +0,0 @@
1
- /* eslint-disable no-underscore-dangle */
2
-
3
- import {
4
- Button, Dropdown, type IModal, Modal, ModalService, Search, TextInput, Text, Report,
5
- AlertService,
6
- EmptyDataError,
7
- } from '@zeedhi/common';
8
- import {
9
- KeyMap, Http, Metadata, type IDictionary, type IEventParam,
10
- Datasource,
11
- init,
12
- } from '@zeedhi/core';
13
- import {
14
- type ITekGrid,
15
- type ITekGridFilterButton,
16
- TekGrid, TekGridColumn, TekGridFilterButton, TekRestDatasource,
17
- } from '../../../../src';
18
- import { ReportFilter } from '../../../../src/utils';
19
- import { setClick, getChild } from '../../../__helpers__';
20
- import { flushPromises } from '../../../__helpers__/flush-promises-helper';
21
-
22
- jest.mock('lodash.debounce', () => jest.fn((fn) => fn));
23
-
24
- jest.useFakeTimers();
25
-
26
- class AppController {
27
- public datasource = {
28
- uniqueKey: 'id',
29
- data: [{ id: '1' }],
30
- };
31
- }
32
-
33
- const clickOnFilterButton = (grid: TekGrid, event?: any) => {
34
- const buttonProps = getChild<ITekGridFilterButton>(grid.toolbarSlot, `${grid.name}_filterButton`);
35
- const filterButton = new TekGridFilterButton(buttonProps);
36
- setClick(filterButton, event);
37
-
38
- return filterButton;
39
- };
40
-
41
- const createAndMount = (props: ITekGrid) => {
42
- const instance = new TekGrid(props);
43
- instance.onMounted({} as HTMLElement);
44
- return instance;
45
- };
46
-
47
- describe('TekGrid', () => {
48
- beforeEach(() => {
49
- // clear all metadata instances before testing
50
- const { instances } = Metadata as any;
51
- Object.keys(instances).forEach((key) => {
52
- Metadata.clearInstance(key, instances[key].componentId);
53
- });
54
-
55
- jest.clearAllTimers();
56
- });
57
-
58
- describe('constructor()', () => {
59
- it('should create new grid with default values', () => {
60
- const instance = new TekGrid({ name: 'Grid', component: 'TekGrid' });
61
- expect(instance.title).toBe('');
62
- expect(instance.addButton).toBeFalsy();
63
- expect(instance.deleteButton).toBe('none');
64
- expect(instance.filterButton).toBeFalsy();
65
- expect(instance.modalFilterProps).toEqual(
66
- expect.objectContaining({
67
- height: 'auto',
68
- persistent: true,
69
- }),
70
- );
71
- expect(instance.columnFilterButton).toBeFalsy();
72
- expect(instance.dragColumns).toBeTruthy();
73
- expect(instance.resizeColumns).toBeTruthy();
74
- expect(instance.showLayoutOptions).toBeTruthy();
75
- expect(instance.showSearch).toBeTruthy();
76
- expect(instance.showSummaryTotal).toBeTruthy();
77
- expect(instance.showReload).toBeTruthy();
78
- expect(instance.columnsButton).toBeFalsy();
79
- expect(instance.columnsButtonIgnore).toEqual([]);
80
- });
81
-
82
- it('should create new Grid replacing default values', () => {
83
- const instance = new TekGrid({
84
- name: 'grid',
85
- component: 'TekGrid',
86
- title: 'title',
87
- addButton: true,
88
- deleteButton: 'selection',
89
- filterButton: true,
90
- modalFilterProps: {
91
- name: 'teste',
92
- height: 'auto',
93
- persistent: false,
94
- cssClass: 'class-test',
95
- cssStyle: 'background: red',
96
- draggable: true,
97
- fullscreen: true,
98
- light: false,
99
- dark: true,
100
- },
101
- columnFilterButton: true,
102
- dragColumns: false,
103
- resizeColumns: false,
104
- showLayoutOptions: false,
105
- showSearch: false,
106
- showSummaryTotal: false,
107
- showReload: false,
108
- columns: [
109
- { name: 'id' },
110
- ],
111
- columnsButton: true,
112
- columnsButtonIgnore: ['id'],
113
- });
114
- expect(instance.title).toBe('title');
115
- expect(instance.addButton).toBeTruthy();
116
- expect(instance.deleteButton).toBe('selection');
117
- expect(instance.filterButton).toBeTruthy();
118
- expect(instance.modalFilterProps).toEqual(
119
- expect.objectContaining({
120
- height: 'auto',
121
- persistent: false,
122
- }),
123
- );
124
- expect(instance.columnFilterButton).toBeTruthy();
125
- expect(instance.dragColumns).toBeFalsy();
126
- expect(instance.resizeColumns).toBeFalsy();
127
- expect(instance.showLayoutOptions).toBeFalsy();
128
- expect(instance.showSearch).toBeFalsy();
129
- expect(instance.showSummaryTotal).toBeFalsy();
130
- expect(instance.showReload).toBeFalsy();
131
- expect(instance.columns[0]).toBeInstanceOf(TekGridColumn);
132
- expect(instance.columnsButton).toBeTruthy();
133
- expect(instance.columnsButtonIgnore).toEqual(['id']);
134
- });
135
-
136
- it('should create toolbar and footer', () => {
137
- const instance = new TekGrid({
138
- name: 'grid',
139
- component: 'TekGrid',
140
- title: 'title',
141
- addButton: true,
142
- deleteButton: 'selection',
143
- filterButton: true,
144
- columnFilterButton: true,
145
- dragColumns: false,
146
- resizeColumns: false,
147
- showLayoutOptions: false,
148
- });
149
- instance.onCreated();
150
- expect(instance.toolbarSlot.length).toBe(3);
151
- });
152
-
153
- it('should overwrite toolbar', () => {
154
- const instance = new TekGrid({
155
- name: 'grid',
156
- component: 'TekGrid',
157
- title: 'title',
158
- addButton: true,
159
- deleteButton: 'selection',
160
- filterButton: true,
161
- columnFilterButton: true,
162
- dragColumns: false,
163
- resizeColumns: false,
164
- showLayoutOptions: false,
165
- toolbarSlot: [],
166
- });
167
- instance.onCreated();
168
- expect(instance.toolbarSlot.length).toBe(0);
169
- });
170
-
171
- it('should change export buttons from exportConfig', () => {
172
- const instance = new TekGrid({
173
- name: 'grid',
174
- component: 'TekGrid',
175
- exportConfig: [
176
- {
177
- type: 'pdf',
178
- iconName: 'icon',
179
- portrait: false,
180
- },
181
- ],
182
- });
183
-
184
- expect((instance as any).gridBase.exportConfigButtons[0].label).toBeUndefined();
185
- expect((instance as any).gridBase.exportConfigButtons[0].iconName).toBe('icon');
186
- expect((instance as any).gridBase.exportConfigButtons[0].name).toBe('grid_export_pdf_landscape');
187
- expect((instance as any).gridBase.exportConfigButtons[0].component).toBe('ZdButton');
188
- expect((instance as any).gridBase.exportConfigButtons[0].flat).toBeTruthy();
189
- expect((instance as any).gridBase.exportConfigButtons[0].events).toBeDefined();
190
- });
191
-
192
- it('should create new TekGrid with accessor datasource', () => {
193
- init({
194
- controllers: {
195
- AppController,
196
- },
197
- });
198
- const instance = new TekGrid({
199
- name: 'grid',
200
- component: 'TekGrid',
201
- datasource: '{{AppController.datasource}}' as any,
202
- });
203
-
204
- expect(instance.datasource).toBeInstanceOf(Datasource);
205
- expect(instance.datasource.data).toEqual([{ id: '1' }]);
206
- });
207
- });
208
-
209
- describe('getReport()', () => {
210
- let httpReportSpy: jest.SpyInstance;
211
- let reportSpy: jest.SpyInstance;
212
-
213
- beforeEach(() => {
214
- reportSpy = jest.spyOn(Report.prototype, 'getReport').mockReturnValue(Promise.resolve(''));
215
- httpReportSpy = jest.spyOn(Http, 'post').mockImplementation((url) => {
216
- if (url === 'https://zhreport.teknisa.com/generateReport') {
217
- return Promise.resolve({
218
- data: { messages: [{ message: 'pdf-report.html' }] },
219
- });
220
- }
221
-
222
- return Promise.reject();
223
- });
224
- });
225
-
226
- afterEach(() => {
227
- reportSpy.mockClear();
228
- httpReportSpy.mockReset();
229
- });
230
-
231
- it('should call window open on getReport method', async () => {
232
- (window as any).open = jest.fn();
233
-
234
- const instance = new TekGrid({
235
- name: 'grid',
236
- component: 'TekGrid',
237
- events: {
238
- beforeOpenReport: jest.fn(),
239
- },
240
- columns: [
241
- {
242
- name: 'id',
243
- label: 'ID',
244
- aggregation: 'COUNT',
245
- },
246
- {
247
- name: 'department_id',
248
- label: 'Department ID',
249
- grouped: true,
250
- },
251
- ],
252
- });
253
-
254
- const spy = jest.spyOn(window, 'open');
255
- await instance.getReport('pdf');
256
-
257
- expect(spy).toHaveBeenCalledTimes(1);
258
-
259
- (window as any).open.mockClear();
260
- spy.mockReset();
261
- });
262
-
263
- it('when called with a dynamicFilter, should format the filter values', async () => {
264
- (window as any).open = jest.fn();
265
- const spy = jest.spyOn(Report.prototype, 'getReport').mockImplementationOnce(async () => '');
266
- const instance = new TekGrid({
267
- name: 'grid_getReport',
268
- component: 'TekGrid',
269
- columns: [
270
- {
271
- name: 'department_id',
272
- label: 'Department',
273
- componentProps: {
274
- component: 'ZdSelect',
275
- name: 'select',
276
- dataText: [
277
- 'id',
278
- 'name',
279
- ],
280
- dataValue: 'id',
281
- datasource: {
282
- uniqueKey: 'id',
283
- data: [
284
- { id: 1, name: 'Department 1' },
285
- { id: 2, name: 'Department 2' },
286
- ],
287
- },
288
- },
289
- },
290
- {
291
- name: 'name',
292
- label: 'Name',
293
- },
294
- ],
295
- datasource: {
296
- type: 'tek-rest',
297
- route: '/employees',
298
- lazyLoad: true,
299
- dynamicFilter: {
300
- department_id: [
301
- {
302
- relation: 'OR',
303
- operation: 'EQUALS',
304
- value: 1,
305
- },
306
- {
307
- relation: 'OR',
308
- operation: 'EQUALS',
309
- value: 2,
310
- },
311
- ],
312
- name: [
313
- {
314
- relation: 'OR',
315
- operation: 'EQUALS',
316
- value: 'Employee 1',
317
- },
318
- ],
319
- },
320
- },
321
- });
322
-
323
- const filter = new ReportFilter({
324
- Department: [
325
- {
326
- relation: 'OR',
327
- operation: 'EQUALS',
328
- value: '1 | Department 1',
329
- },
330
- {
331
- relation: 'OR',
332
- operation: 'EQUALS',
333
- value: '2 | Department 2',
334
- },
335
- ],
336
- Name: [
337
- {
338
- relation: 'OR',
339
- operation: 'EQUALS',
340
- value: 'Employee 1',
341
- },
342
- ],
343
- }).build();
344
-
345
- await instance.getReport('pdf');
346
-
347
- expect(spy).toHaveBeenLastCalledWith('pdf', undefined, expect.objectContaining({
348
- metaData: expect.objectContaining({
349
- filter,
350
- }),
351
- }), undefined);
352
- (window as any).open.mockClear();
353
- spy.mockReset();
354
- });
355
-
356
- it('when called with a dynamicFilter, should handle exceptions', async () => {
357
- (window as any).open = jest.fn();
358
- const spy = jest.spyOn(Report.prototype, 'getReport').mockImplementationOnce(async () => '');
359
- const instance = new TekGrid({
360
- name: 'grid_getReport',
361
- component: 'TekGrid',
362
- datasource: {
363
- type: 'tek-rest',
364
- route: '/employees',
365
- lazyLoad: true,
366
- dynamicFilter: {
367
- invisible_column: [
368
- {
369
- relation: 'OR',
370
- operation: 'EQUALS',
371
- value: 1,
372
- },
373
- ],
374
- },
375
- },
376
- });
377
-
378
- const getReport = async () => { await instance.getReport('pdf'); };
379
-
380
- expect(getReport).not.toThrow();
381
-
382
- const getColumnSpy = jest.spyOn(instance, 'getColumn').mockImplementationOnce(() => {
383
- throw Error();
384
- });
385
-
386
- expect(getReport()).rejects.toEqual(new Error());
387
-
388
- (window as any).open.mockClear();
389
- spy.mockReset();
390
- getColumnSpy.mockReset();
391
- });
392
-
393
- it('should call getReport always with "xls" when there is no group', async () => {
394
- (window as any).open = jest.fn();
395
-
396
- const instance = new TekGrid({
397
- name: 'grid',
398
- component: 'TekGrid',
399
- columns: [
400
- {
401
- name: 'id',
402
- label: 'ID',
403
- },
404
- {
405
- name: 'department_id',
406
- label: 'Department ID',
407
- },
408
- ],
409
- });
410
-
411
- await instance.getReport('xls2');
412
-
413
- expect(reportSpy).toHaveBeenCalledWith('xls', undefined, {
414
- metaData: {
415
- columns: {}, filter: undefined, groups: [], xlsDefaultType: undefined,
416
- },
417
- }, undefined);
418
- });
419
-
420
- it('when data is empty, should show alert', async () => {
421
- reportSpy = jest.spyOn(Report.prototype, 'getReport').mockImplementation(() => { throw new EmptyDataError(); });
422
- const alertSpy = jest.spyOn(AlertService, 'show').mockImplementation(() => 1);
423
-
424
- const instance = new TekGrid({
425
- name: 'grid',
426
- component: 'TekGrid',
427
- datasource: {
428
- data: [],
429
- },
430
- });
431
-
432
- expect(async () => instance.getReport('xls2')).not.toThrow();
433
-
434
- expect(alertSpy).toHaveBeenCalledWith({ name: 'no-data-warning', text: 'TEKGRID_NO_DATA', color: 'warning' });
435
- });
436
-
437
- it('when an error happens, should throw normally', async () => {
438
- reportSpy = jest.spyOn(Report.prototype, 'getReport').mockImplementation(() => { throw new Error(); });
439
-
440
- const instance = new TekGrid({
441
- name: 'grid',
442
- component: 'TekGrid',
443
- datasource: {
444
- data: [],
445
- },
446
- });
447
-
448
- expect(async () => instance.getReport('xls2')).rejects.toThrow();
449
- });
450
- });
451
-
452
- describe('saveChanges()', () => {
453
- it('should call saveEditedRows', () => {
454
- const instance = new TekGrid({
455
- name: 'grid_saveChanges1',
456
- component: 'TekGrid',
457
- });
458
-
459
- const spy = jest.spyOn(instance, 'saveEditedRows');
460
- instance.onCreated();
461
- const toolbarDiv = instance.toolbarSlot[2];
462
- if (toolbarDiv.children) {
463
- const button = new Button(toolbarDiv.children[1]);
464
- const event = new Event('click');
465
- button.click(event, {} as HTMLElement);
466
- expect(spy).toBeCalled();
467
- expect(button.isVisible).toBeFalsy();
468
- spy.mockReset();
469
- }
470
- });
471
-
472
- it('should call events', () => {
473
- let beforeSaveCalled = false;
474
- let afterSaveCalled = false;
475
- const instance = new TekGrid({
476
- name: 'grid_saveChanges2',
477
- component: 'TekGrid',
478
- events: {
479
- beforeSave: () => { beforeSaveCalled = true; },
480
- afterSave: () => { afterSaveCalled = true; },
481
- },
482
- });
483
-
484
- instance.onCreated();
485
- const toolbarDiv = instance.toolbarSlot[2];
486
- if (toolbarDiv.children) {
487
- const button = new Button(toolbarDiv.children[1]);
488
- const event = new Event('click');
489
- button.click(event, {} as HTMLElement);
490
- expect(beforeSaveCalled).toBeTruthy();
491
- expect(afterSaveCalled).toBeTruthy();
492
- }
493
- });
494
-
495
- it('should not call saveEditedRows if beforeSave call preventDefault', () => {
496
- let beforeSaveCalled = false;
497
- let afterSaveCalled = false;
498
- const instance = new TekGrid({
499
- name: 'grid_saveChanges3',
500
- component: 'TekGrid',
501
- events: {
502
- beforeSave: () => { beforeSaveCalled = true; },
503
- afterSave: () => { afterSaveCalled = true; },
504
- },
505
- });
506
-
507
- const spy = jest.spyOn(instance, 'saveEditedRows');
508
- instance.onCreated();
509
- const toolbarDiv = instance.toolbarSlot[2];
510
- if (toolbarDiv.children) {
511
- const button = new Button(toolbarDiv.children[1]);
512
- button.click({ defaultPrevented: true } as Event, {} as HTMLElement);
513
- expect(beforeSaveCalled).toBeTruthy();
514
- expect(afterSaveCalled).toBeFalsy();
515
- expect(spy).not.toBeCalled();
516
- spy.mockReset();
517
- }
518
- });
519
- });
520
-
521
- describe('cancelChanges()', () => {
522
- it('should call cancelEditedRows', () => {
523
- const instance = new TekGrid({
524
- name: 'grid_cancelChanges1',
525
- component: 'TekGrid',
526
- });
527
-
528
- const spy = jest.spyOn(instance, 'cancelEditedRows');
529
- instance.onCreated();
530
- const toolbarDiv = instance.toolbarSlot[2];
531
- if (toolbarDiv.children) {
532
- const button = new Button(toolbarDiv.children[2]);
533
- const event = new Event('click');
534
- button.click(event, {} as HTMLElement);
535
- expect(spy).toBeCalled();
536
- expect(button.isVisible).toBeFalsy();
537
- spy.mockReset();
538
- }
539
- });
540
-
541
- it('should call events', () => {
542
- let beforeCancelCalled = false;
543
- let afterCancelCalled = false;
544
- const instance = new TekGrid({
545
- name: 'grid_cancelChanges2',
546
- component: 'TekGrid',
547
- events: {
548
- beforeCancel: () => { beforeCancelCalled = true; },
549
- afterCancel: () => { afterCancelCalled = true; },
550
- },
551
- });
552
-
553
- instance.onCreated();
554
- const toolbarDiv = instance.toolbarSlot[2];
555
- if (toolbarDiv.children) {
556
- const button = new Button(toolbarDiv.children[2]);
557
- const event = new Event('click');
558
- button.click(event, {} as HTMLElement);
559
- expect(beforeCancelCalled).toBeTruthy();
560
- expect(afterCancelCalled).toBeTruthy();
561
- }
562
- });
563
-
564
- it('should not call cancelEditedRows if beforeSave call preventDefault', () => {
565
- let beforeCancelCalled = false;
566
- let afterCancelCalled = false;
567
- const instance = new TekGrid({
568
- name: 'grid_cancelChanges3',
569
- component: 'TekGrid',
570
- events: {
571
- beforeCancel: () => { beforeCancelCalled = true; },
572
- afterCancel: () => { afterCancelCalled = true; },
573
- },
574
- });
575
-
576
- const spy = jest.spyOn(instance, 'cancelEditedRows');
577
- instance.onCreated();
578
- const toolbarDiv = instance.toolbarSlot[2];
579
- if (toolbarDiv.children) {
580
- const button = new Button(toolbarDiv.children[2]);
581
- button.click({ defaultPrevented: true } as Event, {} as HTMLElement);
582
- expect(beforeCancelCalled).toBeTruthy();
583
- expect(afterCancelCalled).toBeFalsy();
584
- expect(spy).not.toBeCalled();
585
- spy.mockReset();
586
- }
587
- });
588
- });
589
-
590
- describe('addButtonClick()', () => {
591
- it('should call event', () => {
592
- let addClickCalled = false;
593
- const instance = new TekGrid({
594
- name: 'grid_addClick1',
595
- component: 'TekGrid',
596
- addButton: true,
597
- editNewRowsOnly: true,
598
- });
599
-
600
- instance.onCreated();
601
- const toolbarDiv = instance.toolbarSlot[2];
602
- if (toolbarDiv.children) {
603
- const notEditingSpan = toolbarDiv.children[0];
604
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 0) {
605
- const addTooltip = notEditingSpan.children[0];
606
- if (addTooltip && addTooltip.children && addTooltip.children.length > 0) {
607
- const addButton = addTooltip.children[0];
608
- const button = new Button(addButton);
609
- const event = new Event('click');
610
- button.click(event, {} as HTMLElement);
611
- expect(addClickCalled).toBeFalsy();
612
- instance.events = {
613
- addClick: () => { addClickCalled = true; },
614
- };
615
- expect(instance.editingNewRows).toBeTruthy();
616
- button.click(event, {} as HTMLElement);
617
- expect(addClickCalled).toBeTruthy();
618
- expect(button.isVisible).toBeTruthy();
619
- }
620
- }
621
- }
622
- });
623
- });
624
-
625
- describe('hideButtonClick()', () => {
626
- it('should hide toolbar', () => {
627
- const instance = new TekGrid({
628
- name: 'grid_deleteClick2',
629
- component: 'TekGrid',
630
- });
631
-
632
- instance.onCreated();
633
-
634
- const toolbarMock = { isVisible: true };
635
- jest.spyOn(Metadata, 'getInstance').mockImplementation(() => toolbarMock);
636
-
637
- const hideTooltip = instance.toolbarSlot[1];
638
- expect(toolbarMock.isVisible).toBeTruthy();
639
-
640
- if (hideTooltip.children) {
641
- const hideButton = hideTooltip.children[0];
642
- const button = new Button(hideButton);
643
- const event = new Event('click');
644
- button.click(event, {} as HTMLElement);
645
-
646
- expect(toolbarMock.isVisible).toBeFalsy();
647
- }
648
- });
649
-
650
- it('should show toolbar', () => {
651
- const instance = new TekGrid({
652
- name: 'grid_deleteClick2',
653
- component: 'TekGrid',
654
- toolbarOpened: false,
655
- });
656
-
657
- instance.onCreated();
658
-
659
- const toolbarMock = { isVisible: true };
660
- jest.spyOn(Metadata, 'getInstance').mockImplementation(() => toolbarMock);
661
-
662
- const hideTooltip = instance.toolbarSlot[1];
663
-
664
- if (hideTooltip.children) {
665
- const hideButton = hideTooltip.children[0];
666
- const button = new Button(hideButton);
667
- const event = new Event('click');
668
- button.click(event, {} as HTMLElement);
669
-
670
- expect(toolbarMock.isVisible).toBeFalsy();
671
-
672
- button.click(event, {} as HTMLElement);
673
-
674
- expect(toolbarMock.isVisible).toBeTruthy();
675
- }
676
- });
677
- });
678
-
679
- describe('deleteButtonClick()', () => {
680
- it('should call deleteRows', () => {
681
- const instance = new TekGrid({
682
- name: 'grid_deleteButtonClick1',
683
- component: 'TekGrid',
684
- deleteButton: 'currentRow',
685
- });
686
-
687
- const spy = jest.spyOn(instance, 'deleteRows');
688
- instance.onCreated();
689
- const notEditingTag = instance.toolbarSlot[2];
690
- if (notEditingTag.children) {
691
- const notEditingSpan = notEditingTag.children[0];
692
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
693
- const deleteTooltip = notEditingSpan.children[1];
694
- if (deleteTooltip && deleteTooltip.children && deleteTooltip.children.length > 0) {
695
- const deleteButton = deleteTooltip.children[0];
696
- const button = new Button(deleteButton);
697
- const event = new Event('click');
698
- button.click(event, {} as HTMLElement);
699
- expect(spy).toBeCalled();
700
- expect(button.isVisible).toBeTruthy();
701
- expect(button.disabled).toBeTruthy();
702
- }
703
- }
704
- }
705
- spy.mockReset();
706
- });
707
-
708
- it('should call events', () => {
709
- let beforeDeleteCalled = false;
710
- let afterDeleteCalled = false;
711
- const instance = new TekGrid({
712
- name: 'grid_deleteClick2',
713
- component: 'TekGrid',
714
- events: {
715
- beforeDelete: () => { beforeDeleteCalled = true; },
716
- afterDelete: () => { afterDeleteCalled = true; },
717
- },
718
- });
719
-
720
- instance.onCreated();
721
- const notEditingSpan = instance.toolbarSlot[2];
722
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
723
- const deleteTooltip = notEditingSpan.children[1];
724
- if (deleteTooltip && deleteTooltip.children && deleteTooltip.children.length > 0) {
725
- const deleteButton = deleteTooltip.children[0];
726
- const button = new Button(deleteButton);
727
- const event = new Event('click');
728
- button.click(event, {} as HTMLElement);
729
- expect(beforeDeleteCalled).toBeTruthy();
730
- expect(afterDeleteCalled).toBeTruthy();
731
- }
732
- }
733
- });
734
-
735
- it('should not call cancelEditedRows if beforeSave call preventDefault', () => {
736
- let beforeDeleteCalled = false;
737
- let afterDeleteCalled = false;
738
- const instance = new TekGrid({
739
- name: 'grid_deleteClick3',
740
- component: 'TekGrid',
741
- events: {
742
- beforeDelete: () => { beforeDeleteCalled = true; },
743
- afterDelete: () => { afterDeleteCalled = true; },
744
- },
745
- });
746
-
747
- const spy = jest.spyOn(instance, 'cancelEditedRows');
748
- instance.onCreated();
749
- const notEditingSpan = instance.toolbarSlot[2];
750
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
751
- const deleteTooltip = notEditingSpan.children[0];
752
- if (deleteTooltip.children) {
753
- const teste = deleteTooltip.children[1];
754
- if (teste && teste.children && teste.children.length > 0) {
755
- const deleteButton = teste.children[0];
756
- const button = new Button(deleteButton);
757
- button.click({ defaultPrevented: true } as Event, {} as HTMLElement);
758
- expect(beforeDeleteCalled).toBeTruthy();
759
- expect(afterDeleteCalled).toBeFalsy();
760
- expect(spy).not.toBeCalled();
761
- }
762
- }
763
- }
764
- spy.mockReset();
765
- });
766
-
767
- it('when deleteButton is clicked with selectAllPages, should throw', () => {
768
- const instance = new TekGrid({
769
- name: 'grid_deleteButtonClick1',
770
- component: 'TekGrid',
771
- deleteButton: 'selection',
772
- selectAllPages: true,
773
- datasource: {
774
- data: [{ id: '1' }],
775
- },
776
- });
777
-
778
- instance.onCreated();
779
- const notEditingSpan = instance.toolbarSlot[2];
780
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
781
- const deleteTooltip = notEditingSpan.children[0];
782
- if (deleteTooltip.children) {
783
- const teste = deleteTooltip.children[1];
784
- if (teste && teste.children && teste.children.length > 0) {
785
- const deleteButton = teste.children[0];
786
- const button = new Button(deleteButton);
787
- const event = new Event('click');
788
- expect(button.isVisible).toBeTruthy();
789
- expect(button.disabled).toBeTruthy();
790
- instance.selectAll(true);
791
- expect(button.disabled).toBeFalsy();
792
- expect(() => button.click(event, {} as HTMLElement)).toThrow();
793
- }
794
- }
795
- }
796
- });
797
- });
798
-
799
- describe('disableDeleteButton', () => {
800
- it('should not disable deleteButton when deleteButton equals none', () => {
801
- const instance = new TekGrid({
802
- name: 'grid_deleteButtonClick1',
803
- component: 'TekGrid',
804
- deleteButton: 'none',
805
- });
806
-
807
- instance.onCreated();
808
- const notEditingSpan = instance.toolbarSlot[2];
809
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 1) {
810
- const deleteTooltip = notEditingSpan.children[0];
811
- if (deleteTooltip.children) {
812
- const test = deleteTooltip.children[1];
813
- if (test && test.children && test.children.length > 0) {
814
- const deleteButton = test.children[0];
815
- const button = new Button(deleteButton);
816
- expect(button.isVisible).toBeFalsy();
817
- expect(button.disabled).toBeFalsy();
818
- }
819
- }
820
- }
821
- });
822
- });
823
-
824
- describe('navigation', () => {
825
- const dispatchEvent = (key: string, modifiers: any) => {
826
- const event = new KeyboardEvent('keydown', modifiers);
827
- delete (event as any).key;
828
- Object.defineProperty(event, 'key', { value: key });
829
- delete (event as any).code;
830
- Object.defineProperty(event, 'code', { value: key });
831
- document.dispatchEvent(event);
832
- };
833
-
834
- it('should navigate through grid lines using keys', async () => {
835
- const httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
836
- data: {
837
- data: [],
838
- pagination: {
839
- page: 1,
840
- total: 0,
841
- limit: 10,
842
- },
843
- },
844
- }));
845
- const instance = new TekGrid({
846
- name: 'grid_navigation_1',
847
- component: 'TekGrid',
848
- columns: [
849
- {
850
- name: 'id',
851
- label: 'id',
852
- },
853
- {
854
- name: 'name',
855
- label: 'name',
856
- },
857
- {
858
- name: 'department',
859
- label: 'department',
860
- },
861
- ],
862
- datasource: {
863
- uniqueKey: 'id',
864
- order: ['name.asc'],
865
- data: [
866
- { id: 2, name: 'employee 2', department: 1 },
867
- { id: 5, name: 'employee 5', department: 2 },
868
- { id: 4, name: 'employee 4', department: 2 },
869
- { id: 3, name: 'employee 3', department: 1 },
870
- { id: 1, name: 'employee 1', department: 1 },
871
- ],
872
- },
873
- });
874
-
875
- await flushPromises();
876
-
877
- const elem = document.createElement('DIV');
878
- (KeyMap as any).isElementActive = () => true;
879
- instance.onCreated();
880
- instance.onMounted(elem);
881
- instance.navigateDatasource(false);
882
- expect(instance.datasource.currentRow).toEqual(instance.datasource.data[0]);
883
-
884
- instance.navigateDatasource(false);
885
- expect(instance.datasource.currentRow).toEqual(instance.datasource.data[1]);
886
-
887
- instance.navigateDatasource(true);
888
- expect(instance.datasource.currentRow).toEqual(instance.datasource.data[0]);
889
-
890
- dispatchEvent('arrowright', {}); // arrow right
891
- expect(instance.datasource.currentRow).toEqual(instance.datasource.data[0]);
892
-
893
- dispatchEvent('arrowleft', {}); // arrow left
894
- expect(instance.datasource.currentRow).toEqual(instance.datasource.data[0]);
895
-
896
- httpSpy.mockReset();
897
- });
898
-
899
- it('should navigate through grid lines using keys when grouped', async () => {
900
- const httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
901
- data: {
902
- data: [],
903
- pagination: {
904
- page: 1,
905
- total: 0,
906
- limit: 10,
907
- },
908
- },
909
- }));
910
- const instance = new TekGrid({
911
- name: 'grid_navigation_2',
912
- component: 'TekGrid',
913
- columns: [
914
- {
915
- name: 'id',
916
- label: 'id',
917
- },
918
- {
919
- name: 'name',
920
- label: 'name',
921
- },
922
- {
923
- name: 'department',
924
- label: 'department',
925
- grouped: true,
926
- },
927
- ],
928
- datasource: {
929
- uniqueKey: 'id',
930
- order: ['name.asc'],
931
- data: [
932
- { id: 2, name: 'employee 2', department: 1 },
933
- { id: 5, name: 'employee 5', department: 2 },
934
- { id: 4, name: 'employee 4', department: 2 },
935
- { id: 3, name: 'employee 3', department: 1 },
936
- { id: 1, name: 'employee 1', department: 1 },
937
- ],
938
- },
939
- });
940
-
941
- const elem = document.createElement('DIV');
942
- (KeyMap as any).isElementActive = () => true;
943
- instance.onCreated();
944
- instance.onMounted(elem);
945
-
946
- await flushPromises();
947
-
948
- dispatchEvent('arrowdown', {}); // arrow down
949
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[0]);
950
-
951
- dispatchEvent('arrowdown', {}); // arrow down
952
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[4]);
953
-
954
- dispatchEvent('arrowdown', {}); // arrow down
955
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[4]);
956
-
957
- dispatchEvent('arrowup', {}); // arrow up
958
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[0]);
959
-
960
- dispatchEvent('arrowup', {}); // arrow up
961
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[0]);
962
-
963
- instance.datasource.currentRow = {};
964
- dispatchEvent('arrowup', {}); // arrow up
965
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[4]);
966
-
967
- [instance.datasource.currentRow] = instance.groupedData;
968
- expect(instance.datasource.currentRow.groupOpened).toBeFalsy();
969
- dispatchEvent('arrowright', {}); // arrow right
970
- expect(instance.datasource.currentRow.groupOpened).toBeTruthy();
971
-
972
- dispatchEvent('arrowdown', {}); // arrow down
973
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[1]);
974
-
975
- dispatchEvent('arrowdown', {}); // arrow down
976
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[2]);
977
-
978
- dispatchEvent('arrowup', {}); // arrow up
979
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[1]);
980
-
981
- dispatchEvent('arrowleft', {}); // arrow left
982
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[0]);
983
-
984
- expect(instance.datasource.currentRow.groupOpened).toBeTruthy();
985
- dispatchEvent('arrowleft', {}); // arrow left
986
- expect(instance.datasource.currentRow.groupOpened).toBeFalsy();
987
-
988
- [, , , , instance.datasource.currentRow] = instance.groupedData;
989
- expect(instance.datasource.currentRow.groupOpened).toBeFalsy();
990
- dispatchEvent('arrowright', {}); // arrow right
991
- expect(instance.datasource.currentRow.groupOpened).toBeTruthy();
992
-
993
- dispatchEvent('arrowdown', {}); // arrow down
994
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[5]);
995
-
996
- dispatchEvent('arrowdown', {}); // arrow down
997
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[6]);
998
-
999
- dispatchEvent('arrowdown', {}); // arrow down
1000
- expect(instance.datasource.currentRow).toEqual(instance.groupedData[6]);
1001
-
1002
- httpSpy.mockReset();
1003
- });
1004
-
1005
- it('should open filter modal input with keymap', async () => {
1006
- const instance = new TekGrid({
1007
- name: 'grid_navigation_2',
1008
- component: 'TekGrid',
1009
- filterButton: true,
1010
- });
1011
- const elem = document.createElement('DIV');
1012
-
1013
- (KeyMap as any).isElementActive = () => true;
1014
-
1015
- instance.onMounted(elem);
1016
- await flushPromises();
1017
-
1018
- const method = jest.fn();
1019
-
1020
- const filterButton = new TekGridFilterButton({
1021
- name: 'grid_navigation_2_filterButton',
1022
- component: 'TekGridFilterButton',
1023
- grid: instance,
1024
- events: {
1025
- click: method,
1026
- },
1027
- });
1028
-
1029
- const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => filterButton);
1030
- filterButton.onCreated();
1031
-
1032
- dispatchEvent('l', { ctrlKey: true }); // ctrl+l keydown
1033
- await flushPromises();
1034
-
1035
- instance.onBeforeDestroy();
1036
- await flushPromises();
1037
-
1038
- expect(method).toHaveBeenCalled();
1039
- (KeyMap as any).isElementActive = () => false;
1040
- spyMetadata.mockReset();
1041
- });
1042
-
1043
- it('should open action dropdown with keymap', async () => {
1044
- const instance = new TekGrid({
1045
- name: 'grid_navigation_2',
1046
- component: 'TekGrid',
1047
- });
1048
- const elem = document.createElement('DIV');
1049
-
1050
- (KeyMap as any).isElementActive = () => true;
1051
-
1052
- instance.onMounted(elem);
1053
- await flushPromises();
1054
-
1055
- const instanceDropDown = new Dropdown({
1056
- name: 'grid_navigation_2_actions_dropdown',
1057
- component: 'ZdDropdown',
1058
- activator: {
1059
- name: 'grid_navigation_2_actionsButton',
1060
- component: 'ZdButton',
1061
- },
1062
- children: [],
1063
- });
1064
- const instanceDropDownSpy = jest.spyOn(instanceDropDown, 'setFocus').mockImplementation(() => true);
1065
- const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => instanceDropDown);
1066
-
1067
- dispatchEvent('enter', { ctrlKey: true, enterKey: true }); // ctrl + enter keydown
1068
- await flushPromises();
1069
-
1070
- expect(spyMetadata).toHaveBeenCalled();
1071
- expect(instanceDropDownSpy).toHaveBeenCalled();
1072
-
1073
- instance.onBeforeDestroy();
1074
- await flushPromises();
1075
-
1076
- (KeyMap as any).isElementActive = () => false;
1077
- spyMetadata.mockReset();
1078
- instanceDropDownSpy.mockReset();
1079
- });
1080
-
1081
- it('should focus in searchInput with keymap', async () => {
1082
- const instance = new TekGrid({
1083
- name: 'grid_navigation_3',
1084
- component: 'TekGrid',
1085
- });
1086
- const elem = document.createElement('DIV');
1087
-
1088
- (KeyMap as any).isElementActive = () => true;
1089
- instance.onMounted(elem);
1090
- await flushPromises();
1091
-
1092
- const searchInput = new Search({
1093
- name: 'grid_navigation_3_gridSearch',
1094
- component: 'ZdSearch',
1095
- parent: instance,
1096
- });
1097
-
1098
- const spySetFocus = jest.spyOn(searchInput, 'setFocus').mockImplementation(() => true);
1099
- const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => searchInput);
1100
-
1101
- dispatchEvent('f', { ctrlKey: true }); // ctrl+f keydown
1102
- await flushPromises();
1103
-
1104
- expect(spySetFocus).toHaveBeenCalled();
1105
-
1106
- instance.onBeforeDestroy();
1107
- await flushPromises();
1108
-
1109
- (KeyMap as any).isElementActive = () => false;
1110
- spySetFocus.mockReset();
1111
- spyMetadata.mockReset();
1112
- });
1113
-
1114
- it('when using cellSelection, should use viewNavigate', async () => {
1115
- const instance = new TekGrid({
1116
- name: 'grid_navigation_1',
1117
- component: 'TekGrid',
1118
- cellSelection: true,
1119
- columns: [
1120
- {
1121
- name: 'name',
1122
- grouped: true,
1123
- },
1124
- ],
1125
- });
1126
-
1127
- const elem = document.createElement('DIV');
1128
- (KeyMap as any).isElementActive = () => true;
1129
- instance.onCreated();
1130
- instance.onMounted(elem);
1131
-
1132
- await flushPromises();
1133
- const spy = jest.fn();
1134
- instance.setViewNavigate(spy);
1135
-
1136
- instance.navigateUp();
1137
-
1138
- expect(spy).toHaveBeenLastCalledWith('up');
1139
-
1140
- instance.navigateDown();
1141
-
1142
- expect(spy).toHaveBeenLastCalledWith('down');
1143
- });
1144
- });
1145
-
1146
- describe('reloadClick()', () => {
1147
- it('should call event', () => {
1148
- const instance = new TekGrid({
1149
- name: 'grid_reloadClick1',
1150
- component: 'TekGrid',
1151
- });
1152
-
1153
- const spy = jest.spyOn(instance, 'reload');
1154
- instance.onCreated();
1155
- const notEditingTag = instance.toolbarSlot[2];
1156
- if (notEditingTag.children) {
1157
- const notEditingSpan = notEditingTag.children[0];
1158
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 3) {
1159
- const reloadTooltip = notEditingSpan.children[3];
1160
- if (reloadTooltip && reloadTooltip.children && reloadTooltip.children.length > 0) {
1161
- const reloadButton = reloadTooltip.children[0];
1162
- const button = new Button(reloadButton);
1163
- const event = new Event('click');
1164
- button.click(event, {} as HTMLElement);
1165
- expect(spy).toBeCalled();
1166
- }
1167
- }
1168
- }
1169
- spy.mockReset();
1170
- });
1171
-
1172
- it('should call beforeReload event', () => {
1173
- const beforeSpy = jest.fn();
1174
- const instance = new TekGrid({
1175
- name: 'grid_reloadClick2',
1176
- component: 'TekGrid',
1177
- events: {
1178
- beforeReload: beforeSpy,
1179
- },
1180
- });
1181
-
1182
- const spy = jest.spyOn(instance, 'reload');
1183
- instance.onCreated();
1184
- const notEditingTag = instance.toolbarSlot[2];
1185
- if (notEditingTag.children) {
1186
- const notEditingSpan = notEditingTag.children[0];
1187
- if (notEditingSpan && notEditingSpan.children && notEditingSpan.children.length > 3) {
1188
- const reloadTooltip = notEditingSpan.children[3];
1189
- if (reloadTooltip && reloadTooltip.children && reloadTooltip.children.length > 0) {
1190
- const reloadButton = reloadTooltip.children[0];
1191
- const button = new Button(reloadButton);
1192
- setClick(button, { defaultPrevented: true });
1193
- expect(beforeSpy).toBeCalled();
1194
- expect(spy).not.toBeCalled();
1195
- }
1196
- }
1197
- }
1198
- spy.mockReset();
1199
- });
1200
- });
1201
-
1202
- describe('grouping', () => {
1203
- it('should group data based on columns that have grouped property', async () => {
1204
- const instance = createAndMount({
1205
- name: 'grid_grouping_1',
1206
- component: 'TekGrid',
1207
- columns: [
1208
- {
1209
- name: 'id',
1210
- label: 'id',
1211
- },
1212
- {
1213
- name: 'name',
1214
- label: 'name',
1215
- },
1216
- {
1217
- name: 'department',
1218
- label: 'department',
1219
- grouped: true,
1220
- },
1221
- ],
1222
- datasource: {
1223
- uniqueKey: 'id',
1224
- order: ['name.asc'],
1225
- data: [
1226
- { id: 2, name: 'employee 2', department: 1 },
1227
- { id: 5, name: 'employee 5', department: 2 },
1228
- { id: 4, name: 'employee 4', department: 2 },
1229
- { id: 3, name: 'employee 3', department: 1 },
1230
- { id: 1, name: 'employee 1', department: 1 },
1231
- ],
1232
- },
1233
- });
1234
-
1235
- await flushPromises();
1236
-
1237
- instance.getCellWithAppliedConditions(instance.columns, instance.datasource.data[0]);
1238
- expect(instance.datasource.data.length).toBe(5);
1239
- expect(instance.datasource.order).toEqual(['department.asc', 'name.asc']);
1240
- expect(instance.groupedData.length).toBe(7);
1241
- expect(instance.groupedData[0].group).toBeTruthy();
1242
- expect(instance.groupedData[0].groupLabel).toBe('department');
1243
- expect(instance.groupedData[0].groupValue).toBe(1);
1244
- expect(instance.groupedData[1].groupHeaders.length).toBe(1);
1245
- expect(instance.groupedData[1].name).toBe('employee 1');
1246
- });
1247
-
1248
- it('should call debounced updateGroupData', () => {
1249
- const spy = jest.spyOn(TekGrid.prototype, 'updateGroupedData');
1250
- const instance = createAndMount({
1251
- name: 'grid_grouping_1',
1252
- component: 'TekGrid',
1253
- columns: [
1254
- {
1255
- name: 'id',
1256
- label: 'id',
1257
- },
1258
- {
1259
- name: 'name',
1260
- label: 'name',
1261
- },
1262
- {
1263
- name: 'department',
1264
- label: 'department',
1265
- },
1266
- ],
1267
- datasource: {
1268
- uniqueKey: 'id',
1269
- order: ['name.asc'],
1270
- data: [
1271
- { id: 2, name: 'employee 2', department: 1 },
1272
- { id: 5, name: 'employee 5', department: 2 },
1273
- { id: 4, name: 'employee 4', department: 2 },
1274
- { id: 3, name: 'employee 3', department: 1 },
1275
- { id: 1, name: 'employee 1', department: 1 },
1276
- ],
1277
- },
1278
- });
1279
-
1280
- jest.runAllTimers();
1281
-
1282
- instance.columns[2].grouped = true;
1283
-
1284
- expect(spy).toHaveBeenCalled();
1285
-
1286
- spy.mockClear();
1287
- });
1288
-
1289
- it('should group data with aggregations', async () => {
1290
- const instance = createAndMount({
1291
- name: 'grid_aggregate_1',
1292
- component: 'TekGrid',
1293
- columns: [
1294
- {
1295
- name: 'id',
1296
- label: 'id',
1297
- aggregation: 'COUNT',
1298
- },
1299
- {
1300
- name: 'name',
1301
- label: 'name',
1302
- },
1303
- {
1304
- name: 'department',
1305
- label: 'department',
1306
- grouped: true,
1307
- },
1308
- ],
1309
- datasource: {
1310
- uniqueKey: 'id',
1311
- order: ['name.asc'],
1312
- data: [
1313
- { id: 2, name: 'employee 2', department: 1 },
1314
- { id: 5, name: 'employee 5', department: 2 },
1315
- { id: 4, name: 'employee 4', department: 2 },
1316
- { id: 3, name: 'employee 3', department: 1 },
1317
- { id: 1, name: 'employee 1', department: 1 },
1318
- ],
1319
- },
1320
- });
1321
-
1322
- await flushPromises();
1323
-
1324
- instance.getCellWithAppliedConditions(instance.columns, instance.datasource.data[0]);
1325
- expect(instance.datasource.data.length).toBe(5);
1326
- expect(instance.datasource.order).toEqual(['department.asc', 'name.asc']);
1327
- expect(instance.groupedData.length).toBe(10);
1328
- expect(instance.groupedData[0].group).toBeTruthy();
1329
- expect(instance.groupedData[0].groupLabel).toBe('department');
1330
- expect(instance.groupedData[0].groupValue).toBe(1);
1331
- expect(instance.groupedData[1].groupHeaders.length).toBe(1);
1332
- expect(instance.groupedData[1].name).toBe('employee 1');
1333
- expect(instance.groupedData[4].groupFooter).toBeTruthy();
1334
- expect(instance.groupedData[4].id).toBe(3);
1335
- expect(instance.groupedData[9].groupSummary).toBeTruthy();
1336
- expect(instance.groupedData[9].id).toBe(5);
1337
- });
1338
-
1339
- it('should group data with multiple groups', async () => {
1340
- let viewUpdateScrollDataCalled = false;
1341
-
1342
- const instance = createAndMount({
1343
- name: 'grid_grouping_2',
1344
- component: 'TekGrid',
1345
- virtualScroll: true,
1346
- columns: [
1347
- {
1348
- name: 'id',
1349
- label: 'id',
1350
- },
1351
- {
1352
- name: 'name',
1353
- label: 'name',
1354
- },
1355
- {
1356
- name: 'department',
1357
- label: 'department',
1358
- grouped: true,
1359
- },
1360
- {
1361
- name: 'boss',
1362
- label: 'boss',
1363
- groupLabelForEmptyValue: 'boss',
1364
- grouped: true,
1365
- },
1366
- ],
1367
- datasource: {
1368
- uniqueKey: 'id',
1369
- order: ['name.asc'],
1370
- data: [
1371
- {
1372
- id: 2, name: 'employee 2', department: 1, boss: 1,
1373
- },
1374
- {
1375
- id: 5, name: 'employee 5', department: 2, boss: 4,
1376
- },
1377
- { id: 4, name: 'employee 4', department: 2 },
1378
- {
1379
- id: 3, name: 'employee 3', department: 1, boss: 1,
1380
- },
1381
- { id: 1, name: 'employee 1', department: 1 },
1382
- ],
1383
- },
1384
- });
1385
-
1386
- await flushPromises();
1387
-
1388
- expect(instance.datasource.data.length).toBe(5);
1389
- expect(instance.datasource.data[0].groupHeaders).toBeUndefined();
1390
- expect(instance.datasource.order).toEqual(['department.asc', 'boss.asc', 'name.asc']);
1391
- expect(instance.groupedData.length).toBe(11);
1392
- expect(instance.groupedData[0].group).toBeTruthy();
1393
- expect(instance.groupedData[0].groupLabel).toBe('department');
1394
- expect(instance.groupedData[0].groupValue).toBe(1);
1395
- expect(instance.groupedData[1].group).toBeTruthy();
1396
- expect(instance.groupedData[1].groupLabel).toBe('boss');
1397
- expect(instance.groupedData[1].groupValue).toBe('boss');
1398
- expect(instance.groupedData[2].groupHeaders.length).toBe(2);
1399
- expect(instance.groupedData[2].name).toBe('employee 1');
1400
- expect(instance.groupedData[3].group).toBeTruthy();
1401
- expect(instance.groupedData[3].groupLabel).toBe('boss');
1402
- expect(instance.groupedData[3].groupValue).toBe(1);
1403
-
1404
- expect(instance.isItemVisible(instance.groupedData[2])).toBeFalsy();
1405
- instance.openGroup(instance.groupedData[0]);
1406
- expect(instance.isItemVisible(instance.groupedData[2])).toBeFalsy();
1407
- expect(viewUpdateScrollDataCalled).toBeFalsy();
1408
-
1409
- instance.viewUpdateScrollData = () => { viewUpdateScrollDataCalled = true; };
1410
- instance.openGroup(instance.groupedData[1]);
1411
- expect(instance.isItemVisible(instance.groupedData[2])).toBeTruthy();
1412
- expect(viewUpdateScrollDataCalled).toBeTruthy();
1413
- });
1414
-
1415
- it('should group data with multiple groups and aggregation', async () => {
1416
- const instance = createAndMount({
1417
- name: 'grid_aggredate_2',
1418
- component: 'TekGrid',
1419
- showSummaryTotal: false,
1420
- columns: [
1421
- {
1422
- name: 'id',
1423
- label: 'id',
1424
- aggregation: 'COUNT',
1425
- },
1426
- {
1427
- name: 'name',
1428
- label: 'name',
1429
- },
1430
- {
1431
- name: 'salary',
1432
- label: 'salary',
1433
- aggregation: 'SUM',
1434
- },
1435
- {
1436
- name: 'department',
1437
- label: 'department',
1438
- grouped: true,
1439
- },
1440
- {
1441
- name: 'boss',
1442
- label: 'boss',
1443
- groupLabelForEmptyValue: 'boss',
1444
- grouped: true,
1445
- },
1446
- ],
1447
- datasource: {
1448
- uniqueKey: 'id',
1449
- order: ['name.asc'],
1450
- data: [
1451
- {
1452
- id: 2, name: 'employee 2', salary: 10, department: 1, boss: 1,
1453
- },
1454
- {
1455
- id: 5, name: 'employee 5', salary: 10, department: 2, boss: 4,
1456
- },
1457
- {
1458
- id: 4, name: 'employee 4', salary: 10, department: 2,
1459
- },
1460
- {
1461
- id: 3, name: 'employee 3', salary: 10, department: 1, boss: 1,
1462
- },
1463
- {
1464
- id: 1, name: 'employee 1', salary: 10, department: 1,
1465
- },
1466
- ],
1467
- },
1468
- });
1469
-
1470
- await flushPromises();
1471
-
1472
- expect(instance.datasource.data.length).toBe(5);
1473
- expect(instance.datasource.data[0].groupHeaders).toBeUndefined();
1474
- expect(instance.datasource.order).toEqual(['department.asc', 'boss.asc', 'name.asc']);
1475
- expect(instance.groupedData.length).toBe(17);
1476
- expect(instance.groupedData[0].group).toBeTruthy();
1477
- expect(instance.groupedData[0].groupLabel).toBe('department');
1478
- expect(instance.groupedData[0].groupValue).toBe(1);
1479
- expect(instance.groupedData[1].group).toBeTruthy();
1480
- expect(instance.groupedData[1].groupLabel).toBe('boss');
1481
- expect(instance.groupedData[1].groupValue).toBe('boss');
1482
- expect(instance.groupedData[2].groupHeaders.length).toBe(2);
1483
- expect(instance.groupedData[2].name).toBe('employee 1');
1484
- expect(instance.groupedData[3].groupFooter).toBeTruthy();
1485
- expect(instance.groupedData[3].id).toBe(1);
1486
- expect(instance.groupedData[3].salary).toBe(10);
1487
- expect(instance.groupedData[4].group).toBeTruthy();
1488
- expect(instance.groupedData[4].groupLabel).toBe('boss');
1489
- expect(instance.groupedData[4].groupValue).toBe(1);
1490
- expect(instance.groupedData[7].groupFooter).toBeTruthy();
1491
- expect(instance.groupedData[7].id).toBe(2);
1492
- expect(instance.groupedData[7].salary).toBe(20);
1493
- expect(instance.groupedData[8].groupFooter).toBeTruthy();
1494
- expect(instance.groupedData[8].id).toBe(3);
1495
- expect(instance.groupedData[8].salary).toBe(30);
1496
- });
1497
-
1498
- it('should aggregate even without groups', async () => {
1499
- const instance = createAndMount({
1500
- name: 'grid_aggredate_3',
1501
- component: 'TekGrid',
1502
- showSummaryTotal: true,
1503
- columns: [
1504
- {
1505
- name: 'id',
1506
- label: 'id',
1507
- aggregation: 'COUNT',
1508
- },
1509
- {
1510
- name: 'name',
1511
- label: 'name',
1512
- },
1513
- {
1514
- name: 'salary',
1515
- label: 'salary',
1516
- aggregation: 'SUM',
1517
- },
1518
- {
1519
- name: 'department',
1520
- label: 'department',
1521
- },
1522
- {
1523
- name: 'boss',
1524
- label: 'boss',
1525
- groupLabelForEmptyValue: 'boss',
1526
- },
1527
- ],
1528
- datasource: {
1529
- uniqueKey: 'id',
1530
- order: ['name.asc'],
1531
- data: [
1532
- {
1533
- id: 2, name: 'employee 2', salary: 10, department: 1, boss: 1,
1534
- },
1535
- {
1536
- id: 5, name: 'employee 5', salary: 10, department: 2, boss: 4,
1537
- },
1538
- {
1539
- id: 4, name: 'employee 4', salary: 10, department: 2,
1540
- },
1541
- {
1542
- id: 3, name: 'employee 3', salary: 10, department: 1, boss: 1,
1543
- },
1544
- {
1545
- id: 1, name: 'employee 1', salary: 10, department: 1,
1546
- },
1547
- ],
1548
- },
1549
- });
1550
-
1551
- await flushPromises();
1552
-
1553
- expect(instance.datasource.data.length).toBe(5);
1554
- expect(instance.groupedData.length).toBe(6);
1555
- expect(instance.groupedData[5].groupSummary).toBeTruthy();
1556
- expect(instance.groupedData[5].salary).toBe(50);
1557
- });
1558
-
1559
- it('should aggregate with different functions', async () => {
1560
- const instance = createAndMount({
1561
- name: 'grid_aggregate_4',
1562
- component: 'TekGrid',
1563
- showSummaryTotal: true,
1564
- columns: [
1565
- {
1566
- name: 'id',
1567
- label: 'id',
1568
- },
1569
- {
1570
- name: 'column_sum',
1571
- label: 'sum',
1572
- aggregation: 'SUM',
1573
- },
1574
- {
1575
- name: 'column_min',
1576
- label: 'min',
1577
- aggregation: 'MIN',
1578
- },
1579
- {
1580
- name: 'column_max',
1581
- label: 'max',
1582
- aggregation: 'MAX',
1583
- },
1584
- {
1585
- name: 'column_avg',
1586
- label: 'avg',
1587
- aggregation: 'AVG',
1588
- },
1589
- {
1590
- name: 'column_count',
1591
- label: 'count',
1592
- aggregation: 'COUNT',
1593
- },
1594
- {
1595
- name: 'department',
1596
- label: 'department',
1597
- grouped: true,
1598
- },
1599
- ],
1600
- datasource: {
1601
- uniqueKey: 'id',
1602
- data: [
1603
- {
1604
- id: 1, department: 1, column_sum: 10, column_min: 10, column_max: '10', column_avg: 10, column_count: 10,
1605
- },
1606
- {
1607
- id: 2, department: 1, column_sum: 20, column_min: 20, column_max: '20', column_avg: 20, column_count: 20,
1608
- },
1609
- {
1610
- id: 3, department: 1, column_sum: 100, column_min: 100, column_max: '100', column_avg: 100, column_count: 100,
1611
- },
1612
- {
1613
- id: 4, department: 1, column_min: 5, column_max: '5', column_avg: 5,
1614
- },
1615
- ],
1616
- },
1617
- });
1618
-
1619
- await flushPromises();
1620
-
1621
- expect(instance.datasource.data.length).toBe(4);
1622
- expect(instance.groupedData.length).toBe(7);
1623
- expect(instance.groupedData[6].groupSummary).toBeTruthy();
1624
- expect(instance.groupedData[6].column_sum).toBe(130);
1625
- expect(instance.groupedData[6].column_min).toBe(5);
1626
- expect(instance.groupedData[6].column_max).toBe('5');
1627
- expect(instance.groupedData[6].column_avg).toBe(33.75);
1628
- expect(instance.groupedData[6].column_count).toBe(3);
1629
- });
1630
-
1631
- it('should aggregate using calcSummary event', async () => {
1632
- const doCalcSummary = ({ summary, rowValue }: IEventParam<any>) => {
1633
- summary.sum = `${(summary.sum || '')}${rowValue}`;
1634
- summary.avg = `${summary.sum}/${summary.count}`;
1635
- summary.max = summary.max === undefined || summary.max < rowValue ? rowValue : summary.max;
1636
- summary.min = summary.min === undefined || summary.min > rowValue ? rowValue : summary.min;
1637
- };
1638
-
1639
- const instance = createAndMount({
1640
- name: 'grid_aggregate_5',
1641
- component: 'TekGrid',
1642
- showSummaryTotal: true,
1643
- events: {
1644
- calcSummary: doCalcSummary,
1645
- },
1646
- columns: [
1647
- {
1648
- name: 'id',
1649
- label: 'id',
1650
- },
1651
- {
1652
- name: 'column_sum',
1653
- label: 'sum',
1654
- aggregation: 'SUM',
1655
- },
1656
- {
1657
- name: 'column_min',
1658
- label: 'min',
1659
- aggregation: 'MIN',
1660
- },
1661
- {
1662
- name: 'column_max',
1663
- label: 'max',
1664
- aggregation: 'MAX',
1665
- },
1666
- {
1667
- name: 'column_avg',
1668
- label: 'avg',
1669
- aggregation: 'AVG',
1670
- },
1671
- {
1672
- name: 'column_count',
1673
- label: 'count',
1674
- aggregation: 'COUNT',
1675
- },
1676
- {
1677
- name: 'department',
1678
- label: 'department',
1679
- grouped: true,
1680
- },
1681
- ],
1682
- datasource: {
1683
- uniqueKey: 'id',
1684
- data: [
1685
- {
1686
- id: 1, department: 1, column_sum: 'd', column_min: 'd', column_max: 'd', column_avg: 'd', column_count: 'd',
1687
- },
1688
- {
1689
- id: 2, department: 1, column_sum: 'b', column_min: 'b', column_max: 'b', column_avg: 'b', column_count: 'b',
1690
- },
1691
- {
1692
- id: 3, department: 1, column_sum: 'a', column_min: 'a', column_max: 'a', column_avg: 'a', column_count: 'a',
1693
- },
1694
- {
1695
- id: 4, department: 1, column_sum: 'c', column_min: 'c', column_max: 'c', column_avg: 'c', column_count: 'c',
1696
- },
1697
- ],
1698
- },
1699
- });
1700
-
1701
- await flushPromises();
1702
-
1703
- expect(instance.datasource.data.length).toBe(4);
1704
- expect(instance.groupedData.length).toBe(7);
1705
- expect(instance.groupedData[6].groupSummary).toBeTruthy();
1706
- expect(instance.groupedData[6].column_sum).toBe('dbac');
1707
- expect(instance.groupedData[6].column_min).toBe('a');
1708
- expect(instance.groupedData[6].column_max).toBe('d');
1709
- expect(instance.groupedData[6].column_avg).toBe('dbac/4');
1710
- expect(instance.groupedData[6].column_count).toBe(4);
1711
- });
1712
-
1713
- it('should group data on rest datasource with lazyLoad', async () => {
1714
- const httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
1715
- data: {
1716
- data: [
1717
- { id: 1, name: 'employee 1', department: 1 },
1718
- { id: 2, name: 'employee 2', department: 1 },
1719
- { id: 3, name: 'employee 3', department: 1 },
1720
- { id: 4, name: 'employee 4', department: 2 },
1721
- { id: 5, name: 'employee 5', department: 2 },
1722
- ],
1723
- pagination: {
1724
- page: 1,
1725
- total: 5,
1726
- limit: 15,
1727
- },
1728
- },
1729
- }));
1730
-
1731
- const instance = createAndMount({
1732
- name: 'grid_grouping_3',
1733
- component: 'TekGrid',
1734
- columns: [
1735
- {
1736
- name: 'id',
1737
- label: 'id',
1738
- },
1739
- {
1740
- name: 'name',
1741
- label: 'name',
1742
- },
1743
- {
1744
- name: 'department',
1745
- label: 'department',
1746
- grouped: true,
1747
- },
1748
- ],
1749
- datasource: {
1750
- uniqueKey: 'id',
1751
- order: ['name.asc'],
1752
- type: 'rest',
1753
- route: '/zeedhi',
1754
- lazyLoad: true,
1755
- },
1756
- });
1757
-
1758
- await flushPromises();
1759
-
1760
- expect(instance.datasource.data.length).toBe(0);
1761
-
1762
- await instance.datasource.get();
1763
-
1764
- expect(instance.datasource.data.length).toBe(5);
1765
- expect(instance.groupedData.length).toBe(7);
1766
-
1767
- httpSpy.mockReset();
1768
- });
1769
-
1770
- it('editedRows should return cleaned rows', async () => {
1771
- const instance = new TekGrid({
1772
- name: 'grid_grouping_4',
1773
- component: 'TekGrid',
1774
- columns: [
1775
- {
1776
- name: 'id',
1777
- label: 'id',
1778
- },
1779
- {
1780
- name: 'name',
1781
- label: 'name',
1782
- editable: true,
1783
- },
1784
- {
1785
- name: 'department',
1786
- label: 'department',
1787
- grouped: true,
1788
- },
1789
- ],
1790
- datasource: {
1791
- uniqueKey: 'id',
1792
- order: ['name.asc'],
1793
- data: [
1794
- { id: 2, name: 'employee 2', department: 1 },
1795
- { id: 5, name: 'employee 5', department: 2 },
1796
- { id: 4, name: 'employee 4', department: 2 },
1797
- { id: 3, name: 'employee 3', department: 1 },
1798
- { id: 1, name: 'employee 1', department: 1 },
1799
- ],
1800
- },
1801
- });
1802
-
1803
- await flushPromises();
1804
-
1805
- instance.editing = true;
1806
- instance.updateRow({ id: 1, name: 'employee 111', department: 1 });
1807
- const editedRows = instance.getEditedRows();
1808
- expect(editedRows.length).toBe(1);
1809
- expect(editedRows[0].groupHeaders).toBeUndefined();
1810
- });
1811
-
1812
- it('should do nothing if datasource is empty', async () => {
1813
- const instance = new TekGrid({
1814
- name: 'grid_grouping_5',
1815
- component: 'TekGrid',
1816
- columns: [
1817
- {
1818
- name: 'id',
1819
- label: 'id',
1820
- },
1821
- {
1822
- name: 'name',
1823
- label: 'name',
1824
- editable: true,
1825
- },
1826
- {
1827
- name: 'department',
1828
- label: 'department',
1829
- grouped: true,
1830
- },
1831
- ],
1832
- datasource: {
1833
- uniqueKey: 'id',
1834
- data: [],
1835
- },
1836
- });
1837
-
1838
- await flushPromises();
1839
-
1840
- expect(instance.datasource.data.length).toBe(0);
1841
- expect(instance.groupedData.length).toBe(0);
1842
- });
1843
-
1844
- it('should call view update methods', async () => {
1845
- let viewUpdateScrollDataCalled = false;
1846
- let viewUpdateFixedColumnsCalled = false;
1847
-
1848
- const instance = createAndMount({
1849
- name: 'grid_grouping_6',
1850
- component: 'TekGrid',
1851
- virtualScroll: true,
1852
- columns: [
1853
- {
1854
- name: 'id',
1855
- label: 'id',
1856
- },
1857
- {
1858
- name: 'name',
1859
- label: 'name',
1860
- },
1861
- {
1862
- name: 'department',
1863
- label: 'department',
1864
- grouped: true,
1865
- },
1866
- ],
1867
- datasource: {
1868
- uniqueKey: 'id',
1869
- order: ['name.asc'],
1870
- data: [
1871
- { id: 2, name: 'employee 2', department: 1 },
1872
- { id: 5, name: 'employee 5', department: 2 },
1873
- { id: 4, name: 'employee 4', department: 2 },
1874
- { id: 3, name: 'employee 3', department: 1 },
1875
- { id: 1, name: 'employee 1', department: 1 },
1876
- ],
1877
- },
1878
- });
1879
- instance.viewUpdateScrollData = () => { viewUpdateScrollDataCalled = true; };
1880
- instance.viewUpdateFixedColumns = () => { viewUpdateFixedColumnsCalled = true; };
1881
-
1882
- await flushPromises();
1883
-
1884
- expect(viewUpdateScrollDataCalled).toBeTruthy();
1885
- expect(viewUpdateFixedColumnsCalled).toBeTruthy();
1886
- });
1887
-
1888
- it('should call debounced updateGroupData', () => {
1889
- const spy = jest.spyOn(TekGrid.prototype, 'updateGroupedData');
1890
- const instance = new TekGrid({
1891
- name: 'grid_grouping_1',
1892
- component: 'TekGrid',
1893
- columns: [
1894
- {
1895
- name: 'id',
1896
- label: 'id',
1897
- },
1898
- {
1899
- name: 'name',
1900
- label: 'name',
1901
- },
1902
- {
1903
- name: 'department',
1904
- label: 'department',
1905
- grouped: true,
1906
- },
1907
- ],
1908
- datasource: {
1909
- uniqueKey: 'id',
1910
- order: ['name.asc'],
1911
- data: [
1912
- { id: 2, name: 'employee 2', department: 1 },
1913
- { id: 5, name: 'employee 5', department: 2 },
1914
- { id: 4, name: 'employee 4', department: 2 },
1915
- { id: 3, name: 'employee 3', department: 1 },
1916
- { id: 1, name: 'employee 1', department: 1 },
1917
- ],
1918
- },
1919
- });
1920
-
1921
- jest.runAllTimers();
1922
-
1923
- instance.columns[2].groupOpened = true;
1924
-
1925
- expect(spy).toHaveBeenCalled();
1926
-
1927
- spy.mockClear();
1928
- });
1929
- });
1930
-
1931
- describe('setSearch()', () => {
1932
- let baseProps: ITekGrid;
1933
- let httpSpy: jest.SpyInstance;
1934
-
1935
- beforeEach(() => {
1936
- httpSpy = jest.spyOn(Http, 'get').mockImplementation((url: string) => {
1937
- if (url === '/zeedhi') {
1938
- return Promise.resolve({
1939
- data: {
1940
- data: [
1941
- { id: 1, name: 'employee 1', department: 1 },
1942
- { id: 2, name: 'employee 2', department: 1 },
1943
- { id: 3, name: 'employee 3', department: 3 },
1944
- { id: 4, name: 'employee 4', department: 2 },
1945
- { id: 5, name: 'employee 5', department: 2 },
1946
- { id: 15, name: 'employee 15', department: 2 },
1947
- ],
1948
- pagination: {
1949
- page: 1,
1950
- total: 5,
1951
- limit: 15,
1952
- },
1953
- },
1954
- });
1955
- }
1956
-
1957
- if (url === '/departments') {
1958
- return Promise.resolve({
1959
- data: {
1960
- data: [
1961
- { id: 1, name: 'department 1' },
1962
- { id: 2, name: 'department 2' },
1963
- { id: 3, name: 'department 13' },
1964
- ],
1965
- pagination: {
1966
- page: 1,
1967
- total: 3,
1968
- limit: 15,
1969
- },
1970
- },
1971
- });
1972
- }
1973
-
1974
- if (url === '/grid') {
1975
- return Promise.resolve({
1976
- data: {
1977
- data: [
1978
- { id: 1, employee_id: 1, department: 1 },
1979
- { id: 2, employee_id: 2, department: 1 },
1980
- { id: 3, employee_id: 3, department: 3 },
1981
- ],
1982
- pagination: {
1983
- page: 1,
1984
- total: 5,
1985
- limit: 15,
1986
- },
1987
- },
1988
- });
1989
- }
1990
-
1991
- return Promise.reject();
1992
- });
1993
-
1994
- baseProps = {
1995
- name: 'grid_search',
1996
- component: 'TekGrid',
1997
- columns: [
1998
- {
1999
- name: 'id',
2000
- },
2001
- {
2002
- name: 'employee_id',
2003
- componentProps: {
2004
- name: 'ZdSelect',
2005
- dataText: [
2006
- 'name',
2007
- ],
2008
- datasource: {
2009
- type: 'tek-rest',
2010
- uniqueKey: 'id',
2011
- route: '/zeedhi',
2012
- lazyLoad: false,
2013
- },
2014
- },
2015
- },
2016
- ],
2017
- datasource: {
2018
- type: 'tek-rest',
2019
- lazyLoad: true,
2020
- route: '/grid',
2021
- },
2022
- };
2023
- });
2024
-
2025
- afterEach(() => {
2026
- httpSpy.mockReset();
2027
- });
2028
-
2029
- it('should set datasource search_join based on columns that have datasource', async () => {
2030
- const instance = new TekGrid(baseProps);
2031
-
2032
- instance.setSearch('1');
2033
- await flushPromises();
2034
-
2035
- expect(instance.datasource).toBeInstanceOf(TekRestDatasource);
2036
- expect((instance.datasource as TekRestDatasource).searchJoin).toEqual({ employee_id: [1, 15] });
2037
- expect(httpSpy).toHaveBeenCalledTimes(2);
2038
- });
2039
-
2040
- it('should set datasource search_join as empty when search is an empty string', async () => {
2041
- const instance = new TekGrid(baseProps);
2042
-
2043
- await instance.setSearch('');
2044
- expect(instance.datasource).toBeInstanceOf(TekRestDatasource);
2045
- expect((instance.datasource as TekRestDatasource).searchJoin).toEqual({});
2046
- expect(httpSpy).toHaveBeenCalledTimes(1);
2047
- });
2048
-
2049
- it('should search in multiple columns', async () => {
2050
- const instance = new TekGrid({
2051
- name: 'grid_search',
2052
- component: 'TekGrid',
2053
- columns: [
2054
- {
2055
- name: 'id',
2056
- },
2057
- {
2058
- name: 'employee_id',
2059
- componentProps: {
2060
- name: 'ZdSelect',
2061
- dataText: ['name'],
2062
- datasource: {
2063
- type: 'tek-rest',
2064
- uniqueKey: 'id',
2065
- route: '/zeedhi',
2066
- lazyLoad: false,
2067
- },
2068
- },
2069
- },
2070
- {
2071
- name: 'department_id',
2072
- componentProps: {
2073
- name: 'ZdSelect',
2074
- dataText: ['name'],
2075
- datasource: {
2076
- type: 'tek-rest',
2077
- uniqueKey: 'id',
2078
- route: '/departments',
2079
- lazyLoad: false,
2080
- },
2081
- },
2082
- },
2083
- ],
2084
- datasource: {
2085
- type: 'tek-rest',
2086
- lazyLoad: true,
2087
- route: '/grid',
2088
- },
2089
- });
2090
-
2091
- await instance.setSearch('1');
2092
- expect(instance.datasource).toBeInstanceOf(TekRestDatasource);
2093
- expect((instance.datasource as TekRestDatasource).searchJoin).toEqual({ employee_id: [1, 15], department_id: [1, 3] });
2094
- expect(httpSpy).toHaveBeenCalledTimes(3);
2095
- });
2096
-
2097
- it('should not make column search when datasource is not TekRestDatasource', async () => {
2098
- baseProps.datasource!.type = 'rest';
2099
- const instance = new TekGrid(baseProps);
2100
-
2101
- await instance.setSearch('1');
2102
- expect(instance.datasource).not.toBeInstanceOf(TekRestDatasource);
2103
- expect(httpSpy).toHaveBeenCalledTimes(1);
2104
- });
2105
-
2106
- it('when called, should change datasource page to 1 before searching', async () => {
2107
- const instance = new TekGrid({
2108
- name: 'iterable',
2109
- component: 'Iterable',
2110
- });
2111
- instance.datasource.page = 5;
2112
- await instance.setSearch('search value');
2113
-
2114
- expect(instance.datasource.page).toBe(1);
2115
- });
2116
- });
2117
-
2118
- describe('rowClick', () => {
2119
- it('rowClick method should call events.rowClick', () => {
2120
- const rowClick = jest.fn();
2121
-
2122
- const data = [
2123
- { id: '1', name: 'First' },
2124
- { id: '2', name: 'Second' },
2125
- { id: '3', name: 'Third' },
2126
- ];
2127
- const grid = new TekGrid({
2128
- name: 'grid',
2129
- component: 'TekGrid',
2130
- columns: [
2131
- { name: 'id' },
2132
- { name: 'name' },
2133
- ],
2134
- datasource: {
2135
- data,
2136
- },
2137
- events: {
2138
- rowClick,
2139
- },
2140
- });
2141
-
2142
- const event = new Event('click');
2143
- const element = document.createElement('div');
2144
- grid.rowClick(data[0], event, element);
2145
-
2146
- expect(rowClick).toHaveBeenCalledWith({
2147
- event,
2148
- element,
2149
- row: data[0],
2150
- component: grid,
2151
- });
2152
- expect(grid.datasource.currentRow).toEqual(data[0]);
2153
- });
2154
-
2155
- it('should not call events.rowClick if cellClick prevents it', async () => {
2156
- const rowClick = jest.fn();
2157
-
2158
- const data = [
2159
- { id: '1', name: 'First' },
2160
- { id: '2', name: 'Second' },
2161
- { id: '3', name: 'Third' },
2162
- ];
2163
- const grid = new TekGrid({
2164
- name: 'grid',
2165
- component: 'TekGrid',
2166
- columns: [
2167
- { name: 'id' },
2168
- { name: 'name' },
2169
- ],
2170
- datasource: {
2171
- data,
2172
- },
2173
- events: {
2174
- rowClick,
2175
- cellClick: () => true,
2176
- },
2177
- });
2178
- await flushPromises();
2179
-
2180
- const event = new Event('click');
2181
- const element = document.createElement('div');
2182
- grid.cellClick(data[0], grid.getColumn('id') as any, event, element);
2183
- grid.rowClick(data[0], event, element);
2184
-
2185
- expect(rowClick).not.toHaveBeenCalled();
2186
- expect(grid.datasource.currentRow).toEqual({});
2187
- });
2188
- });
2189
-
2190
- describe('rowDoubleClick', () => {
2191
- it('rowDoubleClick method should call events.rowDoubleClick', () => {
2192
- const rowDoubleClick = jest.fn();
2193
-
2194
- const data = [
2195
- { id: '1', name: 'First' },
2196
- { id: '2', name: 'Second' },
2197
- { id: '3', name: 'Third' },
2198
- ];
2199
- const grid = new TekGrid({
2200
- name: 'grid',
2201
- component: 'TekGrid',
2202
- columns: [
2203
- { name: 'id' },
2204
- { name: 'name' },
2205
- ],
2206
- datasource: {
2207
- data,
2208
- },
2209
- events: {
2210
- rowDoubleClick,
2211
- },
2212
- });
2213
-
2214
- const event = new Event('dblclick');
2215
- const element = document.createElement('div');
2216
- grid.rowDoubleClick(data[0], event, element);
2217
-
2218
- expect(rowDoubleClick).toHaveBeenCalledWith({
2219
- event,
2220
- element,
2221
- row: data[0],
2222
- component: grid,
2223
- });
2224
- expect(grid.datasource.currentRow).toEqual(data[0]);
2225
- });
2226
-
2227
- it('should not call events.rowDoubleClick if preventRowDoubleClick is true', () => {
2228
- const rowDoubleClick = jest.fn();
2229
- const cellDoubleClickHandler = jest.fn(() => true);
2230
-
2231
- const data = [
2232
- { id: '1', name: 'First' },
2233
- { id: '2', name: 'Second' },
2234
- { id: '3', name: 'Third' },
2235
- ];
2236
- const grid = new TekGrid({
2237
- name: 'grid',
2238
- component: 'TekGrid',
2239
- columns: [
2240
- { name: 'id' },
2241
- { name: 'name' },
2242
- ],
2243
- datasource: {
2244
- data,
2245
- },
2246
- events: {
2247
- rowDoubleClick,
2248
- cellDoubleClick: cellDoubleClickHandler,
2249
- },
2250
- });
2251
-
2252
- const event = new Event('dblclick');
2253
- const element = document.createElement('div');
2254
-
2255
- grid.cellDoubleClick(grid.groupedData[0], grid.getColumn('id') as any, event, element);
2256
- grid.rowDoubleClick(data[0], event, element);
2257
-
2258
- expect(rowDoubleClick).not.toHaveBeenCalled();
2259
- expect(grid.datasource.currentRow).toEqual({});
2260
- });
2261
- });
2262
-
2263
- describe('groupRowClick', () => {
2264
- it('groupRowClick method should call events.groupRowClick', async () => {
2265
- const groupRowClick = jest.fn();
2266
-
2267
- const data = [
2268
- { id: '1', name: 'First', department: 1 },
2269
- { id: '2', name: 'Second', department: 1 },
2270
- { id: '3', name: 'Third', department: 1 },
2271
- ];
2272
- const grid = new TekGrid({
2273
- name: 'grid',
2274
- component: 'TekGrid',
2275
- columns: [
2276
- { name: 'id' },
2277
- { name: 'name' },
2278
- { name: 'department', grouped: true },
2279
- ],
2280
- datasource: {
2281
- data,
2282
- },
2283
- events: {
2284
- groupRowClick,
2285
- },
2286
- });
2287
-
2288
- await flushPromises();
2289
-
2290
- const event = new Event('click');
2291
- const element = document.createElement('div');
2292
- grid.groupRowClick(grid.groupedData[0], event, element);
2293
-
2294
- expect(groupRowClick).toHaveBeenCalledWith({
2295
- event,
2296
- element,
2297
- row: grid.groupedData[0],
2298
- component: grid,
2299
- });
2300
- expect(grid.datasource.currentRow).toEqual({});
2301
- });
2302
-
2303
- it('should not call events.groupRowClick if cellClick prevents it', async () => {
2304
- const groupRowClick = jest.fn();
2305
-
2306
- const data = [
2307
- { id: '1', name: 'First', department: 1 },
2308
- { id: '2', name: 'Second', department: 1 },
2309
- { id: '3', name: 'Third', department: 1 },
2310
- ];
2311
- const grid = new TekGrid({
2312
- name: 'grid',
2313
- component: 'TekGrid',
2314
- columns: [
2315
- { name: 'id' },
2316
- { name: 'name' },
2317
- { name: 'department', grouped: true },
2318
- ],
2319
- datasource: {
2320
- data,
2321
- },
2322
- events: {
2323
- groupRowClick,
2324
- cellClick: () => true,
2325
- },
2326
- });
2327
- await flushPromises();
2328
-
2329
- const event = new Event('click');
2330
- const element = document.createElement('div');
2331
- grid.cellClick(grid.groupedData[0], grid.getColumn('id') as any, event, element);
2332
- grid.groupRowClick(grid.groupedData[0], event, element);
2333
-
2334
- expect(groupRowClick).not.toHaveBeenCalled();
2335
- });
2336
- });
2337
-
2338
- describe('groupRowDoubleClick', () => {
2339
- it('groupRowDoubleClick method should call events.groupRowDoubleClick', async () => {
2340
- const groupRowDoubleClick = jest.fn();
2341
-
2342
- const data = [
2343
- { id: '1', name: 'First', department: 1 },
2344
- { id: '2', name: 'Second', department: 1 },
2345
- { id: '3', name: 'Third', department: 1 },
2346
- ];
2347
- const grid = new TekGrid({
2348
- name: 'grid',
2349
- component: 'TekGrid',
2350
- columns: [
2351
- { name: 'id' },
2352
- { name: 'name' },
2353
- { name: 'department', grouped: true },
2354
- ],
2355
- datasource: {
2356
- data,
2357
- },
2358
- events: {
2359
- groupRowDoubleClick,
2360
- },
2361
- });
2362
-
2363
- await flushPromises();
2364
-
2365
- const event = new Event('dblclick');
2366
- const element = document.createElement('div');
2367
- grid.groupRowDoubleClick(grid.groupedData[0], event, element);
2368
-
2369
- expect(groupRowDoubleClick).toHaveBeenCalledWith({
2370
- event,
2371
- element,
2372
- row: grid.groupedData[0],
2373
- component: grid,
2374
- });
2375
- expect(grid.datasource.currentRow).toEqual({});
2376
- });
2377
-
2378
- it('should not call events.groupRowDoubleClick if cellDoubleClick prevents it', async () => {
2379
- const groupRowDoubleClick = jest.fn();
2380
- const cellDoubleClickHandler = jest.fn(() => true);
2381
-
2382
- const data = [
2383
- { id: '1', name: 'First', department: 1 },
2384
- { id: '2', name: 'Second', department: 1 },
2385
- { id: '3', name: 'Third', department: 1 },
2386
- ];
2387
- const grid = new TekGrid({
2388
- name: 'grid',
2389
- component: 'TekGrid',
2390
- columns: [
2391
- { name: 'id' },
2392
- { name: 'name' },
2393
- { name: 'department', grouped: true },
2394
- ],
2395
- datasource: {
2396
- data,
2397
- },
2398
- events: {
2399
- groupRowDoubleClick,
2400
- cellDoubleClick: cellDoubleClickHandler,
2401
- },
2402
- });
2403
-
2404
- await flushPromises();
2405
-
2406
- const event = new Event('dblclick');
2407
- const element = document.createElement('div');
2408
-
2409
- grid.cellDoubleClick(grid.groupedData[0], grid.getColumn('id') as any, event, element);
2410
- grid.groupRowDoubleClick(grid.groupedData[0], event, element);
2411
-
2412
- expect(groupRowDoubleClick).not.toHaveBeenCalled();
2413
- });
2414
- });
2415
-
2416
- describe('selectGroupClick', () => {
2417
- it('should trigger events', async () => {
2418
- const groupSelected = jest.fn();
2419
- const groupUnselected = jest.fn();
2420
-
2421
- const data = [
2422
- { id: '1', name: 'First', department: 1 },
2423
- { id: '2', name: 'Second', department: 1 },
2424
- { id: '3', name: 'Third', department: 1 },
2425
- ];
2426
- const grid = new TekGrid({
2427
- name: 'grid',
2428
- component: 'TekGrid',
2429
- columns: [
2430
- { name: 'id' },
2431
- { name: 'name' },
2432
- { name: 'department', grouped: true },
2433
- ],
2434
- datasource: {
2435
- data,
2436
- },
2437
- events: {
2438
- groupSelected,
2439
- groupUnselected,
2440
- },
2441
- });
2442
-
2443
- await flushPromises();
2444
-
2445
- const event = new Event('click');
2446
- const element = document.createElement('div');
2447
-
2448
- grid.selectGroupClick(grid.groupedData[0], true, event, element);
2449
- expect(groupSelected).toHaveBeenCalled();
2450
-
2451
- grid.selectGroupClick(grid.groupedData[0], false, event, element);
2452
- expect(groupUnselected).toHaveBeenCalled();
2453
- });
2454
- });
2455
-
2456
- describe('layoutOptions', () => {
2457
- it('should return layout options instance', async () => {
2458
- const grid = new TekGrid({
2459
- name: 'grid',
2460
- component: 'TekGrid',
2461
- showLayoutOptions: false,
2462
- });
2463
-
2464
- expect(grid.layoutOptions).toBeUndefined();
2465
-
2466
- grid.showLayoutOptions = true;
2467
- const layoutOptionsComp = { name: 'grid_layout_options' };
2468
- const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => layoutOptionsComp);
2469
- expect(grid.layoutOptions).toEqual(layoutOptionsComp);
2470
- spyMetadata.mockReset();
2471
- });
2472
- });
2473
-
2474
- describe('getAtomInstance', () => {
2475
- const createAtom = (name: string) => new Text({
2476
- name: `${name}_gridTitle`,
2477
- component: 'ZdText',
2478
- text: 'Employees',
2479
- });
2480
-
2481
- it('when called, should return the instance of an atom component', () => {
2482
- const instance = new TekGrid({ name: 'Grid', component: 'TekGrid' });
2483
- const atom = createAtom(instance.name);
2484
- const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => atom);
2485
-
2486
- const gridAtom = instance.getAtomInstance<Text>('TITLE');
2487
-
2488
- expect(gridAtom).toEqual(atom);
2489
- spyMetadata.mockReset();
2490
- });
2491
- });
2492
-
2493
- describe('getFilterInputs', () => {
2494
- const mockMetadata = (instance: TekGrid) => {
2495
- const inputs: IDictionary = {};
2496
- instance.columns.forEach((col) => {
2497
- if (!col.filterable || !col.filterProps) return;
2498
-
2499
- col.filterProps.forEach((filter: any, index: number) => {
2500
- const relation = filter.relation || 'AND';
2501
- const operation = filter.operation || 'CONTAINS';
2502
-
2503
- const name = `${instance.name}-filter-${relation}-${operation}-${col.name}-${index}`;
2504
- inputs[name] = new TextInput({
2505
- name,
2506
- ...filter.filterProps,
2507
- });
2508
- });
2509
- });
2510
- return jest.spyOn(Metadata, 'getInstance').mockImplementation((name: string) => inputs[name]);
2511
- };
2512
-
2513
- it('when called with null parameter, should return all the filter inputs', () => {
2514
- const spyModalService = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => (new Modal(modal)));
2515
- const instance = new TekGrid({
2516
- name: 'grid',
2517
- component: 'TekGrid',
2518
- datasource: {
2519
- type: 'tek-rest',
2520
- uniqueKey: 'id',
2521
- route: '/zeedhi',
2522
- lazyLoad: false,
2523
- },
2524
- columns: [
2525
- { name: 'id' },
2526
- { name: 'name', filterable: true, filterProps: [{ name: 'name_edit', label: 'name' }] },
2527
- {
2528
- name: 'salary',
2529
- filterable: true,
2530
- filterProps: [
2531
- {
2532
- name: 'salary_edit',
2533
- component: 'ZdCurrency',
2534
- operation: 'GREATER_THAN_EQUALS',
2535
- label: 'Salary (min)',
2536
- },
2537
- {
2538
- name: 'salary_edit',
2539
- component: 'ZdCurrency',
2540
- operation: 'LESS_THAN_EQUALS',
2541
- label: 'Salary (max)',
2542
- },
2543
- ],
2544
- },
2545
- ],
2546
- });
2547
-
2548
- const spy = mockMetadata(instance);
2549
- instance.onCreated();
2550
-
2551
- const filterButton = clickOnFilterButton(instance);
2552
- filterButton.onCreated();
2553
-
2554
- const inputs = instance.getFilterInputs();
2555
- expect(inputs[0].name).toBe('grid-filter-AND-CONTAINS-name-0');
2556
- expect(inputs[1].name).toBe('grid-filter-AND-GREATER_THAN_EQUALS-salary-0');
2557
- expect(inputs[2].name).toBe('grid-filter-AND-LESS_THAN_EQUALS-salary-1');
2558
- spy.mockReset();
2559
- spyModalService.mockReset();
2560
- });
2561
-
2562
- it('when called, should return the instances of filters with multiple inputs', () => {
2563
- const spyModalService = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => (new Modal(modal)));
2564
- const instance = new TekGrid({
2565
- name: 'grid',
2566
- component: 'TekGrid',
2567
- datasource: {
2568
- type: 'tek-rest',
2569
- uniqueKey: 'id',
2570
- route: '/zeedhi',
2571
- lazyLoad: false,
2572
- },
2573
- columns: [
2574
- { name: 'id' },
2575
- { name: 'name', filterable: true, filterProps: [{ name: 'name_edit', label: 'name' }] },
2576
- {
2577
- name: 'salary',
2578
- filterable: true,
2579
- filterProps: [
2580
- {
2581
- name: 'salary_edit',
2582
- component: 'ZdCurrency',
2583
- operation: 'GREATER_THAN_EQUALS',
2584
- label: 'Salary (min)',
2585
- },
2586
- {
2587
- name: 'salary_edit',
2588
- component: 'ZdCurrency',
2589
- operation: 'LESS_THAN_EQUALS',
2590
- label: 'Salary (max)',
2591
- },
2592
- ],
2593
- },
2594
- ],
2595
- });
2596
-
2597
- const spy = mockMetadata(instance);
2598
- instance.onCreated();
2599
-
2600
- const filterButton = clickOnFilterButton(instance);
2601
- filterButton.onCreated();
2602
-
2603
- const inputs = instance.getFilterInputs('salary');
2604
- expect(inputs[0].name).toBe('grid-filter-AND-GREATER_THAN_EQUALS-salary-0');
2605
- expect(inputs[1].name).toBe('grid-filter-AND-LESS_THAN_EQUALS-salary-1');
2606
- spy.mockReset();
2607
- spyModalService.mockReset();
2608
- });
2609
- });
2610
-
2611
- describe('loadAfterTasks', () => {
2612
- it('should call request even if tasks fail', async () => {
2613
- const instance = new TekGrid({
2614
- name: 'grid',
2615
- component: 'TekGrid',
2616
- });
2617
- const spy = jest.spyOn(instance.datasource, 'get');
2618
-
2619
- const errorFn = async () => {
2620
- throw new Error();
2621
- };
2622
- instance.registerTask(errorFn());
2623
-
2624
- await instance.loadAfterTasks();
2625
-
2626
- expect(spy).toHaveBeenCalledTimes(1);
2627
- });
2628
- });
2629
-
2630
- describe('isColumnSearchable()', () => {
2631
- let instance!: TekGrid;
2632
-
2633
- beforeEach(() => {
2634
- instance = new TekGrid({
2635
- name: 'TekGrid',
2636
- component: 'TekGrid',
2637
- columns: [{ name: 'name' }],
2638
- });
2639
- });
2640
-
2641
- it('should return true if column is visible', () => {
2642
- const column = instance.getColumn('name') as TekGridColumn;
2643
- column.isVisible = true;
2644
-
2645
- expect(instance.isColumnSearchable(column)).toBeTruthy();
2646
- });
2647
-
2648
- it('should return true if column is grouped', () => {
2649
- const column = instance.getColumn('name') as TekGridColumn;
2650
- column.isVisible = false;
2651
- column.grouped = true;
2652
-
2653
- expect(instance.isColumnSearchable(column)).toBeTruthy();
2654
- });
2655
-
2656
- it('should return false if column is not visible', () => {
2657
- const column = instance.getColumn('name') as TekGridColumn;
2658
- column.isVisible = false;
2659
-
2660
- expect(instance.isColumnSearchable(column)).toBeFalsy();
2661
- });
2662
-
2663
- it('should return true if iterable uses searchVisibleOnly as false', () => {
2664
- instance.searchVisibleOnly = false;
2665
-
2666
- const column = instance.getColumn('name') as TekGridColumn;
2667
- expect(instance.isColumnSearchable(column)).toBeTruthy();
2668
- column.isVisible = false;
2669
- expect(instance.isColumnSearchable(column)).toBeTruthy();
2670
- });
2671
- });
2672
-
2673
- describe('cancel column', () => {
2674
- it('should instantiate grid with a cancel column', () => {
2675
- const grid = new TekGrid({
2676
- name: 'grid',
2677
- component: 'TekGrid',
2678
- showCancelColumn: true,
2679
- columns: [
2680
- { name: 'name' },
2681
- ],
2682
- datasource: { data: [{ id: '1' }], uniqueKey: 'id' },
2683
- });
2684
- const spy = jest.fn();
2685
- grid.cancelAddedRow = spy;
2686
-
2687
- expect(grid.columns.length).toBe(2);
2688
- expect(grid.columns[1].name).toBe('cancel_action_grid');
2689
-
2690
- const comp: any = grid.getActionComponent(
2691
- grid.columns[1].children[0],
2692
- grid.columns[1],
2693
- grid.datasource.data[0],
2694
- );
2695
- const btn = new Button(comp);
2696
- btn.click();
2697
-
2698
- expect(spy).toHaveBeenCalledTimes(1);
2699
- });
2700
- });
2701
- });