@zeedhi/teknisa-components-common 1.107.2 → 1.108.0

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 (77) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +7 -7
  3. package/coverage/clover.xml +772 -692
  4. package/coverage/coverage-final.json +47 -47
  5. package/coverage/lcov-report/index.html +15 -15
  6. package/coverage/lcov-report/tests/__helpers__/component-event-helper.ts.html +2 -2
  7. package/coverage/lcov-report/tests/__helpers__/flush-promises-helper.ts.html +2 -2
  8. package/coverage/lcov-report/tests/__helpers__/get-child-helper.ts.html +7 -7
  9. package/coverage/lcov-report/tests/__helpers__/index.html +1 -1
  10. package/coverage/lcov-report/tests/__helpers__/index.ts.html +1 -1
  11. package/coverage/lcov-report/tests/__helpers__/mock-created-helper.ts.html +1 -1
  12. package/coverage/lcov.info +1459 -1275
  13. package/dist/tek-components-common.esm.js +375 -252
  14. package/dist/tek-components-common.umd.js +375 -252
  15. package/jest.config.js +1 -1
  16. package/package.json +2 -2
  17. package/tests/__helpers__/component-event-helper.ts +5 -5
  18. package/tests/__helpers__/flush-promises-helper.ts +3 -3
  19. package/tests/__helpers__/get-child-helper.ts +27 -27
  20. package/tests/__helpers__/index.ts +3 -3
  21. package/tests/__helpers__/mock-created-helper.ts +12 -12
  22. package/tests/__mocks__/@zeedhi/core/i18n.js +74 -74
  23. package/tests/__mocks__/@zeedhi/core.js +4 -4
  24. package/tests/unit/components/crud/crud-add-button.spec.ts +56 -56
  25. package/tests/unit/components/crud/crud-button.spec.ts +94 -94
  26. package/tests/unit/components/crud/crud-cancel-button.spec.ts +91 -91
  27. package/tests/unit/components/crud/crud-delete-button.spec.ts +122 -122
  28. package/tests/unit/components/crud/crud-form.spec.ts +165 -165
  29. package/tests/unit/components/crud/crud-save-button.spec.ts +118 -118
  30. package/tests/unit/components/tek-breadcrumb-header/tek-breadcrumb-header.spec.ts +42 -42
  31. package/tests/unit/components/tek-card-title/CardTitle.spec.ts +34 -34
  32. package/tests/unit/components/tek-datasource/memory-datasource.spec.ts +470 -470
  33. package/tests/unit/components/tek-datasource/rest-datasource.spec.ts +364 -364
  34. package/tests/unit/components/tek-drag-grid/tek-drag-grid.spec.ts +112 -112
  35. package/tests/unit/components/tek-grid/filter-helper.spec.ts +148 -148
  36. package/tests/unit/components/tek-grid/grid-column.spec.ts +270 -270
  37. package/tests/unit/components/tek-grid/grid-columns-button.spec.ts +195 -195
  38. package/tests/unit/components/tek-grid/grid-filter-button.spec.ts +967 -967
  39. package/tests/unit/components/tek-grid/grid.spec.ts +3340 -3116
  40. package/tests/unit/components/tek-grid/layout_options.spec.ts +980 -980
  41. package/tests/unit/components/tek-image/Image.spec.ts +67 -67
  42. package/tests/unit/components/tek-iterable-carousel/iterable-carousel.spec.ts +37 -37
  43. package/tests/unit/components/tek-iterable-component-render/iterable-component-render.spec.ts +200 -200
  44. package/tests/unit/components/tek-loading/Loading.spec.ts +30 -30
  45. package/tests/unit/components/tek-notifications/notifications.spec.ts +201 -201
  46. package/tests/unit/components/tek-product-card/product-card.spec.ts +61 -61
  47. package/tests/unit/components/tek-user-info/TekUserInfoContoller.spec.ts +220 -220
  48. package/tests/unit/components/tek-user-info/tek-user-info-list.spec.ts +86 -86
  49. package/tests/unit/components/tek-user-info/tek-user-info.spec.ts +430 -430
  50. package/tests/unit/components/tree-grid/tree-grid.spec.ts +776 -776
  51. package/tests/unit/utils/grid-base/delete-rows-error.spec.ts +24 -0
  52. package/tests/unit/utils/grid-base/export-options/button-option.spec.ts +44 -44
  53. package/tests/unit/utils/grid-base/export-options/multi-option.spec.ts +135 -135
  54. package/tests/unit/utils/grid-base/grid-controller.spec.ts +267 -174
  55. package/tests/unit/utils/report-filter/report-filter.spec.ts +66 -66
  56. package/tsconfig.eslint.json +8 -8
  57. package/types/components/tek-datasource/memory-datasource.d.ts +2 -4
  58. package/types/components/tek-datasource/rest-datasource.d.ts +6 -3
  59. package/types/components/tek-grid/grid.d.ts +50 -6
  60. package/types/components/tek-grid/interfaces.d.ts +5 -3
  61. package/types/components/tek-tree-grid/tree-grid.d.ts +5 -1
  62. package/types/utils/grid-base/grid-base.d.ts +12 -229
  63. package/types/utils/grid-base/grid-controller.d.ts +1 -3
  64. package/types/components/tek-ag-grid/default-icons.d.ts +0 -53
  65. package/types/components/tek-ag-grid/interfaces.d.ts +0 -9
  66. package/types/components/tek-ag-grid/tek-ag-grid.d.ts +0 -35
  67. package/types/components/tek-datasource/datasource.d.ts +0 -94
  68. package/types/components/tek-grid/default-icons.d.ts +0 -53
  69. package/types/components/tek-grid/filter-dynamic-values.d.ts +0 -9
  70. package/types/components/tek-grid/grid-controller.d.ts +0 -19
  71. package/types/components/tek-grid/grid_column.d.ts +0 -14
  72. package/types/components/tek-grid/grid_controller.d.ts +0 -15
  73. package/types/components/tek-grid/tek-grid.d.ts +0 -35
  74. package/types/components/tek-login/interfaces.d.ts +0 -3
  75. package/types/components/tek-login/login-children.d.ts +0 -3
  76. package/types/components/tek-login/login.d.ts +0 -58
  77. package/types/components/tek-login/login_children.d.ts +0 -3
