@zeedhi/teknisa-components-common 1.88.0 → 1.90.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.
@@ -0,0 +1,846 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+
3
+ import {
4
+ Button, Date, Form, IButton, IForm, IModal, Modal, ModalService,
5
+ } from '@zeedhi/common';
6
+ import {
7
+ Http, Metadata, DateHelper,
8
+ } from '@zeedhi/core';
9
+ import {
10
+ IFilterPropsComponent,
11
+ ITekGridFilterButton,
12
+ TekGrid, TekGridColumn, TekGridFilterButton, TekRestDatasource,
13
+ } from '../../../../src';
14
+ import { setClick, getChild } from '../../../__helpers__';
15
+
16
+ jest.mock('lodash.debounce', () => jest.fn((fn) => fn));
17
+
18
+ jest.useFakeTimers();
19
+
20
+ const clickOnFilterButton = (grid: TekGrid, event?: any) => {
21
+ const buttonProps = getChild<ITekGridFilterButton>(grid.toolbarSlot, `${grid.name}_filterButton`);
22
+ const filterButton = new TekGridFilterButton(buttonProps);
23
+ setClick(filterButton, event);
24
+
25
+ return filterButton;
26
+ };
27
+
28
+ describe('TekGridFilterButton', () => {
29
+ beforeEach(() => {
30
+ // clear all metadata instances before testing
31
+ const { instances } = Metadata as any;
32
+ Object.keys(instances).forEach((key) => {
33
+ Metadata.clearInstance(key, instances[key].componentId);
34
+ });
35
+
36
+ jest.clearAllTimers();
37
+ });
38
+
39
+ describe('loadGrid()', () => {
40
+ it('should load grid by the gridName', () => {
41
+ const instance = new TekGrid({
42
+ name: 'grid-1',
43
+ component: 'TekGrid',
44
+ });
45
+ instance.onCreated();
46
+
47
+ const filterButton = new TekGridFilterButton({
48
+ name: 'columns-button-1',
49
+ component: 'TekGridColumnsButton',
50
+ gridName: 'grid-1',
51
+ });
52
+
53
+ expect(filterButton.grid).toEqual(instance);
54
+ });
55
+
56
+ it('should load grid by the gridName', () => {
57
+ const instance = new TekGrid({
58
+ name: 'grid-1',
59
+ component: 'TekGrid',
60
+ });
61
+ instance.onCreated();
62
+
63
+ const filterButton = new TekGridFilterButton({
64
+ name: 'columns-button-1',
65
+ component: 'TekGridColumnsButton',
66
+ grid: instance,
67
+ });
68
+
69
+ expect(filterButton.grid).toEqual(instance);
70
+ });
71
+
72
+ it('should load grid by the gridName', () => {
73
+ const instance = new TekGrid({
74
+ name: 'grid-1',
75
+ component: 'TekGrid',
76
+ });
77
+ instance.onCreated();
78
+ const filterButton = new TekGridFilterButton({
79
+ name: 'columns-button-1',
80
+ component: 'TekGridColumnsButton',
81
+ });
82
+
83
+ filterButton.loadGrid('grid-1');
84
+
85
+ expect(filterButton.grid).toEqual(instance);
86
+ });
87
+ });
88
+
89
+ describe('filterClick()', () => {
90
+ it('should call createFilterFromColumns', () => {
91
+ const instance = new TekGrid({
92
+ name: 'grid_filterClick1',
93
+ component: 'TekGrid',
94
+ });
95
+
96
+ const formInstance = new Form({
97
+ name: 'form',
98
+ component: 'ZdForm',
99
+ });
100
+
101
+ const spy = jest.spyOn(ModalService, 'create');
102
+ const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => formInstance);
103
+ instance.onCreated();
104
+
105
+ const button = clickOnFilterButton(instance);
106
+ button.onCreated();
107
+
108
+ expect(spy).toBeCalledTimes(1);
109
+ button.click({} as Event);
110
+ expect(spy).toBeCalledTimes(1);
111
+ expect(button.isVisible).toBeFalsy();
112
+ (instance as any).gridBase.hideFilterModal();
113
+
114
+ button.onBeforeDestroy();
115
+
116
+ spy.mockReset();
117
+ spyMetadata.mockReset();
118
+ });
119
+
120
+ it('should call events', () => {
121
+ let filterClickCalled = false;
122
+ const instance = new TekGrid({
123
+ name: 'grid_filterClick2',
124
+ component: 'TekGrid',
125
+ events: {
126
+ filterClick: () => { filterClickCalled = true; },
127
+ },
128
+ });
129
+
130
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal) => new Modal(modal));
131
+
132
+ instance.onCreated();
133
+ clickOnFilterButton(instance);
134
+
135
+ expect(filterClickCalled).toBeTruthy();
136
+
137
+ spy.mockReset();
138
+ });
139
+
140
+ it('should not call saveEditedRows if filterClick call preventDefault', () => {
141
+ let filterClickCalled = false;
142
+ const instance = new TekGrid({
143
+ name: 'grid_filterClick3',
144
+ component: 'TekGrid',
145
+ events: {
146
+ filterClick: () => { filterClickCalled = true; },
147
+ },
148
+ });
149
+
150
+ const spy = jest.spyOn(ModalService, 'create');
151
+
152
+ instance.onCreated();
153
+ clickOnFilterButton(instance, { defaultPrevented: true });
154
+
155
+ expect(filterClickCalled).toBeTruthy();
156
+ expect(spy).not.toBeCalled();
157
+
158
+ spy.mockReset();
159
+ });
160
+
161
+ it('should create modal with no form elements', () => {
162
+ const instance = new TekGrid({
163
+ name: 'grid_filterClick4',
164
+ component: 'TekGrid',
165
+ columns: [
166
+ { name: 'id' },
167
+ { name: 'name', filterable: false, filterProps: { name: 'name_edit', label: 'name' } },
168
+ ],
169
+ });
170
+
171
+ let formElements: any = [];
172
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
173
+ const form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
174
+ formElements = form.children;
175
+
176
+ return new Modal(modal);
177
+ });
178
+ instance.onCreated();
179
+ clickOnFilterButton(instance);
180
+
181
+ expect(formElements).toEqual([]);
182
+ spy.mockReset();
183
+ });
184
+
185
+ it('should create modal with filterable form elements', () => {
186
+ const instance = new TekGrid({
187
+ name: 'grid_filterClick5',
188
+ component: 'TekGrid',
189
+ columns: [
190
+ { name: 'id' },
191
+ { name: 'name', filterable: true, filterProps: [{ name: 'name_edit', label: 'name' }] },
192
+ ],
193
+ });
194
+
195
+ let formElements: any = [];
196
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
197
+ const form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
198
+ formElements = form.children;
199
+
200
+ return new Modal(modal);
201
+ });
202
+ instance.onCreated();
203
+ clickOnFilterButton(instance);
204
+
205
+ expect(formElements[0].name).toBe('grid_filterClick5-filter-AND-CONTAINS-name-0');
206
+
207
+ spy.mockReset();
208
+ });
209
+
210
+ it('should create modal with filterable and ordered form elements', () => {
211
+ const instance = new TekGrid({
212
+ name: 'grid-filter-order',
213
+ component: 'TekGrid',
214
+ columns: [
215
+ { name: 'id' },
216
+ {
217
+ name: 'first_name',
218
+ filterable: true,
219
+ filterProps: [{ name: 'first_name_edit', label: 'first_name' }],
220
+ },
221
+ {
222
+ name: 'last_name',
223
+ filterable: true,
224
+ filterIndex: 1,
225
+ filterProps: [{ name: 'last_name_edit', label: 'last_name' }],
226
+ },
227
+ {
228
+ name: 'dob',
229
+ filterable: true,
230
+ filterIndex: 0,
231
+ filterProps: [{ name: 'dob_edit', label: 'dob' }],
232
+ },
233
+ ],
234
+ });
235
+
236
+ let formElements: any = [];
237
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
238
+ const form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
239
+ formElements = form.children;
240
+
241
+ return new Modal(modal);
242
+ });
243
+ instance.onCreated();
244
+
245
+ clickOnFilterButton(instance);
246
+
247
+ expect(formElements[0].name).toBe('grid-filter-order-filter-AND-CONTAINS-dob-0');
248
+ expect(formElements[1].name).toBe('grid-filter-order-filter-AND-CONTAINS-last_name-0');
249
+ expect(formElements[2].name).toBe('grid-filter-order-filter-AND-CONTAINS-first_name-0');
250
+
251
+ spy.mockReset();
252
+ });
253
+
254
+ it('should create modal with filterable form elements and TekRestDatasource', () => {
255
+ const instance = new TekGrid({
256
+ name: 'grid_filterClick6',
257
+ component: 'TekGrid',
258
+ datasource: {
259
+ type: 'tek-rest',
260
+ lazyLoad: true,
261
+ },
262
+ columns: [
263
+ {
264
+ name: 'id',
265
+ filterProps: [{ name: 'id_edit' }],
266
+ },
267
+ {
268
+ name: 'name',
269
+ componentProps: { name: 'name_edit', component: 'ZdSelect' },
270
+ filterProps: [{
271
+ name: 'name_edit', label: 'name', operation: 'NOT_EQUALS', relation: 'OR',
272
+ }],
273
+ },
274
+ ],
275
+ });
276
+
277
+ let formElements: any = [];
278
+ let form: IForm;
279
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
280
+ form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
281
+ formElements = form.children;
282
+
283
+ return new Modal(modal);
284
+ });
285
+ instance.onCreated();
286
+
287
+ clickOnFilterButton(instance);
288
+
289
+ expect(formElements[0].name).toBe('grid_filterClick6-filter-AND-CONTAINS-id-0');
290
+ expect(formElements[1].name).toBe('grid_filterClick6-filter-OR-NOT_EQUALS-name-0');
291
+ expect(formElements[1].component).toBe('ZdSelect');
292
+
293
+ spy.mockReset();
294
+ });
295
+
296
+ it('should create modal with helper component', () => {
297
+ const instance = new TekGrid({
298
+ name: 'grid_filterClick7',
299
+ component: 'TekGrid',
300
+ datasource: {
301
+ type: 'tek-rest',
302
+ lazyLoad: true,
303
+ },
304
+ columns: [
305
+ {
306
+ name: 'id',
307
+ filterProps: [{ name: 'id_edit' }],
308
+ },
309
+ {
310
+ name: 'date',
311
+ componentProps: { name: 'date_edit', component: 'ZdDate' },
312
+ filterProps: [
313
+ {
314
+ name: 'date_edit1',
315
+ label: 'date1',
316
+ operation: 'NOT_EQUALS',
317
+ relation: 'OR',
318
+ helperOptions: [
319
+ 'TODAY',
320
+ 'TOMORROW',
321
+ ],
322
+ },
323
+ {
324
+ name: 'date_edit2',
325
+ label: 'date2',
326
+ showLabel: false,
327
+ operation: 'NOT_EQUALS',
328
+ relation: 'OR',
329
+ helperOptions: [
330
+ 'TODAY',
331
+ 'TOMORROW',
332
+ ],
333
+ },
334
+ ],
335
+ },
336
+ ],
337
+ });
338
+
339
+ let formElements: any = [];
340
+ let form: IForm;
341
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
342
+ form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
343
+ formElements = form.children;
344
+
345
+ return new Modal(modal);
346
+ });
347
+ const dateHelperSpy = jest.spyOn(DateHelper, 'getLabel').mockImplementation((name: string) => name);
348
+
349
+ instance.onCreated();
350
+ clickOnFilterButton(instance);
351
+
352
+ expect(formElements[0].name).toBe('grid_filterClick7-filter-AND-CONTAINS-id-0');
353
+ expect(formElements[1].name).toBe('grid_filterClick7-filter-OR-NOT_EQUALS-date-0');
354
+ expect(formElements[2].name).toBe('grid_filterClick7-filter-OR-NOT_EQUALS-date-1');
355
+
356
+ spy.mockReset();
357
+ dateHelperSpy.mockReset();
358
+ });
359
+
360
+ it('should apply helper value', () => {
361
+ const instance = new TekGrid({
362
+ name: 'grid_filterClick8',
363
+ component: 'TekGrid',
364
+ datasource: {
365
+ type: 'tek-rest',
366
+ lazyLoad: true,
367
+ },
368
+ columns: [
369
+ {
370
+ name: 'date1',
371
+ componentProps: { name: 'date_edit1', component: 'ZdDate' },
372
+ filterProps: {
373
+ name: 'date_edit1',
374
+ label: 'date1',
375
+ operation: 'NOT_EQUALS',
376
+ relation: 'OR',
377
+ helperOptions: [
378
+ 'TODAY',
379
+ 'TOMORROW',
380
+ ],
381
+ },
382
+ },
383
+ {
384
+ name: 'date2',
385
+ componentProps: { name: 'date_edit2', component: 'ZdDate' },
386
+ filterProps: [
387
+ {
388
+ name: 'date_edit21',
389
+ label: 'date21',
390
+ operation: 'NOT_EQUALS',
391
+ relation: 'OR',
392
+ helperOptions: [
393
+ 'TODAY',
394
+ 'TOMORROW',
395
+ ],
396
+ },
397
+ {
398
+ name: 'date_edit22',
399
+ label: 'date22',
400
+ helperOptions: [
401
+ 'TODAY',
402
+ 'TOMORROW',
403
+ ],
404
+ },
405
+ ],
406
+ },
407
+ ],
408
+ });
409
+
410
+ let formElements: any = [];
411
+ let form: IForm;
412
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
413
+ form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
414
+ formElements = form.children;
415
+
416
+ return new Modal(modal);
417
+ });
418
+ instance.onCreated();
419
+ clickOnFilterButton(instance);
420
+
421
+ const dateComp = new Date(formElements[0]);
422
+ dateComp.helperValue = 'TODAY';
423
+ dateComp.change({} as Event, {} as HTMLElement);
424
+ expect((instance.columns[0].filterProps as IFilterPropsComponent).helperValue).toBe('TODAY');
425
+
426
+ const dateComp21 = new Date(formElements[1]);
427
+ dateComp21.helperValue = 'TODAY';
428
+ dateComp21.change({} as Event, {} as HTMLElement);
429
+ expect((instance.columns[1].filterProps as IFilterPropsComponent[])[0].helperValue).toBe('TODAY');
430
+
431
+ const dateComp22 = new Date(formElements[2]);
432
+ dateComp22.helperValue = 'TODAY';
433
+ dateComp22.change({} as Event, {} as HTMLElement);
434
+ expect((instance.columns[1].filterProps as IFilterPropsComponent[])[1].helperValue).toBe('TODAY');
435
+
436
+ spy.mockReset();
437
+ });
438
+
439
+ it('should load form values from tekdatasource', () => {
440
+ const instance = new TekGrid({
441
+ name: 'grid_filterClick9',
442
+ component: 'TekGrid',
443
+ datasource: {
444
+ type: 'tek-rest',
445
+ lazyLoad: true,
446
+ dynamicFilter: {
447
+ name: [
448
+ {
449
+ operation: 'NOT_EQUALS',
450
+ relation: 'OR',
451
+ value: 'teste',
452
+ },
453
+ ],
454
+ phone: [
455
+ {
456
+ value: '9',
457
+ },
458
+ ],
459
+ date: [
460
+ {
461
+ operation: 'IN',
462
+ relation: 'OR',
463
+ value: ['2021-11-12', '2021-12-12'],
464
+ },
465
+ ],
466
+ },
467
+ },
468
+ columns: [
469
+ {
470
+ name: 'id',
471
+ filterProps: [{ name: 'id_edit' }],
472
+ },
473
+ {
474
+ name: 'name',
475
+ componentProps: { name: 'name_edit', component: 'ZdSelect' },
476
+ filterProps: {
477
+ name: 'name_edit', label: 'name', operation: 'NOT_EQUALS', relation: 'OR',
478
+ },
479
+ },
480
+ {
481
+ name: 'phone',
482
+ filterProps: [{ name: 'phone_edit' }],
483
+ },
484
+ {
485
+ name: 'date',
486
+ filterProps: { name: 'date_edit', helperValue: 'TODAY' },
487
+ },
488
+ ],
489
+ });
490
+
491
+ let form: IForm = { name: 'form', component: 'ZdForm' };
492
+ const dateHelperSpy = jest.spyOn(DateHelper, 'getLabel').mockImplementation((name: string) => name);
493
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
494
+ form = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
495
+
496
+ return new Modal(modal);
497
+ });
498
+ instance.onCreated();
499
+
500
+ clickOnFilterButton(instance);
501
+
502
+ expect(form.name).toBe('grid_filterClick9-filter-form');
503
+ const formObject = new Form(form);
504
+ formObject.value = {
505
+ 'grid_filterClick9-filter-AND_CONTAINS-id-0': null,
506
+ 'grid_filterClick9-filter-OR-NOT_EQUALS-name-0': null,
507
+ 'grid_filterClick9-filter-AND-CONTAINS-phone-0': null,
508
+ 'grid_filterClick9-filter-OR-IN-date-0': null,
509
+ };
510
+ const dateObject = new Date({ name: 'date', component: 'ZdDate' });
511
+ const spyMetadata = jest.spyOn(Metadata, 'getInstances').mockImplementation(() => [dateObject]);
512
+ formObject.onMounted({} as HTMLElement);
513
+ expect(formObject.value).toEqual({
514
+ 'grid_filterClick9-filter-AND_CONTAINS-id-0': null,
515
+ 'grid_filterClick9-filter-OR-NOT_EQUALS-name-0': 'teste',
516
+ 'grid_filterClick9-filter-AND-CONTAINS-phone-0': '9',
517
+ 'grid_filterClick9-filter-OR-IN-date-0': ['2021-11-12', '2021-12-12'],
518
+ });
519
+ expect(dateObject.hint).toBe('TODAY');
520
+ spyMetadata.mockReset();
521
+ dateHelperSpy.mockReset();
522
+ spy.mockReset();
523
+ });
524
+
525
+ it('should load form values from datasource', () => {
526
+ const instance = new TekGrid({
527
+ name: 'grid_filterClick10',
528
+ component: 'TekGrid',
529
+ datasource: {
530
+ filter: {
531
+ id: '',
532
+ name: 'teste',
533
+ phone: '9',
534
+ },
535
+ },
536
+ columns: [
537
+ {
538
+ name: 'id',
539
+ },
540
+ {
541
+ name: 'name',
542
+ },
543
+ {
544
+ name: 'phone',
545
+ },
546
+ ],
547
+ });
548
+
549
+ let formProps: IForm = {} as IForm;
550
+ const spy = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
551
+ formProps = getChild<IForm>(modal.children || [], `${instance.name}-filter-form`);
552
+
553
+ return new Modal(modal);
554
+ });
555
+ instance.onCreated();
556
+ clickOnFilterButton(instance);
557
+
558
+ const formObject = new Form(formProps);
559
+ formObject.onMounted({} as HTMLElement);
560
+
561
+ expect(formObject.value).toEqual({
562
+ 'grid_filterClick10-filter-AND-CONTAINS-name-0': 'teste',
563
+ 'grid_filterClick10-filter-AND-CONTAINS-phone-0': '9',
564
+ });
565
+ spy.mockReset();
566
+ });
567
+
568
+ it('should apply filter from form to tekdatasource', () => {
569
+ const httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
570
+ data: {
571
+ data: [
572
+ { id: 1, name: 'employee 1', department: 1 },
573
+ { id: 2, name: 'employee 2', department: 1 },
574
+ { id: 3, name: 'employee 3', department: 1 },
575
+ { id: 4, name: 'employee 4', department: 2 },
576
+ { id: 5, name: 'employee 5', department: 2 },
577
+ ],
578
+ pagination: {
579
+ page: 1,
580
+ total: 5,
581
+ limit: 15,
582
+ },
583
+ },
584
+ }));
585
+ const instance = new TekGrid({
586
+ name: 'grid_filterClick11',
587
+ component: 'TekGrid',
588
+ datasource: {
589
+ type: 'tek-rest',
590
+ },
591
+ columns: [
592
+ {
593
+ name: 'id',
594
+ filterProps: [{ name: 'id_edit', operation: 'IN', relation: 'AND' }],
595
+ },
596
+ {
597
+ name: 'name',
598
+ componentProps: { name: 'name_edit', component: 'ZdSelect' },
599
+ filterProps: [{
600
+ name: 'name_edit', label: 'name', operation: 'NOT_EQUALS', relation: 'OR',
601
+ }],
602
+ },
603
+ {
604
+ name: 'phone',
605
+ filterProps: [{ name: 'phone_edit' }],
606
+ },
607
+ {
608
+ name: 'salary',
609
+ filterProps: [
610
+ { name: 'salary_min', operation: 'GREATER_THAN_EQUALS', relation: 'AND' },
611
+ { name: 'salary_max', operation: 'LESS_THAN_EQUALS', relation: 'AND' },
612
+ ],
613
+ },
614
+ ],
615
+ });
616
+
617
+ let form: IForm = { name: 'form', component: 'ZdForm' };
618
+ let applyButton: IButton = { name: 'apply', component: 'ZdButton' };
619
+ const spyModalService = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
620
+ form = getChild(modal.children || [], `${instance.name}-filter-form`);
621
+
622
+ const modalFooter = getChild(modal.children || [], `${instance.name}-filter-footer`);
623
+ applyButton = getChild(modalFooter.rightSlot, `${instance.name}-filter-okButton`);
624
+
625
+ return new Modal(modal);
626
+ });
627
+
628
+ instance.onCreated();
629
+
630
+ const buttonProps = getChild<ITekGridFilterButton>(instance.toolbarSlot, `${instance.name}_filterButton`);
631
+ const button = new TekGridFilterButton(buttonProps);
632
+ setClick(button);
633
+
634
+ const formObject = new Form(form);
635
+ const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => formObject);
636
+ const spyDatasourceGet = jest.spyOn(instance.datasource, 'get');
637
+ formObject.value = {
638
+ 'grid_filterClick11-filter-AND-IN-id-0': ['1', '2', '3'],
639
+ 'grid_filterClick11-filter-OR-NOT_EQUALS-name-0': 'teste',
640
+ 'grid_filterClick11-filter-AND-CONTAINS-phone-0': '',
641
+ 'grid_filterClick11-filter-AND-GREATER_THAN_EQUALS-salary-0': '1000',
642
+ 'grid_filterClick11-filter-AND-LESS_THAN_EQUALS-salary-0': '2000',
643
+ };
644
+ const applyButtonObject = new Button(applyButton);
645
+ formObject.validate = () => false;
646
+ setClick(applyButtonObject);
647
+ expect((instance.datasource as TekRestDatasource).dynamicFilter).toEqual({});
648
+
649
+ formObject.validate = () => true;
650
+ setClick(applyButtonObject);
651
+ expect((instance.datasource as TekRestDatasource).dynamicFilter).toEqual({
652
+ id: [
653
+ {
654
+ operation: 'IN',
655
+ relation: 'AND',
656
+ value: ['1', '2', '3'],
657
+ },
658
+ ],
659
+ name: [
660
+ {
661
+ operation: 'NOT_EQUALS',
662
+ relation: 'OR',
663
+ value: 'teste',
664
+ },
665
+ ],
666
+ salary: [
667
+ {
668
+ operation: 'GREATER_THAN_EQUALS',
669
+ relation: 'AND',
670
+ value: '1000',
671
+ },
672
+ {
673
+ operation: 'LESS_THAN_EQUALS',
674
+ relation: 'AND',
675
+ value: '2000',
676
+ },
677
+ ],
678
+ });
679
+ expect(instance.columnHasFilterData({ name: 'name' } as TekGridColumn)).toBeTruthy();
680
+ expect(spyDatasourceGet).toBeCalled();
681
+ spyMetadata.mockReset();
682
+ spyDatasourceGet.mockReset();
683
+ spyModalService.mockReset();
684
+ httpSpy.mockReset();
685
+ });
686
+
687
+ it('should clear data from form', () => {
688
+ const httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
689
+ data: {
690
+ data: [
691
+ { id: 1, name: 'employee 1', department: 1 },
692
+ { id: 2, name: 'employee 2', department: 1 },
693
+ { id: 3, name: 'employee 3', department: 1 },
694
+ { id: 4, name: 'employee 4', department: 2 },
695
+ { id: 5, name: 'employee 5', department: 2 },
696
+ ],
697
+ pagination: {
698
+ page: 1,
699
+ total: 5,
700
+ limit: 15,
701
+ },
702
+ },
703
+ }));
704
+ const instance = new TekGrid({
705
+ name: 'grid_filterClick12',
706
+ component: 'TekGrid',
707
+ datasource: {
708
+ type: 'tek-rest',
709
+ },
710
+ columns: [
711
+ {
712
+ name: 'id',
713
+ filterProps: [{ name: 'id_edit', operation: 'IN', relation: 'AND' }],
714
+ },
715
+ {
716
+ name: 'name',
717
+ componentProps: { name: 'name_edit', component: 'ZdSelect' },
718
+ filterProps: [{
719
+ name: 'name_edit', label: 'name', operation: 'NOT_EQUALS', relation: 'OR',
720
+ }],
721
+ },
722
+ {
723
+ name: 'phone',
724
+ filterProps: [{ name: 'phone_edit' }],
725
+ },
726
+ {
727
+ name: 'salary',
728
+ filterProps: [
729
+ { name: 'salary_min', operation: 'GREATER_THAN_EQUALS', relation: 'AND' },
730
+ { name: 'salary_max', operation: 'LESS_THAN_EQUALS', relation: 'AND' },
731
+ ],
732
+ },
733
+ ],
734
+ });
735
+
736
+ let form: IForm = { name: 'form', component: 'ZdForm' };
737
+ let clearButton: IButton = { name: 'clear', component: 'ZdButton' };
738
+ const spyModalService = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
739
+ form = getChild(modal.children || [], `${instance.name}-filter-form`);
740
+
741
+ const modalFooter = getChild(modal.children || [], `${instance.name}-filter-footer`);
742
+ clearButton = getChild(modalFooter.leftSlot, `${instance.name}-filter-clearButton`);
743
+
744
+ return new Modal(modal);
745
+ });
746
+
747
+ instance.onCreated();
748
+
749
+ const buttonProps = getChild<ITekGridFilterButton>(instance.toolbarSlot, `${instance.name}_filterButton`);
750
+ const button = new TekGridFilterButton(buttonProps);
751
+ setClick(button);
752
+
753
+ const formObject = new Form(form);
754
+ const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => formObject);
755
+ const spyDatasourceGet = jest.spyOn(instance.datasource, 'get');
756
+ formObject.value = {
757
+ 'grid_filterClick12-filter-AND-IN-id-0': ['1', '2', '3'],
758
+ 'grid_filterClick12-filter-OR-NOT_EQUALS-name-0': 'teste',
759
+ 'grid_filterClick12-filter-AND-CONTAINS-phone-0': '',
760
+ 'grid_filterClick12-filter-AND-GREATER_THAN_EQUALS-salary-0': '1000',
761
+ 'grid_filterClick12-filter-AND-LESS_THAN_EQUALS-salary-0': '2000',
762
+ };
763
+ const clearButtonObject = new Button(clearButton);
764
+ setClick(clearButtonObject);
765
+ expect(formObject.value).toEqual({
766
+ 'grid_filterClick12-filter-AND-IN-id-0': null,
767
+ 'grid_filterClick12-filter-OR-NOT_EQUALS-name-0': null,
768
+ 'grid_filterClick12-filter-AND-CONTAINS-phone-0': null,
769
+ 'grid_filterClick12-filter-AND-GREATER_THAN_EQUALS-salary-0': null,
770
+ 'grid_filterClick12-filter-AND-LESS_THAN_EQUALS-salary-0': null,
771
+ });
772
+ spyMetadata.mockReset();
773
+ spyDatasourceGet.mockReset();
774
+ spyModalService.mockReset();
775
+ httpSpy.mockReset();
776
+ });
777
+
778
+ it('should apply filter from form to datasource', () => {
779
+ let beforeApplyFilterCalled = false;
780
+ let changeLayoutCalled = false;
781
+ const instance = new TekGrid({
782
+ name: 'grid_filterClick13',
783
+ component: 'TekGrid',
784
+ columns: [
785
+ {
786
+ name: 'id',
787
+ },
788
+ {
789
+ name: 'name',
790
+ },
791
+ {
792
+ name: 'phone',
793
+ },
794
+ {
795
+ name: 'salary',
796
+ },
797
+ ],
798
+ events: {
799
+ beforeApplyFilter: () => { beforeApplyFilterCalled = true; },
800
+ changeLayout: () => { changeLayoutCalled = true; },
801
+ },
802
+ });
803
+
804
+ let form: IForm = { name: 'form', component: 'ZdForm' };
805
+ let applyButton: IButton = { name: 'apply', component: 'ZdButton' };
806
+ const spyModalService = jest.spyOn(ModalService, 'create').mockImplementation((modal: IModal) => {
807
+ form = getChild(modal.children || [], `${instance.name}-filter-form`);
808
+
809
+ const modalFooter = getChild(modal.children || [], `${instance.name}-filter-footer`);
810
+ applyButton = getChild(modalFooter.rightSlot, `${instance.name}-filter-okButton`);
811
+
812
+ return new Modal(modal);
813
+ });
814
+
815
+ instance.onCreated();
816
+
817
+ clickOnFilterButton(instance);
818
+
819
+ const formObject = new Form(form);
820
+ const spyMetadata = jest.spyOn(Metadata, 'getInstance').mockImplementation(() => formObject);
821
+ const spyDatasourceGet = jest.spyOn(instance.datasource, 'get');
822
+ formObject.value = {
823
+ 'grid_filterClick13-filter-AND-CONTAINS-id-0': '1;2;3',
824
+ 'grid_filterClick13-filter-AND-CONTAINS-name-0': 'teste',
825
+ 'grid_filterClick13-filter-AND-CONTAINS-phone-0': '',
826
+ 'grid_filterClick13-filter-AND-CONTAINS-salary-0': '1000',
827
+ };
828
+ const applyButtonObject = new Button(applyButton);
829
+ setClick(applyButtonObject);
830
+ expect(instance.datasource.filter).toEqual({
831
+ id: '1;2;3',
832
+ name: 'teste',
833
+ salary: '1000',
834
+ });
835
+ expect(beforeApplyFilterCalled).toBeTruthy();
836
+ expect(changeLayoutCalled).toBeTruthy();
837
+ expect(instance.columnHasFilterData({ name: 'name' } as TekGridColumn)).toBeTruthy();
838
+
839
+ setClick(applyButtonObject, { defaultPrevented: true });
840
+ expect(spyDatasourceGet).toBeCalledTimes(1);
841
+ spyMetadata.mockReset();
842
+ spyDatasourceGet.mockReset();
843
+ spyModalService.mockReset();
844
+ });
845
+ });
846
+ });