-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING: O3-2620 Connect Lab Orders to Orderable ConvSet (#1676)
* O3-2620 Connect Lab Orders to Orderable ConvSet * Clean up * Fixup --------- Co-authored-by: Dennis Kigen <[email protected]>
- Loading branch information
1 parent
eb32c3e
commit 059c36f
Showing
6 changed files
with
169 additions
and
19 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
97 changes: 97 additions & 0 deletions
97
packages/esm-patient-labs-app/src/lab-orders/add-lab-order/useTestTypes.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,97 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import useSWRImmutable from 'swr/immutable'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { getDefaultsFromConfigSchema, openmrsFetch, useConfig } from '@openmrs/esm-framework'; | ||
import { useTestTypes } from './useTestTypes'; | ||
import { configSchema } from '../../config-schema'; | ||
|
||
jest.mock('swr/immutable'); | ||
|
||
const mockOpenrsFetch = openmrsFetch as jest.Mock; | ||
const mockUseConfig = useConfig as jest.Mock; | ||
const mockUseSWRImmutable = useSWRImmutable as jest.Mock; | ||
|
||
mockUseSWRImmutable.mockImplementation((keyFcn: () => any, fetcher: any) => { | ||
const promise = useRef(fetcher(keyFcn())); | ||
const [data, setData] = useState(null); | ||
|
||
useEffect(() => { | ||
promise.current.then((response) => { | ||
setData(response); | ||
}); | ||
}, []); | ||
|
||
return { data, isLoading: !data }; | ||
}); | ||
|
||
describe('useTestTypes is configurable', () => { | ||
beforeEach(() => { | ||
mockUseConfig.mockReset(); | ||
mockUseConfig.mockReturnValue(getDefaultsFromConfigSchema(configSchema)); | ||
mockOpenrsFetch.mockReset(); | ||
mockOpenrsFetch.mockImplementation((url: string) => { | ||
if (url.includes('concept?class=Test')) { | ||
return Promise.resolve({ data: { results: [{ display: 'Test concept' }] } }); | ||
} else if (/.*concept\/[0-9a-f]+.*/.test(url)) { | ||
return Promise.resolve({ data: { display: 'Orderable set', setMembers: [{ display: 'Configured concept' }] } }); | ||
} else { | ||
throw Error('Unexpected URL: ' + url); | ||
} | ||
}); | ||
mockUseSWRImmutable.mockClear(); | ||
}); | ||
|
||
it('should return all Test concepts when no labOrderableConcepts are provided', async () => { | ||
mockUseConfig.mockReturnValue({ | ||
...getDefaultsFromConfigSchema(configSchema), | ||
orders: { labOrderableConcepts: [] }, | ||
}); | ||
const { result } = renderHook(() => useTestTypes()); | ||
expect(mockOpenrsFetch).toHaveBeenCalledWith('/ws/rest/v1/concept?class=Test'); | ||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
expect(result.current.error).toBeFalsy(); | ||
expect(result.current.testTypes).toEqual([expect.objectContaining({ label: 'Test concept' })]); | ||
}); | ||
|
||
it('should return children of labOrderableConcepts when provided', async () => { | ||
const { result } = renderHook(() => useTestTypes()); | ||
expect(mockOpenrsFetch).toHaveBeenCalledWith(expect.stringContaining('/ws/rest/v1/concept/')); | ||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
expect(result.current.error).toBeFalsy(); | ||
expect(result.current.testTypes).toEqual([expect.objectContaining({ label: 'Configured concept' })]); | ||
}); | ||
}); | ||
|
||
describe('useTestTypes filters by text', () => { | ||
beforeEach(() => { | ||
mockUseConfig.mockReset(); | ||
mockUseConfig.mockReturnValue(getDefaultsFromConfigSchema(configSchema)); | ||
mockOpenrsFetch.mockReset(); | ||
mockOpenrsFetch.mockResolvedValue({ | ||
data: { | ||
display: 'Orderable set', | ||
setMembers: [{ display: 'Sodium Chloride' }, { display: 'Sodium Bicarbonate' }, { display: 'Potassium' }], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should filter test types by search term', async () => { | ||
const { result, rerender } = renderHook((search: string) => useTestTypes(search), { initialProps: '' }); | ||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
expect(result.current.error).toBeFalsy(); | ||
expect(result.current.testTypes).toEqual([ | ||
expect.objectContaining({ label: 'Sodium Chloride' }), | ||
expect.objectContaining({ label: 'Sodium Bicarbonate' }), | ||
expect.objectContaining({ label: 'Potassium' }), | ||
]); | ||
rerender('sodium'); | ||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
expect(result.current.testTypes).toEqual([ | ||
expect.objectContaining({ label: 'Sodium Chloride' }), | ||
expect.objectContaining({ label: 'Sodium Bicarbonate' }), | ||
]); | ||
rerender('pt'); | ||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
expect(result.current.testTypes).toEqual([expect.objectContaining({ label: 'Potassium' })]); | ||
}); | ||
}); |
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