Skip to content

Commit b3f7ac2

Browse files
added test cases
1 parent 7050f56 commit b3f7ac2

File tree

1 file changed

+106
-45
lines changed

1 file changed

+106
-45
lines changed
Lines changed: 106 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/* eslint-disable */
22
import React from 'react';
3+
import { describe, expect, it, vi } from 'vitest';
34
import { act, render, screen } from '@testing-library/react';
45
import userEvent from '@testing-library/user-event';
5-
import { useConfig, useSession, type LoggedInUser, type Session } from '@openmrs/esm-framework';
6+
import { type LoggedInUser, type Session } from '@openmrs/esm-api';
7+
import { useConfig, useSession } from '@openmrs/esm-react-utils';
68
import {
79
inpatientWardResponse,
810
locationResponseForNonExistingSearch,
@@ -14,9 +16,10 @@ import { LocationSelector } from './location-selector.component';
1416

1517
const validLocationUuid = '1ce1b7d4-c865-4178-82b0-5932e51503d6';
1618
const inpatientWardLocationUuid = 'ba685651-ed3b-4e63-9b35-78893060758a';
19+
const inpatientWardLocation = 'Inpatient Ward';
1720

18-
const mockUseConfig = jest.mocked(useConfig);
19-
const mockUseSession = jest.mocked(useSession);
21+
const mockUseConfig = vi.mocked(useConfig);
22+
const mockUseSession = vi.mocked(useSession);
2023

2124
mockUseConfig.mockReturnValue(mockConfig);
2225
mockUseSession.mockReturnValue({
@@ -27,13 +30,9 @@ mockUseSession.mockReturnValue({
2730
} as LoggedInUser,
2831
} as Session);
2932

30-
jest.mock('@openmrs/esm-framework', () => ({
31-
...jest.requireActual('@openmrs/esm-framework'),
32-
setSessionLocation: jest.fn().mockResolvedValue({}),
33-
setUserProperties: jest.fn().mockResolvedValue({}),
34-
navigate: jest.fn(),
35-
showToast: jest.fn(),
36-
openmrsFetch: jest.fn((url) => {
33+
vi.mock('@openmrs/esm-api', async () => ({
34+
...(await import('@openmrs/esm-api')),
35+
openmrsFetch: vi.fn((url) => {
3736
if (url === `/ws/fhir2/R4/Location?_id=${inpatientWardLocationUuid}`) {
3837
return inpatientWardResponse;
3938
}
@@ -45,41 +44,103 @@ jest.mock('@openmrs/esm-framework', () => ({
4544
}
4645
return mockLoginLocations;
4746
}),
47+
setSessionLocation: vi.fn().mockResolvedValue({}),
48+
setUserProperties: vi.fn().mockResolvedValue({}),
4849
}));
4950

50-
describe('LocationSelector', () => {
51-
// it('renders a combo box with locations', async () => {
52-
// await act(async () => {
53-
// render(
54-
// <LocationSelector
55-
// selectedLocationUuid={inpatientWardLocationUuid}
56-
// onChange={jest.fn()}
57-
// Locationlabel="Select Location"
58-
// comBoxLabel="Location"
59-
// />,
60-
// );
61-
// });
62-
// expect(
63-
// screen.getByRole('combobox', {
64-
// name: /location/i,
65-
// }),
66-
// ).toBeInTheDocument();
67-
// expect(screen.getByText('Inpatient Ward')).toBeInTheDocument();
68-
// });
69-
// it('calls onChange when a location is selected', async () => {
70-
// const onChangeMock = jest.fn();
71-
// await act(async () => {
72-
// render(
73-
// <LocationSelector
74-
// selectedLocationUuid={inpatientWardLocationUuid}
75-
// onChange={onChangeMock}
76-
// Locationlabel="Select Location"
77-
// comBoxLabel="Location"
78-
// />
79-
// );
80-
// });
81-
// const comboBox = screen.getByRole('combobox', { name: /location/i });
82-
// await userEvent.selectOptions(comboBox, 'Inpatient Ward');
83-
// expect(onChangeMock).toHaveBeenCalledWith(inpatientWardLocationUuid);
84-
// })
51+
describe('LocationPicker', () => {
52+
it('renders correctly', async () => {
53+
await act(async () => {
54+
render(
55+
<LocationSelector
56+
comBoxLabel="search for a location"
57+
Locationlabel="Login Location"
58+
selectedLocationUuid={inpatientWardLocationUuid}
59+
onChange={vi.fn()}
60+
/>,
61+
);
62+
});
63+
expect(screen.getByRole('combobox')).toBeInTheDocument();
64+
});
65+
66+
it('renders a list of login locations', async () => {
67+
await act(async () => {
68+
render(
69+
<LocationSelector
70+
comBoxLabel="search for a location"
71+
Locationlabel="Login Location"
72+
selectedLocationUuid={inpatientWardLocationUuid}
73+
onChange={vi.fn()}
74+
/>,
75+
);
76+
});
77+
78+
expect(
79+
screen.getByRole('combobox', {
80+
name: /search for a location/i,
81+
}),
82+
).toBeInTheDocument();
83+
84+
const combo = screen.getByRole('combobox', {
85+
name: /search for a location/i,
86+
});
87+
await userEvent.click(combo);
88+
89+
const locations = screen.getAllByRole('option');
90+
expect(locations.length).toBe(4);
91+
const expectedLocations = [/community outreach/, /inpatient ward/, /mobile clinic/, /outpatient clinic/];
92+
expectedLocations.forEach((row) =>
93+
expect(screen.getByRole('option', { name: new RegExp(row, 'i') })).toBeInTheDocument(),
94+
);
95+
});
96+
97+
it('selects the provided selectedLocation when the component is rendered', async () => {
98+
await act(async () => {
99+
render(
100+
<LocationSelector
101+
comBoxLabel="search for a location"
102+
Locationlabel="Login Location"
103+
selectedLocationUuid={inpatientWardLocationUuid}
104+
onChange={vi.fn()}
105+
/>,
106+
);
107+
});
108+
109+
const combo = screen.getByRole('combobox', {
110+
name: /search for a location/i,
111+
});
112+
113+
expect(combo).toHaveValue('Inpatient Ward');
114+
});
115+
116+
it('loads the default location on top of the list', async () => {
117+
await act(async () => {
118+
render(
119+
<LocationSelector
120+
comBoxLabel="search for a location"
121+
Locationlabel="Login Location"
122+
selectedLocationUuid={inpatientWardLocationUuid}
123+
defaultLocationUuid={inpatientWardLocationUuid}
124+
onChange={vi.fn()}
125+
/>,
126+
);
127+
});
128+
129+
expect(
130+
screen.getByRole('combobox', {
131+
name: /search for a location/i,
132+
}),
133+
).toBeInTheDocument();
134+
135+
const combo = screen.getByRole('combobox', {
136+
name: /search for a location/i,
137+
});
138+
await userEvent.click(combo);
139+
140+
const locations = screen.getAllByRole('option');
141+
142+
expect(locations.length).toBe(4);
143+
locations[0].setAttribute('name', inpatientWardLocation);
144+
expect(locations[0].getAttribute('name')).toBe(inpatientWardLocation);
145+
});
85146
});

0 commit comments

Comments
 (0)