Skip to content

Commit 6eb01fa

Browse files
CSimoesJrrafaellmarques
authored andcommitted
feat(dynamic-view): implementa novas propriedades em fields
Permite visualização de lista ou objeto de uma propriedade. Permite utilização de fieldLabel e fieldValue para itens. Permite concatenar fieldLabel e fieldValue para exibição do item. Criação da propriedade searchService. fixes DTHFUI-7549
1 parent d64b4a6 commit 6eb01fa

14 files changed

+623
-80
lines changed

projects/ui/src/lib/components/po-dynamic/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './po-dynamic-field-type.enum';
22
export * from './po-dynamic-field-force-component.enum';
33
export * from './po-dynamic-form/po-dynamic-form-field.interface';
4+
export * from './po-dynamic-view/interfaces/po-dynamic-view-request.interface';
45
export * from './po-dynamic-form/po-dynamic-form-load/po-dynamic-form-load.interface';
56
export * from './po-dynamic-form/po-dynamic-form-validation/po-dynamic-form-field-changed.interface';
67
export * from './po-dynamic-form/po-dynamic-form-validation/po-dynamic-form-field-validation.interface';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Observable } from 'rxjs';
2+
3+
/**
4+
* @usedBy PoDynamicViewComponent
5+
*
6+
* @description
7+
*
8+
* Define o tipo de busca customizada para um campo em específico.
9+
*/
10+
export interface PoDynamicViewRequest {
11+
/**
12+
* Método responsável por enviar um valor que será buscado no serviço.
13+
*
14+
*
15+
* @param {string|Array<any>} value Valor único a ser buscado na fonte de dados.
16+
* @param {any} filterParams Valor opcional para informar filtros customizados.
17+
*/
18+
getObjectByValue(value: string | Array<any>, filterParams?: any): Observable<any>;
19+
}

projects/ui/src/lib/components/po-dynamic/po-dynamic-view/po-dynamic-view-base.component.spec.ts

Lines changed: 190 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import { CurrencyPipe, DatePipe, DecimalPipe, TitleCasePipe } from '@angular/common';
22
import { HttpClient, HttpHandler } from '@angular/common/http';
3-
import { TestBed } from '@angular/core/testing';
3+
import { TestBed, fakeAsync, inject, tick } from '@angular/core/testing';
44

5-
import { expectPropertiesValues, expectArraysSameOrdering } from '../../../util-test/util-expect.spec';
65
import { PoTimePipe } from '../../../pipes/po-time/po-time.pipe';
6+
import { expectArraysSameOrdering, expectPropertiesValues } from '../../../util-test/util-expect.spec';
77

8+
import { Observable, of } from 'rxjs';
89
import * as PoDynamicUtil from '../po-dynamic.util';
10+
import { PoDynamicViewRequest } from './interfaces/po-dynamic-view-request.interface';
911
import { PoDynamicViewBaseComponent } from './po-dynamic-view-base.component';
10-
import { PoDynamicViewService } from './po-dynamic-view.service';
1112
import { PoDynamicViewField } from './po-dynamic-view-field.interface';
13+
import { PoDynamicViewService } from './services/po-dynamic-view.service';
14+
15+
class DynamicViewService implements PoDynamicViewRequest {
16+
getObjectByValue(id: string): Observable<any> {
17+
return of({ value: 123, label: 'teste' });
18+
}
19+
}
20+
21+
class TestService implements PoDynamicViewRequest {
22+
getObjectByValue(id: string): Observable<any> {
23+
return of({ value: 123, label: 'teste' });
24+
}
25+
}
1226

1327
describe('PoDynamicViewBaseComponent:', () => {
1428
let component: PoDynamicViewBaseComponent;
@@ -30,7 +44,8 @@ describe('PoDynamicViewBaseComponent:', () => {
3044
PoTimePipe,
3145
PoDynamicViewService,
3246
HttpClient,
33-
HttpHandler
47+
HttpHandler,
48+
DynamicViewService
3449
]
3550
});
3651

@@ -189,8 +204,107 @@ describe('PoDynamicViewBaseComponent:', () => {
189204

190205
expectArraysSameOrdering(newFields, expectedFields);
191206
});
207+
208+
it('should return ordering fields with property searchService', fakeAsync(
209+
inject([DynamicViewService], (dynamicService: DynamicViewService) => {
210+
component.service = dynamicService;
211+
const fields: Array<PoDynamicViewField> = [
212+
{ property: 'test 1', order: 6 },
213+
{ property: 'test 0', order: 2, searchService: 'url.test.com', fieldLabel: 'name', fieldValue: 'id' },
214+
{ property: 'test 2', order: 3 },
215+
{ property: 'test 3', order: 1 },
216+
{ property: 'test 4', order: 5 },
217+
{ property: 'test 5', order: 4 }
218+
];
219+
component.value[fields[1].property] = '123';
220+
spyOn(component.service, 'getObjectByValue').and.returnValue(of([{ id: 1, name: 'po' }]));
221+
spyOn(component, <any>'searchById').and.returnValue(of([{ id: 1, name: 'po' }]));
222+
223+
const expectedFields = [
224+
{ property: 'test 3' },
225+
{ property: 'test 0', value: 'po' },
226+
{ property: 'test 2' },
227+
{ property: 'test 5' },
228+
{ property: 'test 4' },
229+
{ property: 'test 1' }
230+
];
231+
232+
component.fields = [...fields];
233+
234+
const newFields = component['getConfiguredFields']();
235+
tick(500);
236+
console.log(newFields);
237+
238+
expectArraysSameOrdering(newFields, expectedFields);
239+
})
240+
));
241+
242+
it('should return ordering fields with property searchService using service type', fakeAsync(
243+
inject([DynamicViewService], (dynamicService: DynamicViewService) => {
244+
component.service = dynamicService;
245+
const fields: Array<PoDynamicViewField> = [
246+
{ property: 'test 1' },
247+
{ property: 'test 0', searchService: new TestService(), fieldLabel: 'name', fieldValue: 'id' },
248+
{ property: 'test 2', searchService: 'url.com' },
249+
{ property: 'test 3', searchService: 'url.com' },
250+
{ property: 'test 4' },
251+
{ property: 'test 5' }
252+
];
253+
component.value[fields[1].property] = '123';
254+
component.value[fields[2].property] = [{ test: 123 }];
255+
component.value[fields[3].property] = { test: 123 };
256+
257+
spyOn(component.service, 'getObjectByValue').and.returnValue(of([{ id: 1, name: 'po' }]));
258+
259+
const expectedFields = [
260+
{ property: 'test 1', value: undefined },
261+
{ property: 'test 0', value: 'teste' },
262+
{ property: 'test 2', value: null },
263+
{ property: 'test 3', value: null },
264+
{ property: 'test 4' },
265+
{ property: 'test 5' }
266+
];
267+
268+
component.fields = [...fields];
269+
270+
const newFields = component['getConfiguredFields']();
271+
tick(500);
272+
console.log(newFields);
273+
274+
expectArraysSameOrdering(newFields, expectedFields);
275+
})
276+
));
277+
});
278+
279+
it('searchById: should return null if value is empty', done => {
280+
const value = '';
281+
const field: any = { property: 'test' };
282+
283+
component['searchById'](value, field).subscribe(result => {
284+
expect(result).toBeNull(); // Verifique se o resultado é nulo
285+
done();
286+
});
192287
});
193288

