-
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.
code refactoring and additional unit tests
- Loading branch information
1 parent
e1b4dd7
commit a12fb72
Showing
7 changed files
with
203 additions
and
59 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
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
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, | ||
}, | ||
}); | ||
}); | ||
}); |
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
src/test/__tests__/components/ComplexLookupField/ComplexLookupSearchResults.test.tsx
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,40 @@ | ||
import { render } from '@testing-library/react'; | ||
import { useComplexLookupSearchResults } from '@common/hooks/useComplexLookupSearchResults'; | ||
import { ComplexLookupSearchResults } from '@components/ComplexLookupField/ComplexLookupSearchResults'; | ||
import { TableFlex } from '@components/Table'; | ||
|
||
jest.mock('@components/Table'); | ||
jest.mock('@common/hooks/useComplexLookupSearchResults'); | ||
|
||
const listHeader = ['Column 1', 'Column 2']; | ||
const formattedData = [ | ||
{ id: '1', values: ['Data 1', 'Data 2'] }, | ||
{ id: '2', values: ['Data 3', 'Data 4'] }, | ||
]; | ||
|
||
const onTitleClick = jest.fn(); | ||
const searchResultsFormatter = jest.fn(); | ||
const tableConfig = {} as SearchResultsTableConfig; | ||
|
||
describe('ComplexLookupSearchResults', () => { | ||
it('renders "TableFlex" component with the required props', () => { | ||
(useComplexLookupSearchResults as jest.Mock).mockReturnValue({ | ||
listHeader, | ||
formattedData, | ||
}); | ||
(TableFlex as jest.Mock).mockReturnValue(<div>Mock TableFlex</div>); | ||
|
||
render( | ||
<ComplexLookupSearchResults | ||
onTitleClick={onTitleClick} | ||
tableConfig={tableConfig} | ||
searchResultsFormatter={searchResultsFormatter} | ||
/>, | ||
); | ||
|
||
expect(TableFlex as jest.Mock).toHaveBeenCalledWith( | ||
{ header: listHeader, data: formattedData, className: 'results-list' }, | ||
{}, | ||
); | ||
}); | ||
}); |