Skip to content

Commit

Permalink
(feat) Test types should be filterable by synonyms (#2006)
Browse files Browse the repository at this point in the history
* (feat) Test types should be filterable by synonyms

* Review feedback

Co-authored-by: Ian <[email protected]>

* Fix tests

---------

Co-authored-by: Ian <[email protected]>
  • Loading branch information
denniskigen and ibacher authored Sep 10, 2024
1 parent 8438c66 commit 2f7ab2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ const mockTestTypes = [
{
conceptUuid: 'test-lab-uuid-1',
label: 'HIV VIRAL LOAD',
synonyms: ['HIV VIRAL LOAD', 'HIV VL'],
},
{
conceptUuid: 'test-lab-uuid-2',
label: 'CD4 COUNT',
synonyms: ['CD4 COUNT', 'CD4'],
},
{
conceptUuid: 'test-lab-uuid-3',
label: 'HEMOGLOBIN',
synonyms: ['HEMOGLOBIN', 'HGB'],
},
];
const mockUseTestTypes = jest.fn().mockReturnValue({
Expand Down Expand Up @@ -200,6 +207,15 @@ describe('AddLabOrder', () => {
expect(screen.getByText('04 — Apr — 1972')).toBeInTheDocument();
});

test('should be possible to search for test types by synonyms', async () => {
const user = userEvent.setup();
renderAddLabOrderWorkspace();
const searchInput = screen.getByRole('searchbox');
await user.type(searchInput, 'hgb');
await screen.findByText(/hemoglobin/i);
expect(screen.queryByText(/hiv viral load/i)).not.toBeInTheDocument();
});

test('should display an error message if test types fail to load', () => {
mockUseTestTypes.mockReturnValue({
testTypes: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ function TestTypeSearchResults({ searchTerm, openOrderForm, focusAndClearSearchI
}

if (searchTerm && searchTerm.trim() !== '') {
return testTypes.filter((testType) => testType.label.toLowerCase().includes(searchTerm.toLowerCase()));
return testTypes?.filter((testType) =>
testType.synonyms.some((name) => name.toLowerCase().includes(searchTerm.toLowerCase())),
);
}
}, [searchTerm, testTypes]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ mockOpenrsFetch.mockImplementation((url: string) => {
});

describe('useTestTypes is configurable', () => {
it('should return all Test concepts when no labOrderableConcepts are provided', async () => {
it('should return all test concepts when no labOrderableConcepts are provided', async () => {
const { result } = renderHook(() => useTestTypes());
expect(mockOpenrsFetch).toHaveBeenCalledWith(
'/ws/rest/v1/concept?class=Test?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))',
'/ws/rest/v1/concept?class=Test?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))',
);
await waitFor(() => expect(result.current.isLoading).toBeFalsy());
expect(result.current.error).toBeFalsy();
Expand All @@ -54,7 +54,7 @@ describe('useTestTypes is configurable', () => {
const { result } = renderHook(() => useTestTypes());
expect(mockOpenrsFetch).toHaveBeenCalledWith(
expect.stringContaining(
'/ws/rest/v1/concept?class=Test?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))',
'/ws/rest/v1/concept?class=Test?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))',
),
);
await waitFor(() => expect(result.current.isLoading).toBeFalsy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ConceptResults = FetchResponse<{ results: Array<Concept> }>;
export interface TestType {
label: string;
conceptUuid: string;
synonyms: string[];
}

export interface UseTestType {
Expand All @@ -31,9 +32,9 @@ function useTestConceptsSWR(labOrderableConcepts?: Array<string>) {
labOrderableConcepts
? labOrderableConcepts.map(
(c) =>
`${restBaseUrl}/concept/${c}?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))`,
`${restBaseUrl}/concept/${c}?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))`,
)
: `${restBaseUrl}/concept?class=Test?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))`,
: `${restBaseUrl}/concept?class=Test?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))`,
(labOrderableConcepts ? openmrsFetchMultiple : openmrsFetch) as any,
{
shouldRetryOnError(err) {
Expand All @@ -58,7 +59,6 @@ function useTestConceptsSWR(labOrderableConcepts?: Array<string>) {

export function useTestTypes(): UseTestType {
const { labOrderableConcepts } = useConfig<ConfigObject>().orders;

const { data, isLoading, error } = useTestConceptsSWR(labOrderableConcepts.length ? labOrderableConcepts : null);

useEffect(() => {
Expand All @@ -73,6 +73,7 @@ export function useTestTypes(): UseTestType {
?.map((concept) => ({
label: concept.display,
conceptUuid: concept.uuid,
synonyms: concept.names?.flatMap((name) => name.display) ?? [],
}))
?.sort((testConcept1, testConcept2) => testConcept1.label.localeCompare(testConcept2.label))
?.filter((item, pos, array) => !pos || array[pos - 1].conceptUuid !== item.conceptUuid),
Expand Down
3 changes: 3 additions & 0 deletions packages/esm-patient-labs-app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export interface Concept {
display: string;
name: string;
};
names: Array<{
display: string;
}>;
answers: [];
setMembers: [];
hiNormal: number;
Expand Down

0 comments on commit 2f7ab2f

Please sign in to comment.