@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,364 +1,364 @@
1
- import {
2
- IConfig, Config, Router, Http, URL, IDictionary, init,
3
- } from '@zeedhi/core';
4
- import {
5
- TekRestDatasource, ITekRestDatasource,
6
- } from '../../../../src';
7
-
8
- describe('TekRestDatasource', () => {
9
- init({
10
- controllers: {
11
- AppController: class AppController {
12
- public value = '1';
13
-
14
- public join: string[] | number[] = ['1', '2'];
15
- },
16
- },
17
- });
18
- let datasource: ITekRestDatasource;
19
- let router: any;
20
- const config: IConfig = {
21
- endPoint: 'http://localhost:9000/',
22
- metadataEndPoint: 'http://localhost:9000/metadata/',
23
- title: 'Samples',
24
- mode: 'production',
25
- datasourceLimit: 10,
26
- };
27
- let httpSpy: jest.SpyInstance;
28
-
29
- beforeEach(() => {
30
- datasource = {
31
- uniqueKey: 'id',
32
- route: '/zeedhi',
33
- lazyLoad: true,
34
- };
35
- Config.set(config);
36
- router = {
37
- push: () => jest.fn,
38
- replace: (route: any) => {
39
- window.history.replaceState(null, '', `${window.location.pathname}?${URL.getFormattedQueryString(route.query)}`);
40
- },
41
- back: () => jest.fn,
42
- getCurrentRoute: () => ({
43
- path: '',
44
- hash: '',
45
- query: {},
46
- params: {},
47
- fullPath: '',
48
- }),
49
- getPath: () => '',
50
- getHash: () => '',
51
- getQuery: () => ({}),
52
- getParams: () => ({}),
53
- getFullPath: () => '',
54
- };
55
- Router.setInstance(router);
56
- httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
57
- data: {
58
- data: [
59
- { name: '1', id: 1 }, { name: '2', id: 2 }, { name: '3', id: 3 },
60
- { name: '4', id: 4 }, { name: '5', id: 5 }, { name: '6', id: 6 },
61
- ],
62
- pagination: {
63
- page: 1,
64
- total: 6,
65
- limit: 15,
66
- },
67
- },
68
- }));
69
- });
70
-
71
- afterEach(() => {
72
- httpSpy.mockClear();
73
- });
74
-
75
- describe('constuctor()', () => {
76
- it('should assign all properties', async () => {
77
- const flushPromises = () => new Promise(setImmediate);
78
- const instance = new TekRestDatasource({
79
- ...datasource,
80
- dynamicFilter: {
81
- name: { operation: 'EQUALS', relation: 'AND', value: '1' },
82
- },
83
- searchJoin: { col1: ['1', '2'], col2: [2], col3: [] },
84
- });
85
- await flushPromises();
86
-
87
- expect(instance.route).toBe('/zeedhi');
88
- expect(instance.lazyLoad).toBeTruthy();
89
- expect(instance.dynamicFilter).toEqual({ name: { operation: 'EQUALS', relation: 'AND', value: '1' } });
90
- expect(instance.data.length).toBe(0);
91
- expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
92
- });
93
-
94
- it('should assign all properties', async () => {
95
- const flushPromises = () => new Promise(setImmediate);
96
- const instance = new TekRestDatasource({
97
- ...datasource,
98
- dynamicFilter: {
99
- name: { operation: 'EQUALS', relation: 'AND', value: '{{AppController.value}}' },
100
- },
101
- searchJoin: { col1: '{{AppController.join}}' as any, col2: [2], col3: [] },
102
- });
103
- await flushPromises();
104
-
105
- expect(instance.route).toBe('/zeedhi');
106
- expect(instance.lazyLoad).toBeTruthy();
107
- expect(instance.dynamicFilter).toEqual({ name: { operation: 'EQUALS', relation: 'AND', value: '1' } });
108
- expect(instance.data.length).toBe(0);
109
- expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
110
- });
111
-
112
- it('should create without lazyLoad', async () => {
113
- const flushPromises = () => new Promise(setImmediate);
114
- const instance = new TekRestDatasource({
115
- ...datasource,
116
- lazyLoad: false,
117
- dynamicFilter: {
118
- name: { operation: 'EQUALS', relation: 'AND', value: '1' },
119
- },
120
- });
121
- await flushPromises();
122
-
123
- expect(instance.data.length).toBe(6);
124
- });
125
-
126
- it('should set values from url', () => {
127
- router.getFullPath = () => 'localhost/?page=2&limit=10&order=id.asc&search=2&id=1&filter=applied';
128
- datasource.watchUrl = true;
129
- const instance = new TekRestDatasource(datasource);
130
- expect(instance.filter).toEqual({ id: '1' });
131
- expect(instance.limit).toBe(10);
132
- expect(instance.order).toEqual(['id.asc']);
133
- expect(instance.page).toBe(2);
134
- expect(instance.search).toBe('2');
135
- });
136
-
137
- it('should set dynamicFilter from url', () => {
138
- router.getFullPath = () => 'localhost/?dynamic_filter=eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0='; // eslint-disable-line max-len
139
- datasource.watchUrl = true;
140
- const instance = new TekRestDatasource(datasource);
141
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
142
- });
143
-
144
- it('should set searchJoin from url', () => {
145
- router.getFullPath = () => 'localhost/?search_join=eyAiY29sMSI6IFsiMSIsICIyIl0sICJjb2wyIjogWzJdLCAiY29sMyI6IFtdIH0='; // eslint-disable-line max-len
146
- datasource.watchUrl = true;
147
- const instance = new TekRestDatasource(datasource);
148
- expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
149
- });
150
- });
151
-
152
- describe('addDynamicFilter()', () => {
153
- it('should add/remove dynamic filter value', () => {
154
- datasource.watchUrl = false;
155
- const instance = new TekRestDatasource(datasource);
156
- instance.addDynamicFilter('name', [{ operation: 'EQUALS', relation: 'AND', value: '1' }]);
157
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
158
- instance.addDynamicFilter('name', [{ operation: 'foo', relation: 'AND', value: '1' }]);
159
- expect(instance.dynamicFilter).toEqual({});
160
- });
161
- });
162
-
163
- describe('setDynamicFilter()', () => {
164
- it('should add/remove dynamic filter value', () => {
165
- datasource.watchUrl = false;
166
- const instance = new TekRestDatasource(datasource);
167
- instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
168
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
169
-
170
- instance.setDynamicFilter({ name: [{ operation: 'foo', relation: 'AND', value: '1' }] });
171
- expect(instance.dynamicFilter).toEqual({});
172
-
173
- instance.setDynamicFilter({
174
- department_id: undefined, hire_date: null, name: [], id: [{}],
175
- });
176
- expect(instance.dynamicFilter).toEqual({});
177
- });
178
-
179
- it('should add dynamic filter value with watchUrl', () => {
180
- let dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0=';
181
- router.getFullPath = () => `localhost/?dynamic_filter=${dynamicFilterValue}`;
182
- const urlSpy = jest.spyOn(URL, 'updateQueryString');
183
-
184
- datasource.watchUrl = true;
185
- const instance = new TekRestDatasource(datasource);
186
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
187
-
188
- dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMiJ9XX0=';
189
- instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '2' }] });
190
- expect(urlSpy).toHaveBeenCalledWith({
191
- dynamic_filter: dynamicFilterValue,
192
- });
193
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '2' }] });
194
- urlSpy.mockClear();
195
- });
196
- });
197
-
198
- describe('clearDynamicFilter()', () => {
199
- it('should clear dynamic filter', () => {
200
- datasource.watchUrl = false;
201
- const instance = new TekRestDatasource(datasource);
202
- instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
203
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
204
- instance.clearDynamicFilter();
205
- expect(instance.dynamicFilter).toEqual({});
206
- });
207
-
208
- it('should clear dynamic filter value with watchUrl', () => {
209
- let dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0=';
210
- router.getFullPath = () => `localhost/?dynamic_filter=${dynamicFilterValue}`;
211
- const urlSpy = jest.spyOn(URL, 'updateQueryString');
212
-
213
- datasource.watchUrl = true;
214
- const instance = new TekRestDatasource(datasource);
215
- expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
216
-
217
- dynamicFilterValue = '';
218
- instance.clearDynamicFilter();
219
- expect(urlSpy).toHaveBeenCalledWith({
220
- dynamic_filter: undefined,
221
- });
222
- expect(instance.dynamicFilter).toEqual({});
223
- urlSpy.mockClear();
224
- });
225
- });
226
-
227
- describe('get()', () => {
228
- it('should send dynamic filter and searchJoin on get request params', async () => {
229
- datasource.watchUrl = false;
230
- const instance = new TekRestDatasource(datasource);
231
- const dynFilter = { name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] };
232
- const searchJoin = { join: [1, 2, 3] };
233
- instance.setDynamicFilter(dynFilter);
234
- instance.searchJoin = searchJoin;
235
-
236
- await instance.get();
237
-
238
- const expectedFilter = btoa(JSON.stringify(dynFilter));
239
- const expectedSJoin = btoa(JSON.stringify(searchJoin));
240
- expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
241
- expect.objectContaining({
242
- params: expect.objectContaining({
243
- dynamic_filter: expectedFilter,
244
- search_join: expectedSJoin,
245
- }),
246
- }));
247
- });
248
-
249
- it('should send dynamic filter on get request params', async () => {
250
- datasource.watchUrl = false;
251
- const instance = new TekRestDatasource(datasource);
252
- const dynFilter = { name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] };
253
- instance.setDynamicFilter(dynFilter);
254
-
255
- await instance.get();
256
-
257
- const expectedFilter = btoa(JSON.stringify(dynFilter));
258
-
259
- // should have dynamic_filter
260
- expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
261
- expect.objectContaining({
262
- params: expect.objectContaining({
263
- dynamic_filter: expectedFilter,
264
- }),
265
- }));
266
- // should not have search_join
267
- expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
268
- expect.objectContaining({
269
- params: expect.not.objectContaining({
270
- search_join: expect.anything(),
271
- }),
272
- }));
273
- });
274
-
275
- it('should send search join on get request params', async () => {
276
- datasource.watchUrl = false;
277
- const instance = new TekRestDatasource(datasource);
278
- const searchJoin = { join: [1, 2, 3] };
279
- instance.searchJoin = searchJoin;
280
-
281
- await instance.get();
282
-
283
- const expectedSJoin = btoa(JSON.stringify(searchJoin));
284
- // should have search_join
285
- expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
286
- expect.objectContaining({
287
- params: expect.objectContaining({
288
- search_join: expectedSJoin,
289
- }),
290
- }));
291
- // should not have dynamic_filter
292
- expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
293
- expect.objectContaining({
294
- params: expect.not.objectContaining({
295
- dynamic_filter: expect.anything(),
296
- }),
297
- }));
298
- });
299
-
300
- it('should not send empty dynamic filter properties', async () => {
301
- const filter: IDictionary = {
302
- department_id: undefined, hire_date: null, name: [], id: [{}],
303
- };
304
- const instance = new TekRestDatasource(datasource);
305
- instance.dynamicFilter = filter;
306
-
307
- await instance.get();
308
-
309
- const callArgs = httpSpy.mock.calls[0]; // Captura a primeira chamada ao mock
310
- expect(callArgs[0]).toBe('/zeedhi'); // Verifica o primeiro argumento
311
- expect(callArgs[1]).toMatchObject({
312
- params: {
313
- in: [],
314
- order: [],
315
- search: undefined,
316
- search_in: [],
317
- page: 1,
318
- limit: 10,
319
- search_in_params: '{}',
320
- },
321
- });
322
- });
323
- });
324
-
325
- describe('clone', () => {
326
- it('should return an object with all datasource props', async () => {
327
- const data = [{ value: 'item 1' }];
328
- const dynamicFilter = {
329
- name: { operation: 'EQUALS', relation: 'AND', value: 'item 1' },
330
- };
331
- const searchJoin = { col1: ['1', '2'], col2: [2], col3: [] };
332
- const instance = new TekRestDatasource({
333
- route: '/zeedhi',
334
- data,
335
- dynamicFilter,
336
- searchJoin,
337
- });
338
-
339
- const props = instance.clone();
340
-
341
- expect(props).toEqual({
342
- data,
343
- dynamicFilter,
344
- searchJoin,
345
- currentRow: {},
346
- filter: {},
347
- find: {},
348
- limit: 10,
349
- order: [],
350
- page: 1,
351
- search: '',
352
- searchIn: [],
353
- uniqueKey: 'id',
354
- watchUrl: false,
355
- events: {},
356
- loadAll: false,
357
- lazyLoad: true,
358
- arrayFormat: 'repeat',
359
- route: '/zeedhi',
360
- type: 'tek-rest',
361
- });
362
- });
363
- });
364
- });
1
+ import {
2
+ IConfig, Config, Router, Http, URL, IDictionary, init,
3
+ } from '@zeedhi/core';
4
+ import {
5
+ TekRestDatasource, ITekRestDatasource,
6
+ } from '../../../../src';
7
+
8
+ describe('TekRestDatasource', () => {
9
+ init({
10
+ controllers: {
11
+ AppController: class AppController {
12
+ public value = '1';
13
+
14
+ public join: string[] | number[] = ['1', '2'];
15
+ },
16
+ },
17
+ });
18
+ let datasource: ITekRestDatasource;
19
+ let router: any;
20
+ const config: IConfig = {
21
+ endPoint: 'http://localhost:9000/',
22
+ metadataEndPoint: 'http://localhost:9000/metadata/',
23
+ title: 'Samples',
24
+ mode: 'production',
25
+ datasourceLimit: 10,
26
+ };
27
+ let httpSpy: jest.SpyInstance;
28
+
29
+ beforeEach(() => {
30
+ datasource = {
31
+ uniqueKey: 'id',
32
+ route: '/zeedhi',
33
+ lazyLoad: true,
34
+ };
35
+ Config.set(config);
36
+ router = {
37
+ push: () => jest.fn,
38
+ replace: (route: any) => {
39
+ window.history.replaceState(null, '', `${window.location.pathname}?${URL.getFormattedQueryString(route.query)}`);
40
+ },
41
+ back: () => jest.fn,
42
+ getCurrentRoute: () => ({
43
+ path: '',
44
+ hash: '',
45
+ query: {},
46
+ params: {},
47
+ fullPath: '',
48
+ }),
49
+ getPath: () => '',
50
+ getHash: () => '',
51
+ getQuery: () => ({}),
52
+ getParams: () => ({}),
53
+ getFullPath: () => '',
54
+ };
55
+ Router.setInstance(router);
56
+ httpSpy = jest.spyOn(Http, 'get').mockImplementation(() => Promise.resolve({
57
+ data: {
58
+ data: [
59
+ { name: '1', id: 1 }, { name: '2', id: 2 }, { name: '3', id: 3 },
60
+ { name: '4', id: 4 }, { name: '5', id: 5 }, { name: '6', id: 6 },
61
+ ],
62
+ pagination: {
63
+ page: 1,
64
+ total: 6,
65
+ limit: 15,
66
+ },
67
+ },
68
+ }));
69
+ });
70
+
71
+ afterEach(() => {
72
+ httpSpy.mockClear();
73
+ });
74
+
75
+ describe('constuctor()', () => {
76
+ it('should assign all properties', async () => {
77
+ const flushPromises = () => new Promise(setImmediate);
78
+ const instance = new TekRestDatasource({
79
+ ...datasource,
80
+ dynamicFilter: {
81
+ name: { operation: 'EQUALS', relation: 'AND', value: '1' },
82
+ },
83
+ searchJoin: { col1: ['1', '2'], col2: [2], col3: [] },
84
+ });
85
+ await flushPromises();
86
+
87
+ expect(instance.route).toBe('/zeedhi');
88
+ expect(instance.lazyLoad).toBeTruthy();
89
+ expect(instance.dynamicFilter).toEqual({ name: { operation: 'EQUALS', relation: 'AND', value: '1' } });
90
+ expect(instance.data.length).toBe(0);
91
+ expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
92
+ });
93
+
94
+ it('should assign all properties', async () => {
95
+ const flushPromises = () => new Promise(setImmediate);
96
+ const instance = new TekRestDatasource({
97
+ ...datasource,
98
+ dynamicFilter: {
99
+ name: { operation: 'EQUALS', relation: 'AND', value: '{{AppController.value}}' },
100
+ },
101
+ searchJoin: { col1: '{{AppController.join}}' as any, col2: [2], col3: [] },
102
+ });
103
+ await flushPromises();
104
+
105
+ expect(instance.route).toBe('/zeedhi');
106
+ expect(instance.lazyLoad).toBeTruthy();
107
+ expect(instance.dynamicFilter).toEqual({ name: { operation: 'EQUALS', relation: 'AND', value: '1' } });
108
+ expect(instance.data.length).toBe(0);
109
+ expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
110
+ });
111
+
112
+ it('should create without lazyLoad', async () => {
113
+ const flushPromises = () => new Promise(setImmediate);
114
+ const instance = new TekRestDatasource({
115
+ ...datasource,
116
+ lazyLoad: false,
117
+ dynamicFilter: {
118
+ name: { operation: 'EQUALS', relation: 'AND', value: '1' },
119
+ },
120
+ });
121
+ await flushPromises();
122
+
123
+ expect(instance.data.length).toBe(6);
124
+ });
125
+
126
+ it('should set values from url', () => {
127
+ router.getFullPath = () => 'localhost/?page=2&limit=10&order=id.asc&search=2&id=1&filter=applied';
128
+ datasource.watchUrl = true;
129
+ const instance = new TekRestDatasource(datasource);
130
+ expect(instance.filter).toEqual({ id: '1' });
131
+ expect(instance.limit).toBe(10);
132
+ expect(instance.order).toEqual(['id.asc']);
133
+ expect(instance.page).toBe(2);
134
+ expect(instance.search).toBe('2');
135
+ });
136
+
137
+ it('should set dynamicFilter from url', () => {
138
+ router.getFullPath = () => 'localhost/?dynamic_filter=eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0='; // eslint-disable-line max-len
139
+ datasource.watchUrl = true;
140
+ const instance = new TekRestDatasource(datasource);
141
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
142
+ });
143
+
144
+ it('should set searchJoin from url', () => {
145
+ router.getFullPath = () => 'localhost/?search_join=eyAiY29sMSI6IFsiMSIsICIyIl0sICJjb2wyIjogWzJdLCAiY29sMyI6IFtdIH0='; // eslint-disable-line max-len
146
+ datasource.watchUrl = true;
147
+ const instance = new TekRestDatasource(datasource);
148
+ expect(instance.searchJoin).toEqual({ col1: ['1', '2'], col2: [2], col3: [] });
149
+ });
150
+ });
151
+
152
+ describe('addDynamicFilter()', () => {
153
+ it('should add/remove dynamic filter value', () => {
154
+ datasource.watchUrl = false;
155
+ const instance = new TekRestDatasource(datasource);
156
+ instance.addDynamicFilter('name', [{ operation: 'EQUALS', relation: 'AND', value: '1' }]);
157
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
158
+ instance.addDynamicFilter('name', [{ operation: 'foo', relation: 'AND', value: '1' }]);
159
+ expect(instance.dynamicFilter).toEqual({});
160
+ });
161
+ });
162
+
163
+ describe('setDynamicFilter()', () => {
164
+ it('should add/remove dynamic filter value', () => {
165
+ datasource.watchUrl = false;
166
+ const instance = new TekRestDatasource(datasource);
167
+ instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
168
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
169
+
170
+ instance.setDynamicFilter({ name: [{ operation: 'foo', relation: 'AND', value: '1' }] });
171
+ expect(instance.dynamicFilter).toEqual({});
172
+
173
+ instance.setDynamicFilter({
174
+ department_id: undefined, hire_date: null, name: [], id: [{}],
175
+ });
176
+ expect(instance.dynamicFilter).toEqual({});
177
+ });
178
+
179
+ it('should add dynamic filter value with watchUrl', () => {
180
+ let dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0=';
181
+ router.getFullPath = () => `localhost/?dynamic_filter=${dynamicFilterValue}`;
182
+ const urlSpy = jest.spyOn(URL, 'updateQueryString');
183
+
184
+ datasource.watchUrl = true;
185
+ const instance = new TekRestDatasource(datasource);
186
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
187
+
188
+ dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMiJ9XX0=';
189
+ instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '2' }] });
190
+ expect(urlSpy).toHaveBeenCalledWith({
191
+ dynamic_filter: dynamicFilterValue,
192
+ });
193
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '2' }] });
194
+ urlSpy.mockClear();
195
+ });
196
+ });
197
+
198
+ describe('clearDynamicFilter()', () => {
199
+ it('should clear dynamic filter', () => {
200
+ datasource.watchUrl = false;
201
+ const instance = new TekRestDatasource(datasource);
202
+ instance.setDynamicFilter({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
203
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
204
+ instance.clearDynamicFilter();
205
+ expect(instance.dynamicFilter).toEqual({});
206
+ });
207
+
208
+ it('should clear dynamic filter value with watchUrl', () => {
209
+ let dynamicFilterValue = 'eyJuYW1lIjpbeyJvcGVyYXRpb24iOiJFUVVBTFMiLCJyZWxhdGlvbiI6IkFORCIsInZhbHVlIjoiMSJ9XX0=';
210
+ router.getFullPath = () => `localhost/?dynamic_filter=${dynamicFilterValue}`;
211
+ const urlSpy = jest.spyOn(URL, 'updateQueryString');
212
+
213
+ datasource.watchUrl = true;
214
+ const instance = new TekRestDatasource(datasource);
215
+ expect(instance.dynamicFilter).toEqual({ name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] });
216
+
217
+ dynamicFilterValue = '';
218
+ instance.clearDynamicFilter();
219
+ expect(urlSpy).toHaveBeenCalledWith({
220
+ dynamic_filter: undefined,
221
+ });
222
+ expect(instance.dynamicFilter).toEqual({});
223
+ urlSpy.mockClear();
224
+ });
225
+ });
226
+
227
+ describe('get()', () => {
228
+ it('should send dynamic filter and searchJoin on get request params', async () => {
229
+ datasource.watchUrl = false;
230
+ const instance = new TekRestDatasource(datasource);
231
+ const dynFilter = { name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] };
232
+ const searchJoin = { join: [1, 2, 3] };
233
+ instance.setDynamicFilter(dynFilter);
234
+ instance.searchJoin = searchJoin;
235
+
236
+ await instance.get();
237
+
238
+ const expectedFilter = btoa(JSON.stringify(dynFilter));
239
+ const expectedSJoin = btoa(JSON.stringify(searchJoin));
240
+ expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
241
+ expect.objectContaining({
242
+ params: expect.objectContaining({
243
+ dynamic_filter: expectedFilter,
244
+ search_join: expectedSJoin,
245
+ }),
246
+ }));
247
+ });
248
+
249
+ it('should send dynamic filter on get request params', async () => {
250
+ datasource.watchUrl = false;
251
+ const instance = new TekRestDatasource(datasource);
252
+ const dynFilter = { name: [{ operation: 'EQUALS', relation: 'AND', value: '1' }] };
253
+ instance.setDynamicFilter(dynFilter);
254
+
255
+ await instance.get();
256
+
257
+ const expectedFilter = btoa(JSON.stringify(dynFilter));
258
+
259
+ // should have dynamic_filter
260
+ expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
261
+ expect.objectContaining({
262
+ params: expect.objectContaining({
263
+ dynamic_filter: expectedFilter,
264
+ }),
265
+ }));
266
+ // should not have search_join
267
+ expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
268
+ expect.objectContaining({
269
+ params: expect.not.objectContaining({
270
+ search_join: expect.anything(),
271
+ }),
272
+ }));
273
+ });
274
+
275
+ it('should send search join on get request params', async () => {
276
+ datasource.watchUrl = false;
277
+ const instance = new TekRestDatasource(datasource);
278
+ const searchJoin = { join: [1, 2, 3] };
279
+ instance.searchJoin = searchJoin;
280
+
281
+ await instance.get();
282
+
283
+ const expectedSJoin = btoa(JSON.stringify(searchJoin));
284
+ // should have search_join
285
+ expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
286
+ expect.objectContaining({
287
+ params: expect.objectContaining({
288
+ search_join: expectedSJoin,
289
+ }),
290
+ }));
291
+ // should not have dynamic_filter
292
+ expect(httpSpy).toHaveBeenCalledWith('/zeedhi',
293
+ expect.objectContaining({
294
+ params: expect.not.objectContaining({
295
+ dynamic_filter: expect.anything(),
296
+ }),
297
+ }));
298
+ });
299
+
300
+ it('should not send empty dynamic filter properties', async () => {
301
+ const filter: IDictionary = {
302
+ department_id: undefined, hire_date: null, name: [], id: [{}],
303
+ };
304
+ const instance = new TekRestDatasource(datasource);
305
+ instance.dynamicFilter = filter;
306
+
307
+ await instance.get();
308
+
309
+ const callArgs = httpSpy.mock.calls[0]; // Captura a primeira chamada ao mock
310
+ expect(callArgs[0]).toBe('/zeedhi'); // Verifica o primeiro argumento
311
+ expect(callArgs[1]).toMatchObject({
312
+ params: {
313
+ in: [],
314
+ order: [],
315
+ search: undefined,
316
+ search_in: [],
317
+ page: 1,
318
+ limit: 10,
319
+ search_in_params: '{}',
320
+ },
321
+ });
322
+ });
323
+ });
324
+
325
+ describe('clone', () => {
326
+ it('should return an object with all datasource props', async () => {
327
+ const data = [{ value: 'item 1' }];
328
+ const dynamicFilter = {
329
+ name: { operation: 'EQUALS', relation: 'AND', value: 'item 1' },
330
+ };
331
+ const searchJoin = { col1: ['1', '2'], col2: [2], col3: [] };
332
+ const instance = new TekRestDatasource({
333
+ route: '/zeedhi',
334
+ data,
335
+ dynamicFilter,
336
+ searchJoin,
337
+ });
338
+
339
+ const props = instance.clone();
340
+
341
+ expect(props).toEqual({
342
+ data,
343
+ dynamicFilter,
344
+ searchJoin,
345
+ currentRow: {},
346
+ filter: {},
347
+ find: {},
348
+ limit: 10,
349
+ order: [],
350
+ page: 1,
351
+ search: '',
352
+ searchIn: [],
353
+ uniqueKey: 'id',
354
+ watchUrl: false,
355
+ events: {},
356
+ loadAll: false,
357
+ lazyLoad: true,
358
+ arrayFormat: 'repeat',
359
+ route: '/zeedhi',
360
+ type: 'tek-rest',
361
+ });
362
+ });
363
+ });
364
+ });