@@ -2,6 +2,8 @@ import { Observable, of } from 'rxjs';
22
33import * as UtilsFunctions from '../../../../utils/util' ;
44import { expectPropertiesValues } from '../../../../util-test/util-expect.spec' ;
5+ import { PoTableColumnSort } from '../../../po-table/interfaces/po-table-column-sort.interface' ;
6+ import { PoTableColumnSortType } from '../../../po-table/enums/po-table-column-sort-type.enum' ;
57
68import { poLookupLiteralsDefault , PoLookupModalBaseComponent } from './po-lookup-modal-base.component' ;
79
@@ -18,7 +20,7 @@ describe('PoLookupModalBaseComponent:', () => {
1820 component = new PoLookupModalComponent ( ) ;
1921
2022 component . filterService = {
21- getFilteredData : ( searchValue , pageSize : number ) => of ( { items : [ ] , hasNext : false } ) ,
23+ getFilteredItems : ( { filter , pageSize} ) => of ( { items : [ ] , hasNext : false } ) ,
2224 getObjectByValue : ( ) => of ( )
2325 } ;
2426
@@ -34,11 +36,11 @@ describe('PoLookupModalBaseComponent:', () => {
3436 } ) ;
3537
3638 it ( 'should init modal with items' , ( ) => {
37- spyOn ( component . filterService , 'getFilteredData ' ) . and . returnValue ( of ( { items : [ ] . concat ( items ) , hasNext : true } ) ) ;
39+ spyOn ( component . filterService , 'getFilteredItems ' ) . and . returnValue ( of ( { items : [ ] . concat ( items ) , hasNext : true } ) ) ;
3840
3941 component . ngOnInit ( ) ;
4042
41- expect ( component . filterService . getFilteredData ) . toHaveBeenCalled ( ) ;
43+ expect ( component . filterService . getFilteredItems ) . toHaveBeenCalled ( ) ;
4244 expect ( component . items . length ) . toBe ( 5 ) ;
4345 expect ( component . hasNext ) . toBeTruthy ( ) ;
4446 } ) ;
@@ -213,34 +215,59 @@ describe('PoLookupModalBaseComponent:', () => {
213215 expect ( fakeSubscription . unsubscribe ) . not . toHaveBeenCalled ( ) ;
214216 } ) ;
215217
216- it ( 'getFilteredData : shoud call `getFilteredData ` and return a Observable. ' , ( ) => {
218+ it ( 'getFilteredItems : shoud call `getFilteredItems ` and return a Observable if `getFilteredItems` is truthy ' , ( ) => {
217219 const page = 1 ;
218220 const pageSize = 1 ;
219221 const filterParams = { code : 1 } ;
220- const searchValue = 'po' ;
222+ const filter = 'po' ;
221223
222224 component . page = page ;
223225 component . pageSize = pageSize ;
224226 component . filterParams = filterParams ;
225227
228+ spyOn ( component . filterService , 'getFilteredItems' ) . and . returnValue ( of ( < any > { items} ) ) ;
229+ spyOn ( component , < any > 'getFilteredParams' ) . and . callThrough ( ) ;
230+
231+ const filteredDataObservable = component [ 'getFilteredItems' ] ( filter ) ;
232+
233+ expect ( component [ 'getFilteredParams' ] ) . toHaveBeenCalled ( ) ;
234+ expect ( filteredDataObservable instanceof Observable ) ;
235+ expect ( component . filterService . getFilteredItems ) . toHaveBeenCalledWith ( { filter, page, pageSize, filterParams} ) ;
236+ } ) ;
237+
238+ it ( 'getFilteredItems: shoud call `getFilteredData` and return a Observable if `getFilteredItems` is falsy' , ( ) => {
239+ const page = 1 ;
240+ const pageSize = 1 ;
241+ const filterParams = { code : 1 } ;
242+ const filter = 'po' ;
243+
244+ component . page = page ;
245+ component . pageSize = pageSize ;
246+ component . filterParams = filterParams ;
247+
248+ component . filterService . getFilteredItems = undefined ;
249+ component . filterService . getFilteredData = ( ) => of ( { items : [ ] , hasNext : false } ) ;
250+
226251 spyOn ( component . filterService , 'getFilteredData' ) . and . returnValue ( of ( < any > { items} ) ) ;
252+ spyOn ( component , < any > 'getFilteredParams' ) ;
227253
228- const filteredDataObservable = component [ 'getFilteredData ' ] ( searchValue ) ;
254+ const filteredDataObservable = component [ 'getFilteredItems ' ] ( filter ) ;
229255
256+ expect ( component [ 'getFilteredParams' ] ) . not . toHaveBeenCalled ( ) ;
230257 expect ( filteredDataObservable instanceof Observable ) ;
231- expect ( component . filterService . getFilteredData ) . toHaveBeenCalledWith ( searchValue , page , pageSize , filterParams ) ;
258+ expect ( component . filterService . getFilteredData ) . toHaveBeenCalledWith ( filter , page , pageSize , filterParams ) ;
232259 } ) ;
233260
234- it ( 'search: should call `getFilteredData ` if `searchValue` it`s truthy.' , ( ) => {
261+ it ( 'search: should call `getFilteredItems ` if `searchValue` it`s truthy.' , ( ) => {
235262 component . searchValue = 'Suco' ;
236263
237264 const filteredItems = items . filter ( f => f . label . includes ( component . searchValue ) ) ;
238265
239- spyOn ( component , < any > 'getFilteredData ' ) . and . returnValue ( of ( { items : filteredItems , hasNext : true } ) ) ;
266+ spyOn ( component , < any > 'getFilteredItems ' ) . and . returnValue ( of ( { items : filteredItems , hasNext : true } ) ) ;
240267
241268 component . search ( ) ;
242269
243- expect ( component [ 'getFilteredData ' ] ) . toHaveBeenCalledWith ( 'Suco' ) ;
270+ expect ( component [ 'getFilteredItems ' ] ) . toHaveBeenCalledWith ( 'Suco' ) ;
244271 expect ( component . items . length ) . toBe ( 2 ) ;
245272 expect ( component . hasNext ) . toBeTruthy ( ) ;
246273 } ) ;
@@ -255,18 +282,18 @@ describe('PoLookupModalBaseComponent:', () => {
255282 expect ( component [ 'initializeData' ] ) . toHaveBeenCalled ( ) ;
256283 } ) ;
257284
258- it ( 'showMoreEvent: should call `getFilteredData `, increment `page` and assign returned items to `items`.' , ( ) => {
285+ it ( 'showMoreEvent: should call `getFilteredItems `, increment `page` and assign returned items to `items`.' , ( ) => {
259286 const searchValue = 'Chocolate' ;
260287 const returnedItems = [ { value : 6 , label : 'Chocolate quente' } ] ;
261288 component . page = 1 ;
262289 component . items = [ ] . concat ( items ) ;
263290 component . searchValue = searchValue ;
264291
265- spyOn ( component , < any > 'getFilteredData ' ) . and . returnValue ( of ( { items : returnedItems } ) ) ;
292+ spyOn ( component , < any > 'getFilteredItems ' ) . and . returnValue ( of ( { items : returnedItems } ) ) ;
266293
267294 component . showMoreEvent ( ) ;
268295
269- expect ( component [ 'getFilteredData ' ] ) . toHaveBeenCalledWith ( searchValue ) ;
296+ expect ( component [ 'getFilteredItems ' ] ) . toHaveBeenCalledWith ( searchValue ) ;
270297 expect ( component . items . length ) . toBe ( [ ...items , ...returnedItems ] . length ) ;
271298 expect ( component . page ) . toBe ( 2 ) ;
272299 expect ( component . isLoading ) . toBeFalsy ( ) ;
@@ -293,6 +320,48 @@ describe('PoLookupModalBaseComponent:', () => {
293320
294321 } ) ;
295322
323+ it ( 'getFilteredParams: should return object with only truthy values' , ( ) => {
324+ const page = 1 ;
325+ const pageSize = 10 ;
326+ const filter = '' ;
327+ const expectedValue = { page, pageSize } ;
328+
329+ component . page = page ;
330+ component . pageSize = pageSize ;
331+ component . filterParams = undefined ;
332+ component [ 'sort' ] = undefined ;
333+
334+ const filteredParams = component [ 'getFilteredParams' ] ( filter ) ;
335+
336+ expect ( filteredParams ) . toEqual ( expectedValue ) ;
337+ } ) ;
338+
339+ it ( 'getOrderParam: should return `column.property` of PoTableColumnSort object if PoTableColumnSortType is Ascending' , ( ) => {
340+ const sort : PoTableColumnSort = { column : { property : 'name' } , type : PoTableColumnSortType . Ascending } ;
341+ const expectedValue = 'name' ;
342+
343+ const orderParam = component [ 'getOrderParam' ] ( sort ) ;
344+
345+ expect ( orderParam ) . toBe ( expectedValue ) ;
346+ } ) ;
347+
348+ it ( 'getOrderParam: should return `-column.property` of PoTableColumnSort object if PoTableColumnSortType is Descending' , ( ) => {
349+ const sort : PoTableColumnSort = { column : { property : 'name' } , type : PoTableColumnSortType . Descending } ;
350+ const expectedValue = '-name' ;
351+
352+ const orderParam = component [ 'getOrderParam' ] ( sort ) ;
353+
354+ expect ( orderParam ) . toBe ( expectedValue ) ;
355+ } ) ;
356+
357+ it ( 'getOrderParam: should return undefined if PoTableColumnSort.column is undefined' , ( ) => {
358+ const expectedValue = undefined ;
359+
360+ const orderParam = component [ 'getOrderParam' ] ( ) ;
361+
362+ expect ( orderParam ) . toBe ( expectedValue ) ;
363+ } ) ;
364+
296365 } ) ;
297366
298367} ) ;
0 commit comments