Skip to content

Commit

Permalink
Added test to check for default location not coming when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Dec 14, 2023
1 parent e548826 commit 17fb6b8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
20 changes: 20 additions & 0 deletions packages/apps/esm-login-app/__mocks__/locations.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,26 @@ export const validatingLocationSuccessResponse = {
},
};

export const validatingLocationEmptyResponse = {
ok: true,
data: {
resourceType: 'Bundle',
id: 'e3c2aa40-21b0-4671-a492-bb7e7f16cc46',
meta: {
lastUpdated: '2023-11-08T08:45:48.967+00:00',
},
type: 'searchset',
total: 0,
link: [
{
relation: 'self',
url: 'https://dev3.openmrs.org/openmrs/ws/fhir2/R4/Location?_id=1ce1b7d4-c865-4178-82b0-5932e51503d6',
},
],
entry: [],
},
};

export const validatingLocationFailureResponse = {
ok: false,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function useDefaultLocation(isUpdateFlow: boolean, searchTerm: string) {

const userDefaultLocationUuid = useMemo(() => userProperties?.defaultLocation, [userProperties?.defaultLocation]);

const { isLocationValid, location: defaultFhirLocation } = useValidateLocationUuid(
const { isLocationValid, location: defaultLocationFhir } = useValidateLocationUuid(
userDefaultLocationUuid,
searchTerm,
);
Expand Down Expand Up @@ -218,7 +218,7 @@ export function useDefaultLocation(isUpdateFlow: boolean, searchTerm: string) {
);

return {
defaultFhirLocation,
defaultLocationFhir,
userDefaultLocationUuid: isLocationValid ? userDefaultLocationUuid : null,
updateDefaultLocation,
savePreference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
} from '@openmrs/esm-framework';
import {
mockLoginLocations,
validatingLocationEmptyResponse,
validatingLocationFailureResponse,
validatingLocationSuccessResponse,
} from '../../__mocks__/locations.mock';
import { mockConfig } from '../../__mocks__/config.mock';
import renderWithRouter from '../test-helpers/render-with-router';
import LocationPicker from './location-picker.component';
import { act } from 'react-dom/test-utils';
import userEvent from '@testing-library/user-event';

const validLocationUuid = '1ce1b7d4-c865-4178-82b0-5932e51503d6';
const invalidLocationUuid = '2gf1b7d4-c865-4178-82b0-5932e51503d6';
Expand All @@ -40,6 +42,13 @@ mockedOpenmrsFetch.mockImplementation((url) => {
return validatingLocationFailureResponse;
}

if (url === `/ws/fhir2/R4/Location?_id=${validLocationUuid}&name%3Acontains=site`) {
return validatingLocationEmptyResponse;
}

if (url === '/ws/fhir2/R4/Location?_summary=data&_count=50&_tag=Login+Location&name%3Acontains=site') {
return validatingLocationEmptyResponse;
}
return mockLoginLocations;
});

Expand All @@ -60,10 +69,7 @@ describe('LocationPicker', () => {
describe('Testing basic workflows', () => {
it('renders a list of login locations', async () => {
await act(() => {
renderWithRouter(LocationPicker, {
currentLocationUuid: 'some-location-uuid',
hideWelcomeMessage: false,
});
renderWithRouter(LocationPicker, {});
});

expect(screen.getByText(/welcome testy mctesterface/i)).toBeInTheDocument();
Expand Down Expand Up @@ -347,4 +353,19 @@ describe('LocationPicker', () => {
expect(radios[0].getAttribute('id')).toBe('Community Outreach');
expect(radios[0]).toHaveAttribute('checked');
});

// it("should not show default location if the searched term doesn't matches the default location", async () => {
// const user = userEvent.setup();
// await act(() => {
// renderWithRouter(LocationPicker, {});
// });

// const searchBox = screen.getByRole('searchbox', {
// name: /search for a location/i,
// });
// await user.type(searchBox, 'site');
// expect(searchBox.getAttribute('value')).toBe('site');

// expect(screen.queryByRole('radio', { name: new RegExp(/community outreach/, 'i') })).not.toBeInTheDocument();
// });
});

0 comments on commit 17fb6b8

Please sign in to comment.