Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reagan-meant committed Oct 2, 2024
1 parent fda4672 commit daeb9d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getDefaultsFromConfigSchema, navigate, useConfig, useSession } from '@o
import { mockSession } from '__mocks__';
import { configSchema, type PatientSearchConfig } from '../config-schema';
import CompactPatientSearchComponent from './compact-patient-search.component';
import { BrowserRouter, Route, Routes } from 'react-router-dom';

const mockUseConfig = jest.mocked(useConfig<PatientSearchConfig>);
const mockUseSession = jest.mocked(useSession);
Expand All @@ -17,13 +18,25 @@ describe('CompactPatientSearchComponent', () => {
});

it('renders a compact search bar', () => {
render(<CompactPatientSearchComponent isSearchPage initialSearchTerm="" />);
render(
<BrowserRouter>
<Routes>
<Route path="/" element={<CompactPatientSearchComponent isSearchPage initialSearchTerm="" />} />
</Routes>
</BrowserRouter>,
);
expect(screen.getByPlaceholderText(/Search for a patient by name or identifier number/i)).toBeInTheDocument();
});

it('renders search results when search term is not empty', async () => {
const user = userEvent.setup();
render(<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />);
render(
<BrowserRouter>
<Routes>
<Route path="/" element={<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />} />
</Routes>
</BrowserRouter>,
);
const searchbox = screen.getByPlaceholderText(/Search for a patient by name or identifier number/i);
await user.type(searchbox, 'John');
const searchResultsContainer = screen.getByTestId('floatingSearchResultsContainer');
Expand All @@ -39,15 +52,34 @@ describe('CompactPatientSearchComponent', () => {
patientResultUrl: configSchema.search.patientResultUrl._default,
},
});
render(<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />);
render(
<BrowserRouter>
<Routes>
<Route path="/" element={<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" />} />
</Routes>
</BrowserRouter>,
);
const searchResultsContainer = screen.getByTestId('floatingSearchResultsContainer');
expect(searchResultsContainer).toBeInTheDocument();
});

it('navigates to the advanced search page with the correct query string when the Search button is clicked', async () => {
const user = userEvent.setup();
render(
<CompactPatientSearchComponent isSearchPage={false} initialSearchTerm="" shouldNavigateToPatientSearchPage />,
<BrowserRouter>
<Routes>
<Route
path="/"
element={
<CompactPatientSearchComponent
isSearchPage={false}
initialSearchTerm=""
shouldNavigateToPatientSearchPage
/>
}
/>
</Routes>
</BrowserRouter>,
);
const searchbox = screen.getByRole('searchbox');
await user.type(searchbox, 'John');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react';
import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework';
import { type PatientSearchConfig, configSchema } from '../config-schema';
import PatientSearchButton from './patient-search-button.component';
import { BrowserRouter, Route, Routes } from 'react-router-dom';

const mockUseConfig = jest.mocked(useConfig<PatientSearchConfig>);

Expand Down Expand Up @@ -37,8 +38,13 @@ describe('PatientSearchButton', () => {
it('displays overlay when button is clicked', async () => {
const user = userEvent.setup();

render(<PatientSearchButton />);

render(
<BrowserRouter>
<Routes>
<Route path="/" element={<PatientSearchButton />} />
</Routes>
</BrowserRouter>,
);
const searchButton = screen.getByLabelText('Search Patient Button');

await user.click(searchButton);
Expand Down

0 comments on commit daeb9d7

Please sign in to comment.