-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Add configurability to Recently Searched Patients feature
- Loading branch information
1 parent
ef6236b
commit 3c456ab
Showing
6 changed files
with
194 additions
and
163 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
70 changes: 46 additions & 24 deletions
70
packages/esm-patient-search-app/src/compact-patient-search/compact-patient-search.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 |
---|---|---|
@@ -1,71 +1,93 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { useConfig } from '@openmrs/esm-framework'; | ||
import CompactPatientSearchComponent from './compact-patient-search.component'; | ||
|
||
const mockedUseConfig = jest.mocked(useConfig); | ||
|
||
jest.mock('@openmrs/esm-framework', () => ({ | ||
...jest.requireActual('@openmrs/esm-framework'), | ||
useSession: jest.fn(() => ({ | ||
...jest.requireActual('@openmrs/esm-framework').useSession(), | ||
useSession: jest.fn().mockReturnValue({ | ||
sessionLocation: { | ||
uuid: 'location-uuid', | ||
}, | ||
})), | ||
useConfig: jest.fn(() => ({ | ||
...jest.requireActual('@openmrs/esm-framework').useConfig(), | ||
}), | ||
useConfig: jest.fn().mockReturnValue({ | ||
search: { | ||
patientResultUrl: '/patient/{{patientUuid}}/chart', | ||
showRecentlySearchedPatients: false, | ||
}, | ||
})), | ||
}), | ||
})); | ||
|
||
describe('CompactPatientSearchComponent', () => { | ||
it('renders without crashing', () => { | ||
render(<CompactPatientSearchComponent isSearchPage={true} initialSearchTerm="" />); | ||
expect(screen.getByRole('searchbox')).toBeInTheDocument(); | ||
beforeEach(() => mockedUseConfig.mockClear()); | ||
|
||
it('renders a compact search bar', () => { | ||
render(<CompactPatientSearchComponent isSearchPage initialSearchTerm="" />); | ||
|
||
expect(screen.getByPlaceholderText(/Search for a patient by name or identifier number/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates search term on input change', async () => { | ||
it('typing into the searchbox updates the search term and triggers a search', async () => { | ||
const user = userEvent.setup(); | ||
render(<CompactPatientSearchComponent isSearchPage={true} initialSearchTerm="" />); | ||
const searchInput: HTMLInputElement = screen.getByRole('searchbox'); | ||
|
||
await user.type(searchInput, 'John'); | ||
render(<CompactPatientSearchComponent isSearchPage initialSearchTerm="" />); | ||
|
||
const searchbox = screen.getByPlaceholderText(/Search for a patient by name or identifier number/i); | ||
|
||
expect(searchInput.value).toBe('John'); | ||
await user.type(searchbox, 'John'); | ||
|
||
expect(screen.getByDisplayValue(/John/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('clears search term on clear button click', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render(<CompactPatientSearchComponent isSearchPage={true} initialSearchTerm="" />); | ||
const searchInput: HTMLInputElement = screen.getByRole('searchbox'); | ||
const clearButton = screen.getByRole('button', { name: 'Clear' }); | ||
|
||
await user.type(searchInput, 'John'); | ||
const searchbox = screen.getByPlaceholderText(/Search for a patient by name or identifier number/i); | ||
|
||
const clearButton = screen.getByRole('button', { name: /Clear/i }); | ||
|
||
await user.type(searchbox, 'John'); | ||
await user.click(clearButton); | ||
|
||
expect(searchInput.value).toBe(''); | ||
expect(screen.queryByDisplayValue(/John/i)).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders search results when search term is not empty', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render(<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />); | ||
const searchInput = screen.getByRole('searchbox'); | ||
|
||
await user.type(searchInput, 'John'); | ||
const searchbox = screen.getByPlaceholderText(/Search for a patient by name or identifier number/i); | ||
|
||
await user.type(searchbox, 'John'); | ||
|
||
const searchResultsContainer = screen.getByTestId('floatingSearchResultsContainer'); | ||
|
||
expect(searchResultsContainer).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a list of recently searched patients when a search term is not provided', async () => { | ||
it('renders a list of recently searched patients when a search term is not provided and the showRecentlySearchedPatients config property is set', async () => { | ||
const user = userEvent.setup(); | ||
|
||
mockedUseConfig.mockReturnValue({ | ||
search: { | ||
showRecentlySearchedPatients: true, | ||
}, | ||
}); | ||
|
||
render(<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />); | ||
const searchInput = screen.getByRole('searchbox'); | ||
|
||
await user.clear(searchInput); | ||
const searchbox = screen.getByRole('searchbox'); | ||
|
||
await user.clear(searchbox); | ||
|
||
const searchResultsContainer = screen.getByTestId('floatingSearchResultsContainer'); | ||
|
||
expect(searchResultsContainer).toBeInTheDocument(); | ||
}); | ||
}); |
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
Oops, something went wrong.