From 5bb4bdfe18600ee9af0858b476d5a98494c1bbee Mon Sep 17 00:00:00 2001 From: xKeCo Date: Wed, 27 Nov 2024 12:12:39 -0500 Subject: [PATCH] :recycle: refactor(filter-indicator) P2-903: enhance filtering logic and improve test coverage for edge cases --- .../filter-indicator-by-search.pipe.spec.ts | 134 +++++++++++++----- .../pipes/filter-indicator-by-search.pipe.ts | 1 - 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.spec.ts b/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.spec.ts index 27b92928f..18b1b8180 100644 --- a/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.spec.ts +++ b/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.spec.ts @@ -7,62 +7,122 @@ describe('FilterIndicatorBySearchPipe', () => { pipe = new FilterIndicatorBySearchPipe(); }); - it('should create an instance', () => { + it('should create', () => { expect(pipe).toBeTruthy(); }); - it('should return an empty array if input is not an array', () => { - const result = pipe.transform(null, 'search'); - expect(result).toEqual([]); + it('should return empty array if list is not an array', () => { + expect(pipe.transform(null as any, '', false)).toEqual([]); + expect(pipe.transform(undefined as any, '', false)).toEqual([]); }); - it('should return the original list if searchFilter is empty', () => { - const list = [{ toc_result_description: 'Test', toc_results: [] }]; - const result = pipe.transform(list, ''); - expect(result).toBeDefined(); - expect(result).toEqual(list); - }); - - it('should filter the list based on searchFilter', () => { - const list = [ - { toc_result_description: 'Test1', toc_results: [{ toc_result_description: 'Desc1', indicators: [] }] }, - { toc_result_description: 'Test2', toc_results: [{ toc_result_description: 'Desc2', indicators: [] }] } - ]; - const result = pipe.transform(list, 'Desc1'); - expect(result.length).toBe(1); - expect(result[0].toc_result_description).toBe('Test1'); - }); - - it('should handle isWPsTable flag correctly', () => { - const list = [ + describe('Default filtering (non-WPsTable)', () => { + const mockList = [ { - toc_result_description: 'Test1', - toc_results: [ + toc_result_description: 'Result 1', + indicators: [ + { + indicator_name: 'Indicator 1', + indicator_description: 'Description 1', + is_indicator_custom: true + } + ] + }, + { + toc_result_description: 'Result 2', + indicators: [ { - toc_result_description: 'Desc1', - indicators: [{ indicator_description: 'Indicator1', isVisible: false }] + indicator_name: 'Indicator 2', + indicator_description: 'Description 2', + is_indicator_custom: false } ] } ]; - const result = pipe.transform(list, 'Indicator1', true); - expect(result.length).toBe(1); - expect(result[0].toc_results[0].indicators[0].isVisible).toBe(true); + + it('should return full list when no search filter', () => { + const result = pipe.transform(mockList, '', false); + expect(result.length).toBe(2); + }); + + it('should filter by result description', () => { + const result = pipe.transform(mockList, 'Result 1', false); + expect(result.length).toBe(1); + expect(result[0].toc_result_description).toBe('Result 1'); + }); + + it('should be case insensitive', () => { + const result = pipe.transform(mockList, 'result 1', false); + expect(result.length).toBe(1); + expect(result[0].toc_result_description).toBe('Result 1'); + }); }); - it('should reset indicators if searchFilter is empty and isWPsTable is true', () => { - const list = [ + describe('WPsTable filtering', () => { + const mockWPsList = [ + { + toc_results: [ + { + toc_result_description: 'WP Result 1', + indicators: [{ indicator_description: 'WP Indicator 1' }, { indicator_description: 'WP Indicator 2' }] + } + ] + }, { - toc_result_description: 'Test1', toc_results: [ { - toc_result_description: 'Desc1', - indicators: [{ indicator_description: 'Indicator1', isVisible: true }] + toc_result_description: 'WP Result 2', + indicators: [{ indicator_description: 'WP Indicator 3' }] } ] } ]; - pipe.transform(list, '', true); - expect(list[0].toc_results[0].indicators[0].isVisible).toBe(true); + + it('should return full list when no search filter', () => { + const result = pipe.transform(mockWPsList, '', true); + expect(result.length).toBe(2); + }); + + it('should filter by result description in WPsTable mode', () => { + const result = pipe.transform(mockWPsList, 'WP Result 1', true); + expect(result.length).toBe(1); + expect(result[0].toc_results[0].toc_result_description).toBe('WP Result 1'); + }); + + it('should filter by indicator description in WPsTable mode', () => { + const result = pipe.transform(mockWPsList, 'WP Indicator 1', true); + expect(result.length).toBe(1); + expect(result[0].toc_results[0].indicators[0].indicator_description).toBe('WP Indicator 1'); + }); + + it('should preserve original indicators when resetting', () => { + pipe.transform(mockWPsList, 'WP Indicator 1', true); + const result = pipe.transform(mockWPsList, '', true); + + expect(result[0].toc_results[0].indicators.length).toBe(2); + expect(result[1].toc_results[0].indicators.length).toBe(1); + }); + + it('should mark indicators as visible based on search', () => { + const result = pipe.transform(mockWPsList, 'WP Indicator 1', true); + expect(result[0].toc_results[0].indicators[0].isVisible).toBe(true); + }); + }); + + describe('Edge cases', () => { + it('should handle empty lists', () => { + expect(pipe.transform([], '', false)).toEqual([]); + expect(pipe.transform([], 'search', true)).toEqual([]); + }); + + it('should handle missing properties gracefully', () => { + const incompleteList = [{ toc_results: [] }, { toc_results: null }, {}]; + expect(() => pipe.transform(incompleteList, 'search', false)).not.toThrow(TypeError); + }); + + it('should handle undefined search filter', () => { + const mockList = [{ toc_result_description: 'Test' }]; + expect(() => pipe.transform(mockList, undefined as any, false)).not.toThrow(); + }); }); }); diff --git a/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.ts b/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.ts index 6c24dd7e3..fac5f7ab9 100644 --- a/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.ts +++ b/onecgiar-pr-client/src/app/pages/outcome-indicator/pipes/filter-indicator-by-search.pipe.ts @@ -38,7 +38,6 @@ export class FilterIndicatorBySearchPipe implements PipeTransform { } private isIndicatorVisible(indicator: any, searchUpper: string, result: any): boolean { - // return indicator.indicator_description.toUpperCase().includes(searchUpper); return ( !searchUpper || indicator.indicator_description.toUpperCase().includes(searchUpper) ||