@@ -1,980 +1,980 @@
1
- import { Grid, Tag } from '@zeedhi/common';
2
- import { Config, Http } from '@zeedhi/core';
3
- import {
4
- ITekConfig,
5
- ITekGridLayoutInfo,
6
- TekGrid,
7
- TekGridLayoutOptions,
8
- } from '../../../../src';
9
-
10
- let savedLayouts: string = '';
11
- const testLayouts: ITekGridLayoutInfo = {
12
- layouts: {
13
- 'Name first': {
14
- name: 'Name first',
15
- gridWidth: '544px',
16
- columns: [
17
- {
18
- name: 'id',
19
- width: '34px',
20
- isVisible: true,
21
- grouped: false,
22
- },
23
- {
24
- name: 'name',
25
- width: '107px',
26
- isVisible: true,
27
- grouped: false,
28
- },
29
- {
30
- name: 'salary',
31
- width: '114px',
32
- isVisible: true,
33
- grouped: false,
34
- },
35
- ],
36
- },
37
- },
38
- };
39
-
40
- describe('LayoutOptions', () => {
41
- describe('constructor()', () => {
42
- it('should create layout options object', () => {
43
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
44
-
45
- expect(instance.currentLayoutName).toBe('');
46
- expect(instance.layouts).toEqual({});
47
- expect(instance.layoutNames).toEqual([]);
48
- expect(instance.originalColumnProps).toEqual([]);
49
- });
50
- });
51
-
52
- describe('mounted()', () => {
53
- it('should store the original column props of a TekGrid', () => {
54
- const tag = new Tag({
55
- name: 'toolbar',
56
- component: 'ZdTag',
57
- tag: 'span',
58
- });
59
- const grid = new TekGrid({
60
- name: 'grid',
61
- component: 'TekGrid',
62
- columns: [
63
- { name: 'id' },
64
- { name: 'name' },
65
- { name: 'salary' },
66
- {
67
- name: 'date1',
68
- filterProps: {
69
- name: 'date1-filter',
70
- helperValue: 'TODAY',
71
- },
72
- },
73
- {
74
- name: 'date2',
75
- filterProps: [
76
- {
77
- name: 'date2-filter1',
78
- helperValue: 'TODAY',
79
- },
80
- {
81
- name: 'date2-filter2',
82
- helperValue: 'TOMORROW',
83
- },
84
- ],
85
- },
86
- ],
87
- });
88
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
89
- instance.parent = tag;
90
- tag.parent = grid;
91
-
92
- instance.onMounted({} as HTMLElement);
93
-
94
- expect(instance.originalDatasourceDynamicFilter).toEqual({});
95
- expect(instance.originalDatasourceFilter).toEqual({});
96
- expect(instance.originalColumnProps).toEqual([
97
- {
98
- name: 'id',
99
- isVisible: true,
100
- fixed: false,
101
- grouped: false,
102
- minWidth: undefined,
103
- maxWidth: undefined,
104
- aggregation: undefined,
105
- align: 'left',
106
- groupOpened: undefined,
107
- label: '',
108
- filterHelperValue: [],
109
- },
110
- {
111
- name: 'name',
112
- isVisible: true,
113
- fixed: false,
114
- grouped: false,
115
- minWidth: undefined,
116
- maxWidth: undefined,
117
- aggregation: undefined,
118
- align: 'left',
119
- groupOpened: undefined,
120
- label: '',
121
- filterHelperValue: [],
122
- },
123
- {
124
- name: 'salary',
125
- isVisible: true,
126
- fixed: false,
127
- grouped: false,
128
- minWidth: undefined,
129
- maxWidth: undefined,
130
- aggregation: undefined,
131
- align: 'left',
132
- groupOpened: undefined,
133
- label: '',
134
- filterHelperValue: [],
135
- },
136
- {
137
- name: 'date1',
138
- isVisible: true,
139
- fixed: false,
140
- grouped: false,
141
- minWidth: undefined,
142
- maxWidth: undefined,
143
- aggregation: undefined,
144
- align: 'left',
145
- groupOpened: undefined,
146
- label: '',
147
- filterHelperValue: 'TODAY',
148
- },
149
- {
150
- name: 'date2',
151
- isVisible: true,
152
- fixed: false,
153
- grouped: false,
154
- minWidth: undefined,
155
- maxWidth: undefined,
156
- aggregation: undefined,
157
- align: 'left',
158
- groupOpened: undefined,
159
- label: '',
160
- filterHelperValue: ['TODAY', 'TOMORROW'],
161
- },
162
- ]);
163
- });
164
-
165
- it('should store the original column props of a normal Grid', () => {
166
- const grid = new Grid({
167
- name: 'grid',
168
- component: 'Grid',
169
- columns: [
170
- { name: 'id', maxWidth: '200px', minWidth: '100px' },
171
- { name: 'name' },
172
- { name: 'salary' },
173
- ],
174
- });
175
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
176
- instance.parent = grid;
177
-
178
- instance.onMounted({} as HTMLElement);
179
-
180
- expect(instance.originalColumnProps).toEqual([
181
- {
182
- name: 'id',
183
- isVisible: true,
184
- maxWidth: '200px',
185
- minWidth: '100px',
186
- aggregation: undefined,
187
- align: 'left',
188
- groupOpened: undefined,
189
- label: '',
190
- filterHelperValue: '',
191
- fixed: false,
192
- grouped: undefined,
193
- },
194
- {
195
- name: 'name',
196
- isVisible: true,
197
- minWidth: undefined,
198
- maxWidth: undefined,
199
- aggregation: undefined,
200
- align: 'left',
201
- groupOpened: undefined,
202
- label: '',
203
- filterHelperValue: '',
204
- fixed: false,
205
- grouped: undefined,
206
- },
207
- {
208
- name: 'salary',
209
- isVisible: true,
210
- minWidth: undefined,
211
- maxWidth: undefined,
212
- aggregation: undefined,
213
- align: 'left',
214
- groupOpened: undefined,
215
- label: '',
216
- filterHelperValue: '',
217
- fixed: false,
218
- grouped: undefined,
219
- },
220
- ]);
221
- });
222
- });
223
-
224
- describe('loadLayouts()', () => {
225
- it('should load using event', async () => {
226
- const layouts: ITekGridLayoutInfo = {
227
- currentLayoutName: 'Name first',
228
- layouts: {
229
- 'Name first': {
230
- name: 'Name first',
231
- gridWidth: '544px',
232
- columns: [
233
- {
234
- name: 'id',
235
- width: '34px',
236
- isVisible: true,
237
- grouped: false,
238
- },
239
- {
240
- name: 'name',
241
- width: '107px',
242
- isVisible: true,
243
- grouped: false,
244
- },
245
- {
246
- name: 'salary',
247
- width: '114px',
248
- isVisible: true,
249
- grouped: false,
250
- },
251
- ],
252
- },
253
- },
254
- };
255
-
256
- const tag = new Tag({
257
- name: 'toolbar',
258
- component: 'ZdTag',
259
- tag: 'span',
260
- });
261
- const grid = new TekGrid({
262
- name: 'grid',
263
- component: 'TekGrid',
264
- columns: [
265
- { name: 'id' },
266
- { name: 'name' },
267
- { name: 'salary' },
268
- ],
269
- events: {
270
- loadLayouts: async () => layouts,
271
- },
272
- });
273
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
274
- instance.parent = tag;
275
- tag.parent = grid;
276
-
277
- await instance.onMounted({} as HTMLElement);
278
-
279
- expect(instance.currentLayoutName).toBe('Name first');
280
- expect(instance.layoutNames).toEqual(['Name first']);
281
- expect(instance.layouts).toEqual(layouts.layouts);
282
- });
283
-
284
- it('should load using localStorage', async () => {
285
- const tag = new Tag({
286
- name: 'toolbar',
287
- component: 'ZdTag',
288
- tag: 'span',
289
- });
290
- const grid = new TekGrid({
291
- name: 'grid',
292
- component: 'TekGrid',
293
- columns: [
294
- { name: 'id' },
295
- { name: 'name' },
296
- { name: 'salary' },
297
- ],
298
- });
299
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
300
- instance.parent = tag;
301
- tag.parent = grid;
302
-
303
- const localStorageMock = (function () { // eslint-disable-line func-names
304
- return {
305
- getItem(): string {
306
- return JSON.stringify(testLayouts);
307
- },
308
- setItem(_name: string, value: string) {
309
- savedLayouts = value;
310
- },
311
- };
312
- }());
313
-
314
- Object.defineProperty(window, 'localStorage', {
315
- value: localStorageMock,
316
- });
317
-
318
- await instance.onMounted({} as HTMLElement);
319
-
320
- expect(instance.currentLayoutName).toBe('');
321
- expect(instance.layoutNames).toEqual(['Name first']);
322
- expect(instance.layouts).toEqual(testLayouts.layouts);
323
- });
324
- });
325
-
326
- describe('newLayout()', () => {
327
- it('should add new layout', () => {
328
- const newLayout = {
329
- name: 'Name first',
330
- gridWidth: '544px',
331
- columns: [
332
- {
333
- name: 'id',
334
- width: '34px',
335
- isVisible: true,
336
- grouped: false,
337
- },
338
- {
339
- name: 'name',
340
- width: '107px',
341
- isVisible: true,
342
- grouped: false,
343
- },
344
- {
345
- name: 'salary',
346
- width: '114px',
347
- isVisible: true,
348
- grouped: false,
349
- },
350
- ],
351
- };
352
- const tag = new Tag({
353
- name: 'toolbar',
354
- component: 'ZdTag',
355
- tag: 'span',
356
- });
357
- const grid = new TekGrid({
358
- name: 'grid',
359
- component: 'TekGrid',
360
- columns: [
361
- { name: 'id' },
362
- { name: 'name' },
363
- { name: 'salary' },
364
- ],
365
- });
366
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
367
- instance.parent = tag;
368
- tag.parent = grid;
369
-
370
- instance.onMounted({} as HTMLElement);
371
-
372
- instance.newLayout(newLayout);
373
-
374
- expect(instance.currentLayoutName).toBe('Name first');
375
- expect(instance.layoutNames).toEqual(['Name first']);
376
- expect(instance.layouts).toEqual({ 'Name first': newLayout });
377
- expect(savedLayouts).toBe(JSON.stringify({
378
- currentLayoutName: 'Name first',
379
- layouts: { 'Name first': newLayout },
380
- }));
381
- });
382
- });
383
-
384
- describe('addLayout()', () => {
385
- it('should add new layout and not change current layout', () => {
386
- const newLayout = {
387
- name: 'Name first',
388
- gridWidth: '544px',
389
- columns: [
390
- {
391
- name: 'id',
392
- width: '34px',
393
- isVisible: true,
394
- grouped: false,
395
- },
396
- {
397
- name: 'name',
398
- width: '107px',
399
- isVisible: true,
400
- grouped: false,
401
- },
402
- {
403
- name: 'salary',
404
- width: '114px',
405
- isVisible: true,
406
- grouped: false,
407
- },
408
- ],
409
- };
410
- const tag = new Tag({
411
- name: 'toolbar',
412
- component: 'ZdTag',
413
- tag: 'span',
414
- });
415
- const grid = new TekGrid({
416
- name: 'grid',
417
- component: 'TekGrid',
418
- columns: [
419
- { name: 'id' },
420
- { name: 'name' },
421
- { name: 'salary' },
422
- ],
423
- });
424
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
425
- instance.parent = tag;
426
- tag.parent = grid;
427
-
428
- instance.onMounted({} as HTMLElement);
429
-
430
- instance.addLayout(newLayout);
431
-
432
- expect(instance.currentLayoutName).toBe('');
433
- expect(instance.layoutNames).toEqual(['Name first']);
434
- expect(instance.layouts).toEqual({ 'Name first': newLayout });
435
- expect(savedLayouts).toBe(JSON.stringify({
436
- currentLayoutName: '',
437
- layouts: { 'Name first': newLayout },
438
- }));
439
- });
440
- });
441
-
442
- describe('applyLayout()', () => {
443
- it('should change Layout and save layout', async () => {
444
- let viewCalled = false;
445
- const tag = new Tag({
446
- name: 'toolbar',
447
- component: 'ZdTag',
448
- tag: 'span',
449
- });
450
- const grid = new TekGrid({
451
- name: 'grid',
452
- component: 'TekGrid',
453
- columns: [
454
- { name: 'id' },
455
- { name: 'name' },
456
- { name: 'salary' },
457
- ],
458
- });
459
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
460
- instance.viewApplyLayout = () => { viewCalled = true; };
461
- instance.parent = tag;
462
- tag.parent = grid;
463
-
464
- await instance.onMounted({} as HTMLElement);
465
-
466
- expect(instance.currentLayoutName).toBe('');
467
- instance.applyLayout('Name first');
468
- expect(instance.currentLayoutName).toBe('Name first');
469
- expect(viewCalled).toBeTruthy();
470
- });
471
-
472
- it('should change Layout and save layout using event', async () => {
473
- let eventSavedLayouts: ITekGridLayoutInfo = {};
474
- const tag = new Tag({
475
- name: 'toolbar',
476
- component: 'ZdTag',
477
- tag: 'span',
478
- });
479
- const grid = new TekGrid({
480
- name: 'grid',
481
- component: 'TekGrid',
482
- columns: [
483
- { name: 'id' },
484
- { name: 'name' },
485
- { name: 'salary' },
486
- ],
487
- events: {
488
- saveLayouts: ({ layouts }) => { eventSavedLayouts = layouts; },
489
- },
490
- });
491
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
492
- instance.parent = tag;
493
- tag.parent = grid;
494
-
495
- await instance.onMounted({} as HTMLElement);
496
-
497
- expect(instance.currentLayoutName).toBe('');
498
- instance.applyLayout('Name first');
499
- expect(instance.currentLayoutName).toBe('Name first');
500
- expect(eventSavedLayouts.currentLayoutName).toBe('Name first');
501
- });
502
- });
503
-
504
- describe('deleteLayout()', () => {
505
- it('should delete layout and save', async () => {
506
- const tag = new Tag({
507
- name: 'toolbar',
508
- component: 'ZdTag',
509
- tag: 'span',
510
- });
511
- const grid = new TekGrid({
512
- name: 'grid',
513
- component: 'TekGrid',
514
- columns: [
515
- { name: 'id' },
516
- { name: 'name' },
517
- { name: 'salary' },
518
- ],
519
- });
520
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
521
- instance.parent = tag;
522
- tag.parent = grid;
523
-
524
- await instance.onMounted({} as HTMLElement);
525
-
526
- instance.applyLayout('Name first');
527
- expect(instance.currentLayoutName).toBe('Name first');
528
- expect(instance.layoutNames).toEqual(['Name first']);
529
- instance.deleteLayout('Name first');
530
- expect(instance.currentLayoutName).toBe('');
531
- expect(instance.layoutNames).toEqual([]);
532
- });
533
-
534
- it('should delete layout without applying layout', async () => {
535
- const tag = new Tag({
536
- name: 'toolbar',
537
- component: 'ZdTag',
538
- tag: 'span',
539
- });
540
- const grid = new TekGrid({
541
- name: 'grid',
542
- component: 'TekGrid',
543
- columns: [
544
- { name: 'id' },
545
- { name: 'name' },
546
- { name: 'salary' },
547
- ],
548
- });
549
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
550
- instance.parent = tag;
551
- tag.parent = grid;
552
-
553
- await instance.onMounted({} as HTMLElement);
554
-
555
- expect(instance.currentLayoutName).toBe('');
556
- expect(instance.layoutNames).toEqual(['Name first']);
557
- instance.deleteLayout('Name first');
558
- expect(instance.currentLayoutName).toBe('');
559
- expect(instance.layoutNames).toEqual([]);
560
- });
561
- });
562
-
563
- describe('updateLayout()', () => {
564
- it('should update layout and save', async () => {
565
- const tag = new Tag({
566
- name: 'toolbar',
567
- component: 'ZdTag',
568
- tag: 'span',
569
- });
570
- const grid = new TekGrid({
571
- name: 'grid',
572
- component: 'TekGrid',
573
- columns: [
574
- { name: 'id' },
575
- { name: 'name' },
576
- { name: 'salary' },
577
- ],
578
- });
579
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
580
- instance.parent = tag;
581
- tag.parent = grid;
582
-
583
- await instance.onMounted({} as HTMLElement);
584
-
585
- instance.updateLayout('Name first', {
586
- name: 'Name first',
587
- gridWidth: '500px',
588
- filter: {
589
- name: 'a',
590
- },
591
- dynamicFilter: {
592
- name: [
593
- {
594
- operation: 'CONTAINS',
595
- relation: 'AND',
596
- value: 'a',
597
- },
598
- ],
599
- },
600
- });
601
-
602
- expect(instance.currentLayoutName).toBe('Name first');
603
- expect(instance.layoutNames).toEqual(['Name first']);
604
- expect(instance.layouts).toEqual({
605
- 'Name first': {
606
- name: 'Name first',
607
- gridWidth: '500px',
608
- columns: instance.originalColumnProps,
609
- filter: {
610
- name: 'a',
611
- },
612
- dynamicFilter: {
613
- name: [
614
- {
615
- operation: 'CONTAINS',
616
- relation: 'AND',
617
- value: 'a',
618
- },
619
- ],
620
- },
621
- },
622
- });
623
- });
624
- });
625
-
626
- describe('discardChanges()', () => {
627
- it('should discard changes and restore layout', async () => {
628
- const layouts: ITekGridLayoutInfo = {
629
- currentLayoutName: 'Name first',
630
- layouts: {
631
- 'Name first': {
632
- name: 'Name first',
633
- gridWidth: '544px',
634
- columns: [
635
- {
636
- name: 'id',
637
- width: '34px',
638
- isVisible: true,
639
- grouped: false,
640
- },
641
- {
642
- name: 'name',
643
- width: '107px',
644
- isVisible: true,
645
- grouped: false,
646
- },
647
- {
648
- name: 'salary',
649
- width: '114px',
650
- isVisible: true,
651
- grouped: false,
652
- },
653
- ],
654
- },
655
- },
656
- };
657
-
658
- let viewCalled = false;
659
-
660
- const tag = new Tag({
661
- name: 'toolbar',
662
- component: 'ZdTag',
663
- tag: 'span',
664
- });
665
- const grid = new TekGrid({
666
- name: 'grid',
667
- component: 'TekGrid',
668
- columns: [
669
- { name: 'id' },
670
- { name: 'name' },
671
- { name: 'salary' },
672
- ],
673
- events: {
674
- loadLayouts: async () => layouts,
675
- },
676
- });
677
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
678
- instance.viewApplyLayout = () => { viewCalled = true; };
679
- instance.parent = tag;
680
- tag.parent = grid;
681
-
682
- await instance.onMounted({} as HTMLElement);
683
- instance.applyLayout('Name first');
684
- expect(viewCalled).toBeTruthy();
685
- expect(instance.layoutEdited).toBeFalsy();
686
- expect(instance.currentLayoutName).toBe('Name first');
687
-
688
- instance.layoutEdited = true;
689
-
690
- instance.discardChanges();
691
- expect(instance.layoutEdited).toBeFalsy();
692
- });
693
- });
694
-
695
- describe('updateDefaultLayout()', () => {
696
- it('should update default layout', async () => {
697
- const tag = new Tag({
698
- name: 'toolbar',
699
- component: 'ZdTag',
700
- tag: 'span',
701
- });
702
- const grid = new TekGrid({
703
- name: 'grid',
704
- component: 'TekGrid',
705
- columns: [
706
- { name: 'id' },
707
- { name: 'name' },
708
- { name: 'salary' },
709
- ],
710
- });
711
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
712
- instance.parent = tag;
713
- tag.parent = grid;
714
-
715
- await instance.onMounted({} as HTMLElement);
716
-
717
- expect(instance.originalDatasourceOrder).toEqual([]);
718
- expect(instance.originalDatasourceFilter).toEqual({});
719
- expect(instance.originalDatasourceDynamicFilter).toEqual({});
720
- expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['id', 'name', 'salary']);
721
-
722
- const newDynamicFilter = {
723
- name: [
724
- {
725
- operation: 'CONTAINS',
726
- relation: 'AND',
727
- value: 'a',
728
- },
729
- ],
730
- };
731
-
732
- instance.updateDefaultLayout({
733
- name: '',
734
- columns: [
735
- { name: 'name' },
736
- { name: 'id' },
737
- { name: 'hire_date' },
738
- ],
739
- order: ['name.asc'],
740
- filter: {
741
- name: 'a',
742
- },
743
- dynamicFilter: newDynamicFilter,
744
- });
745
-
746
- expect(instance.originalDatasourceOrder).toEqual(['name.asc']);
747
- expect(instance.originalDatasourceFilter).toEqual({ name: 'a' });
748
- expect(instance.originalDatasourceDynamicFilter).toEqual(newDynamicFilter);
749
- expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['name', 'id', 'hire_date', 'salary']);
750
- });
751
-
752
- it('should update default layout with some values', async () => {
753
- const tag = new Tag({
754
- name: 'toolbar',
755
- component: 'ZdTag',
756
- tag: 'span',
757
- });
758
- const grid = new TekGrid({
759
- name: 'grid',
760
- component: 'TekGrid',
761
- columns: [
762
- { name: 'id' },
763
- { name: 'name' },
764
- { name: 'salary' },
765
- ],
766
- });
767
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
768
- instance.parent = tag;
769
- tag.parent = grid;
770
-
771
- await instance.onMounted({} as HTMLElement);
772
-
773
- const applyLayoutSpy = jest.spyOn(instance, 'applyLayout');
774
-
775
- expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['id', 'name', 'salary']);
776
-
777
- instance.updateDefaultLayout({
778
- name: 'default',
779
- columns: [
780
- { name: 'name' },
781
- { name: 'id' },
782
- { name: 'hire_date' },
783
- ],
784
- });
785
-
786
- expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['name', 'id', 'hire_date', 'salary']);
787
- expect(applyLayoutSpy).toBeCalled();
788
- });
789
- });
790
-
791
- describe('loadLayouts()/saveLayouts()', () => {
792
- it('should load layouts using config endpoint', async () => {
793
- (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
794
- const tag = new Tag({
795
- name: 'toolbar',
796
- component: 'ZdTag',
797
- tag: 'span',
798
- });
799
- const grid = new TekGrid({
800
- name: 'grid',
801
- component: 'TekGrid',
802
- columns: [
803
- { name: 'id' },
804
- { name: 'name' },
805
- { name: 'salary' },
806
- ],
807
- });
808
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
809
- const flushPromises = () => new Promise(setImmediate);
810
- instance.parent = tag;
811
- tag.parent = grid;
812
-
813
- const layouts = {
814
- currentLayoutName: 'layout-test',
815
- layouts: {
816
- 'layout-test': {
817
- name: 'layout-test',
818
- gridWidth: '100px',
819
- columns: [
820
- {
821
- name: 'id',
822
- label: 'ID',
823
- },
824
- ],
825
- },
826
- },
827
- };
828
-
829
- const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: { layouts } } }));
830
-
831
- await instance.onMounted({} as HTMLElement);
832
- await grid.onMounted({} as HTMLElement);
833
-
834
- await flushPromises();
835
- expect(spy).toBeCalled();
836
- expect(instance.layouts).toEqual({
837
- 'layout-test': {
838
- name: 'layout-test',
839
- gridWidth: '100px',
840
- columns: [
841
- {
842
- name: 'id',
843
- label: 'ID',
844
- },
845
- ],
846
- },
847
- });
848
- spy.mockReset();
849
- (Config as ITekConfig).loadGridLayoutsEndPoint = '';
850
- });
851
-
852
- it('should load layouts using config endpoint with array data', async () => {
853
- (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
854
- const tag = new Tag({
855
- name: 'toolbar',
856
- component: 'ZdTag',
857
- tag: 'span',
858
- });
859
- const grid = new TekGrid({
860
- name: 'grid',
861
- component: 'TekGrid',
862
- columns: [
863
- { name: 'id' },
864
- { name: 'name' },
865
- { name: 'salary' },
866
- ],
867
- });
868
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
869
- const flushPromises = () => new Promise(setImmediate);
870
- instance.parent = tag;
871
- tag.parent = grid;
872
-
873
- const layouts = {
874
- currentLayoutName: 'layout-test',
875
- layouts: {
876
- 'layout-test': {
877
- name: 'layout-test',
878
- gridWidth: '100px',
879
- columns: [
880
- {
881
- name: 'id',
882
- label: 'ID',
883
- },
884
- ],
885
- },
886
- },
887
- };
888
-
889
- const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: [{ layouts }] } }));
890
-
891
- await instance.onMounted({} as HTMLElement);
892
- await grid.onMounted({} as HTMLElement);
893
-
894
- await flushPromises();
895
- expect(spy).toBeCalled();
896
- expect(instance.layouts).toEqual({
897
- 'layout-test': {
898
- name: 'layout-test',
899
- gridWidth: '100px',
900
- columns: [
901
- {
902
- name: 'id',
903
- label: 'ID',
904
- },
905
- ],
906
- },
907
- });
908
- spy.mockReset();
909
- (Config as ITekConfig).loadGridLayoutsEndPoint = '';
910
- });
911
-
912
- it('should load layouts using config endpoint with empty data', async () => {
913
- (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
914
- const tag = new Tag({
915
- name: 'toolbar',
916
- component: 'ZdTag',
917
- tag: 'span',
918
- });
919
- const grid = new TekGrid({
920
- name: 'grid',
921
- component: 'TekGrid',
922
- columns: [
923
- { name: 'id' },
924
- { name: 'name' },
925
- { name: 'salary' },
926
- ],
927
- });
928
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
929
- const flushPromises = () => new Promise(setImmediate);
930
- instance.parent = tag;
931
- tag.parent = grid;
932
-
933
- const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: '' } }));
934
-
935
- await instance.onMounted({} as HTMLElement);
936
- await grid.onMounted({} as HTMLElement);
937
-
938
- await flushPromises();
939
- expect(spy).toBeCalled();
940
- expect(instance.layouts).toEqual({});
941
- spy.mockReset();
942
- (Config as ITekConfig).loadGridLayoutsEndPoint = '';
943
- });
944
-
945
- it('should save layout using endpoint', async () => {
946
- (Config as ITekConfig).saveGridLayoutsEndPoint = '/saveLayoutsRoute';
947
- const tag = new Tag({
948
- name: 'toolbar',
949
- component: 'ZdTag',
950
- tag: 'span',
951
- });
952
- const grid = new TekGrid({
953
- name: 'grid',
954
- component: 'TekGrid',
955
- columns: [
956
- { name: 'id' },
957
- { name: 'name' },
958
- { name: 'salary' },
959
- ],
960
- });
961
- const layouts = {
962
- name: 'layout-test',
963
- gridWidth: '100px',
964
- columns: [
965
- {
966
- name: 'id',
967
- label: 'ID',
968
- },
969
- ],
970
- };
971
- const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
972
- instance.grid = grid;
973
- instance.parent = tag;
974
- tag.parent = grid;
975
- instance.newLayout(layouts);
976
- expect(instance.currentLayoutName).toBe('layout-test');
977
- (Config as ITekConfig).saveGridLayoutsEndPoint = '';
978
- });
979
- });
980
- });
1
+ import { Grid, Tag } from '@zeedhi/common';
2
+ import { Config, Http } from '@zeedhi/core';
3
+ import {
4
+ ITekConfig,
5
+ ITekGridLayoutInfo,
6
+ TekGrid,
7
+ TekGridLayoutOptions,
8
+ } from '../../../../src';
9
+
10
+ let savedLayouts: string = '';
11
+ const testLayouts: ITekGridLayoutInfo = {
12
+ layouts: {
13
+ 'Name first': {
14
+ name: 'Name first',
15
+ gridWidth: '544px',
16
+ columns: [
17
+ {
18
+ name: 'id',
19
+ width: '34px',
20
+ isVisible: true,
21
+ grouped: false,
22
+ },
23
+ {
24
+ name: 'name',
25
+ width: '107px',
26
+ isVisible: true,
27
+ grouped: false,
28
+ },
29
+ {
30
+ name: 'salary',
31
+ width: '114px',
32
+ isVisible: true,
33
+ grouped: false,
34
+ },
35
+ ],
36
+ },
37
+ },
38
+ };
39
+
40
+ describe('LayoutOptions', () => {
41
+ describe('constructor()', () => {
42
+ it('should create layout options object', () => {
43
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
44
+
45
+ expect(instance.currentLayoutName).toBe('');
46
+ expect(instance.layouts).toEqual({});
47
+ expect(instance.layoutNames).toEqual([]);
48
+ expect(instance.originalColumnProps).toEqual([]);
49
+ });
50
+ });
51
+
52
+ describe('mounted()', () => {
53
+ it('should store the original column props of a TekGrid', () => {
54
+ const tag = new Tag({
55
+ name: 'toolbar',
56
+ component: 'ZdTag',
57
+ tag: 'span',
58
+ });
59
+ const grid = new TekGrid({
60
+ name: 'grid',
61
+ component: 'TekGrid',
62
+ columns: [
63
+ { name: 'id' },
64
+ { name: 'name' },
65
+ { name: 'salary' },
66
+ {
67
+ name: 'date1',
68
+ filterProps: {
69
+ name: 'date1-filter',
70
+ helperValue: 'TODAY',
71
+ },
72
+ },
73
+ {
74
+ name: 'date2',
75
+ filterProps: [
76
+ {
77
+ name: 'date2-filter1',
78
+ helperValue: 'TODAY',
79
+ },
80
+ {
81
+ name: 'date2-filter2',
82
+ helperValue: 'TOMORROW',
83
+ },
84
+ ],
85
+ },
86
+ ],
87
+ });
88
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
89
+ instance.parent = tag;
90
+ tag.parent = grid;
91
+
92
+ instance.onMounted({} as HTMLElement);
93
+
94
+ expect(instance.originalDatasourceDynamicFilter).toEqual({});
95
+ expect(instance.originalDatasourceFilter).toEqual({});
96
+ expect(instance.originalColumnProps).toEqual([
97
+ {
98
+ name: 'id',
99
+ isVisible: true,
100
+ fixed: false,
101
+ grouped: false,
102
+ minWidth: undefined,
103
+ maxWidth: undefined,
104
+ aggregation: undefined,
105
+ align: 'left',
106
+ groupOpened: undefined,
107
+ label: '',
108
+ filterHelperValue: [],
109
+ },
110
+ {
111
+ name: 'name',
112
+ isVisible: true,
113
+ fixed: false,
114
+ grouped: false,
115
+ minWidth: undefined,
116
+ maxWidth: undefined,
117
+ aggregation: undefined,
118
+ align: 'left',
119
+ groupOpened: undefined,
120
+ label: '',
121
+ filterHelperValue: [],
122
+ },
123
+ {
124
+ name: 'salary',
125
+ isVisible: true,
126
+ fixed: false,
127
+ grouped: false,
128
+ minWidth: undefined,
129
+ maxWidth: undefined,
130
+ aggregation: undefined,
131
+ align: 'left',
132
+ groupOpened: undefined,
133
+ label: '',
134
+ filterHelperValue: [],
135
+ },
136
+ {
137
+ name: 'date1',
138
+ isVisible: true,
139
+ fixed: false,
140
+ grouped: false,
141
+ minWidth: undefined,
142
+ maxWidth: undefined,
143
+ aggregation: undefined,
144
+ align: 'left',
145
+ groupOpened: undefined,
146
+ label: '',
147
+ filterHelperValue: 'TODAY',
148
+ },
149
+ {
150
+ name: 'date2',
151
+ isVisible: true,
152
+ fixed: false,
153
+ grouped: false,
154
+ minWidth: undefined,
155
+ maxWidth: undefined,
156
+ aggregation: undefined,
157
+ align: 'left',
158
+ groupOpened: undefined,
159
+ label: '',
160
+ filterHelperValue: ['TODAY', 'TOMORROW'],
161
+ },
162
+ ]);
163
+ });
164
+
165
+ it('should store the original column props of a normal Grid', () => {
166
+ const grid = new Grid({
167
+ name: 'grid',
168
+ component: 'Grid',
169
+ columns: [
170
+ { name: 'id', maxWidth: '200px', minWidth: '100px' },
171
+ { name: 'name' },
172
+ { name: 'salary' },
173
+ ],
174
+ });
175
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
176
+ instance.parent = grid;
177
+
178
+ instance.onMounted({} as HTMLElement);
179
+
180
+ expect(instance.originalColumnProps).toEqual([
181
+ {
182
+ name: 'id',
183
+ isVisible: true,
184
+ maxWidth: '200px',
185
+ minWidth: '100px',
186
+ aggregation: undefined,
187
+ align: 'left',
188
+ groupOpened: undefined,
189
+ label: '',
190
+ filterHelperValue: '',
191
+ fixed: false,
192
+ grouped: undefined,
193
+ },
194
+ {
195
+ name: 'name',
196
+ isVisible: true,
197
+ minWidth: undefined,
198
+ maxWidth: undefined,
199
+ aggregation: undefined,
200
+ align: 'left',
201
+ groupOpened: undefined,
202
+ label: '',
203
+ filterHelperValue: '',
204
+ fixed: false,
205
+ grouped: undefined,
206
+ },
207
+ {
208
+ name: 'salary',
209
+ isVisible: true,
210
+ minWidth: undefined,
211
+ maxWidth: undefined,
212
+ aggregation: undefined,
213
+ align: 'left',
214
+ groupOpened: undefined,
215
+ label: '',
216
+ filterHelperValue: '',
217
+ fixed: false,
218
+ grouped: undefined,
219
+ },
220
+ ]);
221
+ });
222
+ });
223
+
224
+ describe('loadLayouts()', () => {
225
+ it('should load using event', async () => {
226
+ const layouts: ITekGridLayoutInfo = {
227
+ currentLayoutName: 'Name first',
228
+ layouts: {
229
+ 'Name first': {
230
+ name: 'Name first',
231
+ gridWidth: '544px',
232
+ columns: [
233
+ {
234
+ name: 'id',
235
+ width: '34px',
236
+ isVisible: true,
237
+ grouped: false,
238
+ },
239
+ {
240
+ name: 'name',
241
+ width: '107px',
242
+ isVisible: true,
243
+ grouped: false,
244
+ },
245
+ {
246
+ name: 'salary',
247
+ width: '114px',
248
+ isVisible: true,
249
+ grouped: false,
250
+ },
251
+ ],
252
+ },
253
+ },
254
+ };
255
+
256
+ const tag = new Tag({
257
+ name: 'toolbar',
258
+ component: 'ZdTag',
259
+ tag: 'span',
260
+ });
261
+ const grid = new TekGrid({
262
+ name: 'grid',
263
+ component: 'TekGrid',
264
+ columns: [
265
+ { name: 'id' },
266
+ { name: 'name' },
267
+ { name: 'salary' },
268
+ ],
269
+ events: {
270
+ loadLayouts: async () => layouts,
271
+ },
272
+ });
273
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
274
+ instance.parent = tag;
275
+ tag.parent = grid;
276
+
277
+ await instance.onMounted({} as HTMLElement);
278
+
279
+ expect(instance.currentLayoutName).toBe('Name first');
280
+ expect(instance.layoutNames).toEqual(['Name first']);
281
+ expect(instance.layouts).toEqual(layouts.layouts);
282
+ });
283
+
284
+ it('should load using localStorage', async () => {
285
+ const tag = new Tag({
286
+ name: 'toolbar',
287
+ component: 'ZdTag',
288
+ tag: 'span',
289
+ });
290
+ const grid = new TekGrid({
291
+ name: 'grid',
292
+ component: 'TekGrid',
293
+ columns: [
294
+ { name: 'id' },
295
+ { name: 'name' },
296
+ { name: 'salary' },
297
+ ],
298
+ });
299
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
300
+ instance.parent = tag;
301
+ tag.parent = grid;
302
+
303
+ const localStorageMock = (function () { // eslint-disable-line func-names
304
+ return {
305
+ getItem(): string {
306
+ return JSON.stringify(testLayouts);
307
+ },
308
+ setItem(_name: string, value: string) {
309
+ savedLayouts = value;
310
+ },
311
+ };
312
+ }());
313
+
314
+ Object.defineProperty(window, 'localStorage', {
315
+ value: localStorageMock,
316
+ });
317
+
318
+ await instance.onMounted({} as HTMLElement);
319
+
320
+ expect(instance.currentLayoutName).toBe('');
321
+ expect(instance.layoutNames).toEqual(['Name first']);
322
+ expect(instance.layouts).toEqual(testLayouts.layouts);
323
+ });
324
+ });
325
+
326
+ describe('newLayout()', () => {
327
+ it('should add new layout', () => {
328
+ const newLayout = {
329
+ name: 'Name first',
330
+ gridWidth: '544px',
331
+ columns: [
332
+ {
333
+ name: 'id',
334
+ width: '34px',
335
+ isVisible: true,
336
+ grouped: false,
337
+ },
338
+ {
339
+ name: 'name',
340
+ width: '107px',
341
+ isVisible: true,
342
+ grouped: false,
343
+ },
344
+ {
345
+ name: 'salary',
346
+ width: '114px',
347
+ isVisible: true,
348
+ grouped: false,
349
+ },
350
+ ],
351
+ };
352
+ const tag = new Tag({
353
+ name: 'toolbar',
354
+ component: 'ZdTag',
355
+ tag: 'span',
356
+ });
357
+ const grid = new TekGrid({
358
+ name: 'grid',
359
+ component: 'TekGrid',
360
+ columns: [
361
+ { name: 'id' },
362
+ { name: 'name' },
363
+ { name: 'salary' },
364
+ ],
365
+ });
366
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
367
+ instance.parent = tag;
368
+ tag.parent = grid;
369
+
370
+ instance.onMounted({} as HTMLElement);
371
+
372
+ instance.newLayout(newLayout);
373
+
374
+ expect(instance.currentLayoutName).toBe('Name first');
375
+ expect(instance.layoutNames).toEqual(['Name first']);
376
+ expect(instance.layouts).toEqual({ 'Name first': newLayout });
377
+ expect(savedLayouts).toBe(JSON.stringify({
378
+ currentLayoutName: 'Name first',
379
+ layouts: { 'Name first': newLayout },
380
+ }));
381
+ });
382
+ });
383
+
384
+ describe('addLayout()', () => {
385
+ it('should add new layout and not change current layout', () => {
386
+ const newLayout = {
387
+ name: 'Name first',
388
+ gridWidth: '544px',
389
+ columns: [
390
+ {
391
+ name: 'id',
392
+ width: '34px',
393
+ isVisible: true,
394
+ grouped: false,
395
+ },
396
+ {
397
+ name: 'name',
398
+ width: '107px',
399
+ isVisible: true,
400
+ grouped: false,
401
+ },
402
+ {
403
+ name: 'salary',
404
+ width: '114px',
405
+ isVisible: true,
406
+ grouped: false,
407
+ },
408
+ ],
409
+ };
410
+ const tag = new Tag({
411
+ name: 'toolbar',
412
+ component: 'ZdTag',
413
+ tag: 'span',
414
+ });
415
+ const grid = new TekGrid({
416
+ name: 'grid',
417
+ component: 'TekGrid',
418
+ columns: [
419
+ { name: 'id' },
420
+ { name: 'name' },
421
+ { name: 'salary' },
422
+ ],
423
+ });
424
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
425
+ instance.parent = tag;
426
+ tag.parent = grid;
427
+
428
+ instance.onMounted({} as HTMLElement);
429
+
430
+ instance.addLayout(newLayout);
431
+
432
+ expect(instance.currentLayoutName).toBe('');
433
+ expect(instance.layoutNames).toEqual(['Name first']);
434
+ expect(instance.layouts).toEqual({ 'Name first': newLayout });
435
+ expect(savedLayouts).toBe(JSON.stringify({
436
+ currentLayoutName: '',
437
+ layouts: { 'Name first': newLayout },
438
+ }));
439
+ });
440
+ });
441
+
442
+ describe('applyLayout()', () => {
443
+ it('should change Layout and save layout', async () => {
444
+ let viewCalled = false;
445
+ const tag = new Tag({
446
+ name: 'toolbar',
447
+ component: 'ZdTag',
448
+ tag: 'span',
449
+ });
450
+ const grid = new TekGrid({
451
+ name: 'grid',
452
+ component: 'TekGrid',
453
+ columns: [
454
+ { name: 'id' },
455
+ { name: 'name' },
456
+ { name: 'salary' },
457
+ ],
458
+ });
459
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
460
+ instance.viewApplyLayout = () => { viewCalled = true; };
461
+ instance.parent = tag;
462
+ tag.parent = grid;
463
+
464
+ await instance.onMounted({} as HTMLElement);
465
+
466
+ expect(instance.currentLayoutName).toBe('');
467
+ instance.applyLayout('Name first');
468
+ expect(instance.currentLayoutName).toBe('Name first');
469
+ expect(viewCalled).toBeTruthy();
470
+ });
471
+
472
+ it('should change Layout and save layout using event', async () => {
473
+ let eventSavedLayouts: ITekGridLayoutInfo = {};
474
+ const tag = new Tag({
475
+ name: 'toolbar',
476
+ component: 'ZdTag',
477
+ tag: 'span',
478
+ });
479
+ const grid = new TekGrid({
480
+ name: 'grid',
481
+ component: 'TekGrid',
482
+ columns: [
483
+ { name: 'id' },
484
+ { name: 'name' },
485
+ { name: 'salary' },
486
+ ],
487
+ events: {
488
+ saveLayouts: ({ layouts }) => { eventSavedLayouts = layouts; },
489
+ },
490
+ });
491
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
492
+ instance.parent = tag;
493
+ tag.parent = grid;
494
+
495
+ await instance.onMounted({} as HTMLElement);
496
+
497
+ expect(instance.currentLayoutName).toBe('');
498
+ instance.applyLayout('Name first');
499
+ expect(instance.currentLayoutName).toBe('Name first');
500
+ expect(eventSavedLayouts.currentLayoutName).toBe('Name first');
501
+ });
502
+ });
503
+
504
+ describe('deleteLayout()', () => {
505
+ it('should delete layout and save', async () => {
506
+ const tag = new Tag({
507
+ name: 'toolbar',
508
+ component: 'ZdTag',
509
+ tag: 'span',
510
+ });
511
+ const grid = new TekGrid({
512
+ name: 'grid',
513
+ component: 'TekGrid',
514
+ columns: [
515
+ { name: 'id' },
516
+ { name: 'name' },
517
+ { name: 'salary' },
518
+ ],
519
+ });
520
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
521
+ instance.parent = tag;
522
+ tag.parent = grid;
523
+
524
+ await instance.onMounted({} as HTMLElement);
525
+
526
+ instance.applyLayout('Name first');
527
+ expect(instance.currentLayoutName).toBe('Name first');
528
+ expect(instance.layoutNames).toEqual(['Name first']);
529
+ instance.deleteLayout('Name first');
530
+ expect(instance.currentLayoutName).toBe('');
531
+ expect(instance.layoutNames).toEqual([]);
532
+ });
533
+
534
+ it('should delete layout without applying layout', async () => {
535
+ const tag = new Tag({
536
+ name: 'toolbar',
537
+ component: 'ZdTag',
538
+ tag: 'span',
539
+ });
540
+ const grid = new TekGrid({
541
+ name: 'grid',
542
+ component: 'TekGrid',
543
+ columns: [
544
+ { name: 'id' },
545
+ { name: 'name' },
546
+ { name: 'salary' },
547
+ ],
548
+ });
549
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
550
+ instance.parent = tag;
551
+ tag.parent = grid;
552
+
553
+ await instance.onMounted({} as HTMLElement);
554
+
555
+ expect(instance.currentLayoutName).toBe('');
556
+ expect(instance.layoutNames).toEqual(['Name first']);
557
+ instance.deleteLayout('Name first');
558
+ expect(instance.currentLayoutName).toBe('');
559
+ expect(instance.layoutNames).toEqual([]);
560
+ });
561
+ });
562
+
563
+ describe('updateLayout()', () => {
564
+ it('should update layout and save', async () => {
565
+ const tag = new Tag({
566
+ name: 'toolbar',
567
+ component: 'ZdTag',
568
+ tag: 'span',
569
+ });
570
+ const grid = new TekGrid({
571
+ name: 'grid',
572
+ component: 'TekGrid',
573
+ columns: [
574
+ { name: 'id' },
575
+ { name: 'name' },
576
+ { name: 'salary' },
577
+ ],
578
+ });
579
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
580
+ instance.parent = tag;
581
+ tag.parent = grid;
582
+
583
+ await instance.onMounted({} as HTMLElement);
584
+
585
+ instance.updateLayout('Name first', {
586
+ name: 'Name first',
587
+ gridWidth: '500px',
588
+ filter: {
589
+ name: 'a',
590
+ },
591
+ dynamicFilter: {
592
+ name: [
593
+ {
594
+ operation: 'CONTAINS',
595
+ relation: 'AND',
596
+ value: 'a',
597
+ },
598
+ ],
599
+ },
600
+ });
601
+
602
+ expect(instance.currentLayoutName).toBe('Name first');
603
+ expect(instance.layoutNames).toEqual(['Name first']);
604
+ expect(instance.layouts).toEqual({
605
+ 'Name first': {
606
+ name: 'Name first',
607
+ gridWidth: '500px',
608
+ columns: instance.originalColumnProps,
609
+ filter: {
610
+ name: 'a',
611
+ },
612
+ dynamicFilter: {
613
+ name: [
614
+ {
615
+ operation: 'CONTAINS',
616
+ relation: 'AND',
617
+ value: 'a',
618
+ },
619
+ ],
620
+ },
621
+ },
622
+ });
623
+ });
624
+ });
625
+
626
+ describe('discardChanges()', () => {
627
+ it('should discard changes and restore layout', async () => {
628
+ const layouts: ITekGridLayoutInfo = {
629
+ currentLayoutName: 'Name first',
630
+ layouts: {
631
+ 'Name first': {
632
+ name: 'Name first',
633
+ gridWidth: '544px',
634
+ columns: [
635
+ {
636
+ name: 'id',
637
+ width: '34px',
638
+ isVisible: true,
639
+ grouped: false,
640
+ },
641
+ {
642
+ name: 'name',
643
+ width: '107px',
644
+ isVisible: true,
645
+ grouped: false,
646
+ },
647
+ {
648
+ name: 'salary',
649
+ width: '114px',
650
+ isVisible: true,
651
+ grouped: false,
652
+ },
653
+ ],
654
+ },
655
+ },
656
+ };
657
+
658
+ let viewCalled = false;
659
+
660
+ const tag = new Tag({
661
+ name: 'toolbar',
662
+ component: 'ZdTag',
663
+ tag: 'span',
664
+ });
665
+ const grid = new TekGrid({
666
+ name: 'grid',
667
+ component: 'TekGrid',
668
+ columns: [
669
+ { name: 'id' },
670
+ { name: 'name' },
671
+ { name: 'salary' },
672
+ ],
673
+ events: {
674
+ loadLayouts: async () => layouts,
675
+ },
676
+ });
677
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
678
+ instance.viewApplyLayout = () => { viewCalled = true; };
679
+ instance.parent = tag;
680
+ tag.parent = grid;
681
+
682
+ await instance.onMounted({} as HTMLElement);
683
+ instance.applyLayout('Name first');
684
+ expect(viewCalled).toBeTruthy();
685
+ expect(instance.layoutEdited).toBeFalsy();
686
+ expect(instance.currentLayoutName).toBe('Name first');
687
+
688
+ instance.layoutEdited = true;
689
+
690
+ instance.discardChanges();
691
+ expect(instance.layoutEdited).toBeFalsy();
692
+ });
693
+ });
694
+
695
+ describe('updateDefaultLayout()', () => {
696
+ it('should update default layout', async () => {
697
+ const tag = new Tag({
698
+ name: 'toolbar',
699
+ component: 'ZdTag',
700
+ tag: 'span',
701
+ });
702
+ const grid = new TekGrid({
703
+ name: 'grid',
704
+ component: 'TekGrid',
705
+ columns: [
706
+ { name: 'id' },
707
+ { name: 'name' },
708
+ { name: 'salary' },
709
+ ],
710
+ });
711
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
712
+ instance.parent = tag;
713
+ tag.parent = grid;
714
+
715
+ await instance.onMounted({} as HTMLElement);
716
+
717
+ expect(instance.originalDatasourceOrder).toEqual([]);
718
+ expect(instance.originalDatasourceFilter).toEqual({});
719
+ expect(instance.originalDatasourceDynamicFilter).toEqual({});
720
+ expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['id', 'name', 'salary']);
721
+
722
+ const newDynamicFilter = {
723
+ name: [
724
+ {
725
+ operation: 'CONTAINS',
726
+ relation: 'AND',
727
+ value: 'a',
728
+ },
729
+ ],
730
+ };
731
+
732
+ instance.updateDefaultLayout({
733
+ name: '',
734
+ columns: [
735
+ { name: 'name' },
736
+ { name: 'id' },
737
+ { name: 'hire_date' },
738
+ ],
739
+ order: ['name.asc'],
740
+ filter: {
741
+ name: 'a',
742
+ },
743
+ dynamicFilter: newDynamicFilter,
744
+ });
745
+
746
+ expect(instance.originalDatasourceOrder).toEqual(['name.asc']);
747
+ expect(instance.originalDatasourceFilter).toEqual({ name: 'a' });
748
+ expect(instance.originalDatasourceDynamicFilter).toEqual(newDynamicFilter);
749
+ expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['name', 'id', 'hire_date', 'salary']);
750
+ });
751
+
752
+ it('should update default layout with some values', async () => {
753
+ const tag = new Tag({
754
+ name: 'toolbar',
755
+ component: 'ZdTag',
756
+ tag: 'span',
757
+ });
758
+ const grid = new TekGrid({
759
+ name: 'grid',
760
+ component: 'TekGrid',
761
+ columns: [
762
+ { name: 'id' },
763
+ { name: 'name' },
764
+ { name: 'salary' },
765
+ ],
766
+ });
767
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
768
+ instance.parent = tag;
769
+ tag.parent = grid;
770
+
771
+ await instance.onMounted({} as HTMLElement);
772
+
773
+ const applyLayoutSpy = jest.spyOn(instance, 'applyLayout');
774
+
775
+ expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['id', 'name', 'salary']);
776
+
777
+ instance.updateDefaultLayout({
778
+ name: 'default',
779
+ columns: [
780
+ { name: 'name' },
781
+ { name: 'id' },
782
+ { name: 'hire_date' },
783
+ ],
784
+ });
785
+
786
+ expect(instance.originalColumnProps.map((item) => item.name)).toEqual(['name', 'id', 'hire_date', 'salary']);
787
+ expect(applyLayoutSpy).toBeCalled();
788
+ });
789
+ });
790
+
791
+ describe('loadLayouts()/saveLayouts()', () => {
792
+ it('should load layouts using config endpoint', async () => {
793
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
794
+ const tag = new Tag({
795
+ name: 'toolbar',
796
+ component: 'ZdTag',
797
+ tag: 'span',
798
+ });
799
+ const grid = new TekGrid({
800
+ name: 'grid',
801
+ component: 'TekGrid',
802
+ columns: [
803
+ { name: 'id' },
804
+ { name: 'name' },
805
+ { name: 'salary' },
806
+ ],
807
+ });
808
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
809
+ const flushPromises = () => new Promise(setImmediate);
810
+ instance.parent = tag;
811
+ tag.parent = grid;
812
+
813
+ const layouts = {
814
+ currentLayoutName: 'layout-test',
815
+ layouts: {
816
+ 'layout-test': {
817
+ name: 'layout-test',
818
+ gridWidth: '100px',
819
+ columns: [
820
+ {
821
+ name: 'id',
822
+ label: 'ID',
823
+ },
824
+ ],
825
+ },
826
+ },
827
+ };
828
+
829
+ const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: { layouts } } }));
830
+
831
+ await instance.onMounted({} as HTMLElement);
832
+ await grid.onMounted({} as HTMLElement);
833
+
834
+ await flushPromises();
835
+ expect(spy).toBeCalled();
836
+ expect(instance.layouts).toEqual({
837
+ 'layout-test': {
838
+ name: 'layout-test',
839
+ gridWidth: '100px',
840
+ columns: [
841
+ {
842
+ name: 'id',
843
+ label: 'ID',
844
+ },
845
+ ],
846
+ },
847
+ });
848
+ spy.mockReset();
849
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '';
850
+ });
851
+
852
+ it('should load layouts using config endpoint with array data', async () => {
853
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
854
+ const tag = new Tag({
855
+ name: 'toolbar',
856
+ component: 'ZdTag',
857
+ tag: 'span',
858
+ });
859
+ const grid = new TekGrid({
860
+ name: 'grid',
861
+ component: 'TekGrid',
862
+ columns: [
863
+ { name: 'id' },
864
+ { name: 'name' },
865
+ { name: 'salary' },
866
+ ],
867
+ });
868
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
869
+ const flushPromises = () => new Promise(setImmediate);
870
+ instance.parent = tag;
871
+ tag.parent = grid;
872
+
873
+ const layouts = {
874
+ currentLayoutName: 'layout-test',
875
+ layouts: {
876
+ 'layout-test': {
877
+ name: 'layout-test',
878
+ gridWidth: '100px',
879
+ columns: [
880
+ {
881
+ name: 'id',
882
+ label: 'ID',
883
+ },
884
+ ],
885
+ },
886
+ },
887
+ };
888
+
889
+ const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: [{ layouts }] } }));
890
+
891
+ await instance.onMounted({} as HTMLElement);
892
+ await grid.onMounted({} as HTMLElement);
893
+
894
+ await flushPromises();
895
+ expect(spy).toBeCalled();
896
+ expect(instance.layouts).toEqual({
897
+ 'layout-test': {
898
+ name: 'layout-test',
899
+ gridWidth: '100px',
900
+ columns: [
901
+ {
902
+ name: 'id',
903
+ label: 'ID',
904
+ },
905
+ ],
906
+ },
907
+ });
908
+ spy.mockReset();
909
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '';
910
+ });
911
+
912
+ it('should load layouts using config endpoint with empty data', async () => {
913
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '/loadLayoutsRoute';
914
+ const tag = new Tag({
915
+ name: 'toolbar',
916
+ component: 'ZdTag',
917
+ tag: 'span',
918
+ });
919
+ const grid = new TekGrid({
920
+ name: 'grid',
921
+ component: 'TekGrid',
922
+ columns: [
923
+ { name: 'id' },
924
+ { name: 'name' },
925
+ { name: 'salary' },
926
+ ],
927
+ });
928
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
929
+ const flushPromises = () => new Promise(setImmediate);
930
+ instance.parent = tag;
931
+ tag.parent = grid;
932
+
933
+ const spy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({ data: { data: '' } }));
934
+
935
+ await instance.onMounted({} as HTMLElement);
936
+ await grid.onMounted({} as HTMLElement);
937
+
938
+ await flushPromises();
939
+ expect(spy).toBeCalled();
940
+ expect(instance.layouts).toEqual({});
941
+ spy.mockReset();
942
+ (Config as ITekConfig).loadGridLayoutsEndPoint = '';
943
+ });
944
+
945
+ it('should save layout using endpoint', async () => {
946
+ (Config as ITekConfig).saveGridLayoutsEndPoint = '/saveLayoutsRoute';
947
+ const tag = new Tag({
948
+ name: 'toolbar',
949
+ component: 'ZdTag',
950
+ tag: 'span',
951
+ });
952
+ const grid = new TekGrid({
953
+ name: 'grid',
954
+ component: 'TekGrid',
955
+ columns: [
956
+ { name: 'id' },
957
+ { name: 'name' },
958
+ { name: 'salary' },
959
+ ],
960
+ });
961
+ const layouts = {
962
+ name: 'layout-test',
963
+ gridWidth: '100px',
964
+ columns: [
965
+ {
966
+ name: 'id',
967
+ label: 'ID',
968
+ },
969
+ ],
970
+ };
971
+ const instance = new TekGridLayoutOptions({ name: 'layout', component: 'TekGridLayoutOptions' });
972
+ instance.grid = grid;
973
+ instance.parent = tag;
974
+ tag.parent = grid;
975
+ instance.newLayout(layouts);
976
+ expect(instance.currentLayoutName).toBe('layout-test');
977
+ (Config as ITekConfig).saveGridLayoutsEndPoint = '';
978
+ });
979
+ });
980
+ });