11import { CurrencyPipe , DatePipe , DecimalPipe , TitleCasePipe } from '@angular/common' ;
22import { 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' ;
65import { 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' ;
89import * as PoDynamicUtil from '../po-dynamic.util' ;
10+ import { PoDynamicViewRequest } from './interfaces/po-dynamic-view-request.interface' ;
911import { PoDynamicViewBaseComponent } from './po-dynamic-view-base.component' ;
10- import { PoDynamicViewService } from './po-dynamic-view.service' ;
1112import { 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
1327describe ( '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