289+
it('createFieldWithService: should call searchById and update newFields correctly', fakeAsync(() => {
290+
const field = { property: 'test' };
291+
const newFields = [];
292+
const index = 0;
293+
294+
const valueToSearch = '123';
295+
const expectedResult = 'transformedValue';
296+
297+
const mockSearchById = spyOn(component, <any>'searchById').and.returnValue(of(expectedResult));
298+
299+
component.value[field.property] = valueToSearch;
300+
component['createFieldWithService'](field, newFields, index);
301+
302+
tick();
303+
304+
expect(mockSearchById).toHaveBeenCalledWith(valueToSearch, field);
305+
expect(newFields[index].value).toBe(expectedResult);
306+
}));
307+
194308
it('getMergedFields: should return a merged array between configuredFields and valueFields', () => {
195309
const configuredFields = [{ property: 'name', value: 'po' }];
196310
const valueFields = [{ property: 'email' }];
@@ -236,6 +350,78 @@ describe('PoDynamicViewBaseComponent:', () => {
236350
expect(component['transformValue']).toHaveBeenCalled();
237351
});
238352

353+
it(`createField: should call 'transformArrayValue', return a
354+
object and value is a label property`, () => {
355+
const field = { property: 'name', label: 'Nome', isArrayOrObject: true };
356+
component.value = { name: { label: 'Test1', value: 123 } };
357+
358+
const newField = component['createField'](field);
359+
360+
expect(newField.value).toBe('Test1');
361+
});
362+
363+
it(`createField: should call 'transformArrayValue', return a
364+
list and value is a title property`, () => {
365+
const field = {
366+
property: 'name',
367+
label: 'Nome',
368+
isArrayOrObject: true,
369+
concatLabelValue: true,
370+
fieldLabel: 'title',
371+
fieldValue: 'id'
372+
};
373+
component.value = {
374+
name: [
375+
{ title: 'Test1', id: 123 },
376+
{ title: 'Test2', id: 321 }
377+
]
378+
};
379+
380+
const newField = component['createField'](field);
381+
382+
expect(newField.value).toBe('Test1 - 123, Test2 - 321');
383+
});
384+
385+
it(`createField: should call 'transformArrayValue' and return a empty value if fieldLabel is a property invalid`, () => {
386+
const field = { property: 'name', label: 'Nome', isArrayOrObject: true, fieldLabel: 'item', fieldValue: 'other' };
387+
const listName = [
388+
{ title: 'Test1', id: 123 },
389+
{ title: 'Test2', id: 321 }
390+
];
391+
component.value = {
392+
name: listName
393+
};
394+
395+
const newField = component['createField'](field);
396+
397+
expect(newField.value).toEqual(listName);
398+
});
399+
400+
it(`createField: should call 'transformFieldLabel' and return a fieldLabel property`, () => {
401+
const field = { property: 'name', label: 'Nome', fieldLabel: 'title', fieldValue: 'id' };
402+
component.value = { name: 'Test Name', title: 'Title Test', id: 123 };
403+
404+
const newField = component['createField'](field);
405+
406+
expect(newField.value).toBe('Title Test');
407+
});
408+
409+
it('createField: should call `transformFieldLabel`, return a `fieldLabel` and `fieldValue` property if `concatLabelValue` is true', () => {
410+
const field = {
411+
property: 'name',
412+
label: 'Nome',
413+
fieldLabel: 'title',
414+
fieldValue: 'id',
415+
concatLabelValue: true,
416+
type: 'currency'
417+
};
418+
component.value = { name: 'Test Name', title: 'Test Title', id: 123 };
419+
420+
const newField = component['createField'](field);
421+
422+
expect(newField.value).toBe('Test Title - 123');
423+
});
424+
239425
it('getValueFields: should return an array converting the value object', () => {
240426
component.value = { name: 'Po' };
241427

0 commit comments

Comments
 (0)