Skip to content

Commit 17c8786

Browse files
committed
Add tests for All locations list component
1 parent b11946e commit 17c8786

File tree

6 files changed

+274
-2
lines changed

6 files changed

+274
-2
lines changed

packages/fhir-location-management/src/components/AllLocationListFlat/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ interface RouteParams {
2121
locationId: string | undefined;
2222
}
2323

24-
interface Props {
24+
export interface Props {
2525
fhirBaseURL: string;
26-
LocationPageSize: number;
2726
}
2827

2928
const getSearchParams = (search: string | null) => {
@@ -36,6 +35,11 @@ const getSearchParams = (search: string | null) => {
3635

3736
export type LocationListPropTypes = Props & RouteComponentProps<RouteParams>;
3837

38+
/* Function which shows the list of all locations
39+
*
40+
* @param {Object} props - AllLocationListFlat component props
41+
* @returns {Function} returns paginated locations list display
42+
*/
3943
export const AllLocationListFlat: React.FC<LocationListPropTypes> = (props) => {
4044
const { fhirBaseURL } = props;
4145
const { t } = useTranslation();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table data 1`] = `"Good Health Clinic"`;
4+
5+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table data 2`] = `"Building"`;
6+
7+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table data 3`] = `"active"`;
8+
9+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table data 4`] = `""`;
10+
11+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table data 5`] = `"Edit"`;
12+
13+
exports[`location-management/src/components/AllLocationListFlat Show data as expected: table header 1`] = `"NameTypeStatusParentActions"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { AllLocationListFlat } from '..';
2+
import React from 'react';
3+
import { store } from '@opensrp/store';
4+
import { authenticateUser } from '@onaio/session-reducer';
5+
import { QueryClientProvider, QueryClient } from 'react-query';
6+
import nock from 'nock';
7+
import { render, cleanup, waitForElementToBeRemoved, screen } from '@testing-library/react';
8+
import { Router } from 'react-router';
9+
import { createBrowserHistory } from 'history';
10+
import { flatLocations } from '../../../ducks/tests/fixtures';
11+
import { Provider } from 'react-redux';
12+
import { RoleContext } from '@opensrp/rbac';
13+
import { superUserRole } from '@opensrp/react-utils';
14+
import { locationResourceType } from '../../../constants';
15+
16+
const history = createBrowserHistory();
17+
18+
const props = {
19+
fhirBaseURL: 'http://test.server.org',
20+
};
21+
22+
jest.mock('fhirclient', () => {
23+
return jest.requireActual('fhirclient/lib/entry/browser');
24+
});
25+
26+
jest.setTimeout(10000);
27+
28+
const queryClient = new QueryClient({
29+
defaultOptions: {
30+
queries: {
31+
retry: false,
32+
cacheTime: 0,
33+
},
34+
},
35+
});
36+
37+
const AppWrapper = (props) => {
38+
return (
39+
<Provider store={store}>
40+
<RoleContext.Provider value={superUserRole}>
41+
<Router history={history}>
42+
<QueryClientProvider client={queryClient}>
43+
<AllLocationListFlat {...props} />
44+
</QueryClientProvider>
45+
</Router>
46+
</RoleContext.Provider>
47+
</Provider>
48+
);
49+
};
50+
51+
describe('location-management/src/components/AllLocationListFlat', () => {
52+
beforeAll(() => {
53+
nock.disableNetConnect();
54+
store.dispatch(
55+
authenticateUser(
56+
true,
57+
{
58+
59+
name: 'Bobbie',
60+
username: 'RobertBaratheon',
61+
},
62+
{ api_token: 'hunter2', oAuth2Data: { access_token: 'hunter2', state: 'abcde' } }
63+
)
64+
);
65+
});
66+
67+
afterEach(() => {
68+
nock.cleanAll();
69+
cleanup();
70+
});
71+
72+
afterAll(() => {
73+
nock.enableNetConnect();
74+
});
75+
76+
it('shows broken page', async () => {
77+
// __summary: 'count'
78+
nock(props.fhirBaseURL)
79+
.get(`/${locationResourceType}/_search`)
80+
.query({
81+
_total: 'accurate',
82+
_include: 'Location:partof',
83+
_getpagesoffset: 0,
84+
_count: 20,
85+
})
86+
.replyWithError({
87+
message: 'something awful happened',
88+
code: 'AWFUL_ERROR',
89+
});
90+
91+
render(<AppWrapper {...props} />);
92+
await waitForElementToBeRemoved(document.querySelector('.ant-spin'));
93+
expect(screen.getByText(/failed, reason: something awful happened/)).toBeInTheDocument();
94+
});
95+
96+
it('shows no data page', async () => {
97+
// __summary: 'count'
98+
nock(props.fhirBaseURL)
99+
.get(`/${locationResourceType}/_search`)
100+
.query({
101+
_total: 'accurate',
102+
_include: 'Location:partof',
103+
_getpagesoffset: 0,
104+
_count: 20,
105+
})
106+
.reply(200, { total: 0 });
107+
108+
render(<AppWrapper {...props} />);
109+
await waitForElementToBeRemoved(document.querySelector('.ant-spin'));
110+
expect(screen.getByText(/All Locations Flat/)).toBeInTheDocument();
111+
expect(screen.getByTitle(/Empty raw svg icon/)).toBeInTheDocument();
112+
expect(
113+
screen.getByText(/No data available to display, you can start adding data now/)
114+
).toBeInTheDocument();
115+
});
116+
117+
it('Show data as expected', async () => {
118+
// __summary: 'count'
119+
nock(props.fhirBaseURL)
120+
.get(`/${locationResourceType}/_search`)
121+
.query({
122+
_total: 'accurate',
123+
_include: 'Location:partof',
124+
_getpagesoffset: 0,
125+
_count: 20,
126+
})
127+
.reply(200, flatLocations);
128+
129+
render(<AppWrapper {...props} />);
130+
await waitForElementToBeRemoved(document.querySelector('.ant-spin'));
131+
expect(screen.getByText(/All Locations Flat/)).toBeInTheDocument();
132+
133+
// check table contnets
134+
const table = document.querySelector('table');
135+
// check table headers
136+
expect(table?.querySelectorAll('thead tr')).toHaveLength(1);
137+
const header = table?.querySelectorAll('thead tr');
138+
header?.forEach((td) => {
139+
expect(td.textContent).toMatchSnapshot('table header');
140+
});
141+
// check table body
142+
expect(table?.querySelectorAll('tbody tr')).toHaveLength(1);
143+
const firstRowTd = table?.querySelectorAll('tbody tr:nth-child(1) td');
144+
firstRowTd?.forEach((td) => {
145+
expect(td.textContent).toMatchSnapshot('table data');
146+
});
147+
});
148+
});

packages/fhir-location-management/src/ducks/tests/fixtures.ts

+82
Original file line numberDiff line numberDiff line change
@@ -1182,3 +1182,85 @@ export const fhirHierarchy = {
11821182

11831183
export const serializedSample =
11841184
'["{\\"config\\":{\\"childrenPropertyName\\":\\"children\\"},\\"model\\":{\\"nodeId\\":\\"Location/2252\\",\\"label\\":\\"Root FHIR Location\\",\\"node\\":{\\"resourceType\\":\\"Location\\",\\"id\\":\\"2252\\",\\"meta\\":{\\"versionId\\":\\"3\\",\\"lastUpdated\\":\\"2021-10-14T13:10:14.524+00:00\\",\\"source\\":\\"#5887f723a045b500\\"},\\"identifier\\":[{\\"use\\":\\"official\\",\\"value\\":\\"eff94f33-c356-4634-8795-d52340706ba9\\"}],\\"status\\":\\"active\\",\\"name\\":\\"Root FHIR Location\\",\\"alias\\":[\\"Root Location\\"],\\"description\\":\\"This is the Root Location that all other locations are part of. Any locations that are directly part of this should be displayed as the root location.\\",\\"physicalType\\":{\\"coding\\":[{\\"system\\":\\"http://terminology.hl7.org/CodeSystem/location-physical-type\\",\\"code\\":\\"jdn\\",\\"display\\":\\"Jurisdiction\\"}]}},\\"children\\":[{\\"nodeId\\":\\"Location/303\\",\\"label\\":\\"Ona Office Sub Location\\",\\"node\\":{\\"resourceType\\":\\"Location\\",\\"id\\":\\"303\\",\\"meta\\":{\\"versionId\\":\\"4\\",\\"lastUpdated\\":\\"2021-10-14T13:12:22.740+00:00\\",\\"source\\":\\"#13bbc7f09daa1751\\"},\\"identifier\\":[{\\"use\\":\\"official\\",\\"value\\":\\"93bc9c3d-6321-41b0-9b93-1275d7114e22\\"}],\\"status\\":\\"active\\",\\"name\\":\\"Ona Office Sub Location\\",\\"alias\\":[\\"ona office\\"],\\"description\\":\\"The Sub location\\",\\"physicalType\\":{\\"coding\\":[{\\"system\\":\\"http://terminology.hl7.org/CodeSystem/location-physical-type\\",\\"code\\":\\"jdn\\",\\"display\\":\\"Jurisdiction\\"}]},\\"partOf\\":{\\"reference\\":\\"Location/2252\\",\\"display\\":\\"Root FHIR Location\\"}},\\"parent\\":\\"Location/2252\\",\\"children\\":[]}]},\\"children\\":[{\\"config\\":{\\"$ref\\":\\"$[\\\\\\"config\\\\\\"]\\"},\\"model\\":{\\"$ref\\":\\"$[\\\\\\"model\\\\\\"][\\\\\\"children\\\\\\"][0]\\"},\\"children\\":[],\\"parent\\":{\\"$ref\\":\\"$\\"}}]}"]';
1185+
1186+
export const flatLocations = {
1187+
resourceType: 'Bundle',
1188+
id: '7363bc54-d734-4f0b-9444-5e87514754ec',
1189+
meta: {
1190+
lastUpdated: '2024-03-05T14:57:02.776+00:00',
1191+
},
1192+
type: 'searchset',
1193+
total: 1,
1194+
link: [
1195+
{
1196+
relation: 'self',
1197+
url: 'https://fhir.labs.smartregister.org/fhir/Location/_search?_count=1&_getpagesoffset=0&_include=Location%3Apartof&_total=accurate',
1198+
},
1199+
{
1200+
relation: 'next',
1201+
url: 'https://fhir.labs.smartregister.org/fhir?_getpages=7363bc54-d734-4f0b-9444-5e87514754ec&_getpagesoffset=5&_count=1&_pretty=true&_include=Location%3Apartof&_bundletype=searchset',
1202+
},
1203+
],
1204+
entry: [
1205+
{
1206+
fullUrl: 'https://fhir.labs.smartregister.org/fhir/Location/3509',
1207+
resource: {
1208+
resourceType: 'Location',
1209+
id: '3509',
1210+
meta: {
1211+
versionId: '1',
1212+
lastUpdated: '2021-10-13T08:04:47.087+00:00',
1213+
source: '#ca825ff8d77152b4',
1214+
},
1215+
status: 'active',
1216+
name: 'Good Health Clinic',
1217+
description: 'HL7 Headquarters',
1218+
mode: 'instance',
1219+
type: [
1220+
{
1221+
coding: [
1222+
{
1223+
system: 'http://terminology.hl7.org/CodeSystem/v3-RoleCode',
1224+
code: 'SLEEP',
1225+
display: 'Sleep disorders unit',
1226+
},
1227+
],
1228+
},
1229+
],
1230+
telecom: [
1231+
{
1232+
system: 'phone',
1233+
value: '(+1) 734-677-7777',
1234+
},
1235+
{
1236+
system: 'fax',
1237+
value: '(+1) 734-677-6622',
1238+
},
1239+
{
1240+
system: 'email',
1241+
1242+
},
1243+
],
1244+
address: {
1245+
line: ['3300 Washtenaw Avenue, Suite 227'],
1246+
city: 'Ann Arbor',
1247+
state: 'MI',
1248+
postalCode: '48104',
1249+
country: 'USA',
1250+
},
1251+
physicalType: {
1252+
coding: [
1253+
{
1254+
system: 'http://terminology.hl7.org/CodeSystem/location-physical-type',
1255+
code: 'bu',
1256+
display: 'Building',
1257+
},
1258+
],
1259+
},
1260+
},
1261+
search: {
1262+
mode: 'match',
1263+
},
1264+
},
1265+
],
1266+
};

packages/react-utils/src/components/NoData/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const EmptySvgIcon = () => (
1616
fill="none"
1717
xmlns="http://www.w3.org/2000/svg"
1818
>
19+
<title>Empty raw svg icon</title>
1920
<g clipPath="url(#clip0_22874_4831)">
2021
<circle cx="80" cy="79.5" r="79.5" fill="url(#paint0_linear_22874_4831)" />
2122
<path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { render } from '@testing-library/react';
2+
import { NoData } from '..';
3+
import React from 'react';
4+
5+
const props = { description: 'descriptive text' };
6+
7+
test('Component renders correctly', () => {
8+
const { getByText, getByTitle } = render(<NoData {...props} />);
9+
10+
expect(getByTitle(/Empty raw svg icon/)).toBeInTheDocument();
11+
expect(getByText(/No data available/)).toBeInTheDocument();
12+
expect(getByText(/descriptive text/i)).toBeInTheDocument();
13+
});
14+
15+
test('Component Renders with children', () => {
16+
const { getByText } = render(
17+
<NoData>
18+
<div>Additional children</div>
19+
</NoData>
20+
);
21+
22+
expect(getByText(/No data available/)).toBeInTheDocument();
23+
expect(getByText(/Additional children/)).toBeInTheDocument();
24+
});

0 commit comments

Comments
 (0)