-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UILD-410: Fix Sonar issues and increase tests coverage (#32)
- Loading branch information
1 parent
d27f2db
commit b771ec2
Showing
15 changed files
with
455 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { useIntl } from 'react-intl'; | ||
import { type Row } from '@components/Table'; | ||
import { useSearchContext } from '@common/hooks/useSearchContext'; | ||
import { ComplexLookupSearchResultsProps } from '@components/ComplexLookupField/ComplexLookupSearchResults'; | ||
import state from '@state'; | ||
|
||
export const useComplexLookupSearchResults = ({ | ||
onTitleClick, | ||
tableConfig, | ||
searchResultsFormatter, | ||
}: ComplexLookupSearchResultsProps) => { | ||
const { onAssignRecord } = useSearchContext(); | ||
const data = useRecoilValue(state.search.data); | ||
const sourceData = useRecoilValue(state.search.sourceData); | ||
const { formatMessage } = useIntl(); | ||
|
||
const applyActionItems = useCallback( | ||
(rows: Row[]): Row[] => | ||
rows?.map(row => { | ||
const formattedRow: Row = { ...row }; | ||
|
||
Object.entries(tableConfig.columns).forEach(([key, column]) => { | ||
formattedRow[key] = { | ||
...row[key], | ||
children: column.formatter | ||
? column.formatter({ row, formatMessage, onAssign: onAssignRecord, onTitleClick }) | ||
: row[key].label, | ||
}; | ||
}); | ||
|
||
return formattedRow; | ||
}), | ||
[onAssignRecord, tableConfig], | ||
); | ||
|
||
const formattedData = useMemo( | ||
() => applyActionItems(searchResultsFormatter(data || [], sourceData || [])), | ||
[applyActionItems, data], | ||
); | ||
|
||
const listHeader = useMemo( | ||
() => | ||
Object.keys(tableConfig.columns).reduce((accum, key) => { | ||
const { label, position, className } = (tableConfig.columns[key] || {}) as SearchResultsTableColumn; | ||
|
||
accum[key] = { | ||
label: label ? formatMessage({ id: label }) : '', | ||
position: position, | ||
className: className, | ||
}; | ||
|
||
return accum; | ||
}, {} as Row), | ||
[tableConfig], | ||
); | ||
|
||
return { formattedData, listHeader }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/__tests__/common/hooks/useComplesLookupSearchResults.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useRecoilValue } from 'recoil'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { useSearchContext } from '@common/hooks/useSearchContext'; | ||
import { useComplexLookupSearchResults } from '@common/hooks/useComplexLookupSearchResults'; | ||
import { ComplexLookupSearchResultsProps } from '@components/ComplexLookupField/ComplexLookupSearchResults'; | ||
import { Row } from '@components/Table'; | ||
|
||
jest.mock('recoil'); | ||
jest.mock('@common/hooks/useSearchContext', () => ({ | ||
useSearchContext: jest.fn(), | ||
})); | ||
|
||
const data = [ | ||
{ | ||
id: '1', | ||
name: { label: 'Item 1' }, | ||
description: { label: 'Description 1' }, | ||
}, | ||
]; | ||
const sourceData = [ | ||
{ | ||
id: '1', | ||
name: 'Item 1', | ||
description: 'Description 1', | ||
}, | ||
]; | ||
const tableConfig = { | ||
columns: { | ||
name: { | ||
label: 'name.label', | ||
position: 1, | ||
formatter: ({ row }: any) => row.name.label, | ||
}, | ||
description: { | ||
label: 'description.label', | ||
position: 2, | ||
}, | ||
}, | ||
}; | ||
const searchResultsFormatter = (data: Row[]) => data; | ||
|
||
describe('useComplesLookupSearchResults', () => { | ||
beforeEach(() => { | ||
(useSearchContext as jest.Mock).mockReturnValue({ | ||
onAssignRecord: jest.fn(), | ||
}); | ||
(useRecoilValue as jest.Mock).mockReturnValueOnce(data).mockReturnValueOnce(sourceData); | ||
}); | ||
|
||
it('returns "formattedData" and "listHeader"', () => { | ||
const props: ComplexLookupSearchResultsProps = { | ||
onTitleClick: jest.fn(), | ||
tableConfig, | ||
searchResultsFormatter, | ||
}; | ||
|
||
const { result } = renderHook(() => useComplexLookupSearchResults(props)); | ||
|
||
expect(result.current.formattedData).toEqual([ | ||
{ | ||
id: '1', | ||
name: { | ||
label: 'Item 1', | ||
children: 'Item 1', | ||
}, | ||
description: { | ||
label: 'Description 1', | ||
children: 'Description 1', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(result.current.listHeader).toEqual({ | ||
name: { | ||
label: 'name.label', | ||
position: 1, | ||
className: undefined, | ||
}, | ||
description: { | ||
label: 'description.label', | ||
position: 2, | ||
className: undefined, | ||
}, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.