Skip to content

Commit

Permalink
Modify tests for zscore functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ODORA0 committed Nov 20, 2023
1 parent d0be0c6 commit c564b93
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 63 deletions.
2 changes: 1 addition & 1 deletion __mocks__/forms/ohri-forms/zscore-weight-height-form.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"label":"Weight for Height Zscore result",
"type":"obs",
"questionOptions":{
"rendering":"number",
"rendering":"text",
"calculate":{
"calculateExpression":"calcWeightForHeightZscore( 'height', 'weight')"
},
Expand Down
116 changes: 116 additions & 0 deletions __mocks__/patient.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,119 @@ export const mockPatient = {
},
],
};

export const mockPatientAge4 = {
resourceType: 'Patient',
id: '8673ee4f-e2ab-4077-ba55-4980f408773e',
extension: [
{
url: 'http://fhir-es.transcendinsights.com/stu3/StructureDefinition/resource-date-created',
valueDateTime: '2017-01-18T09:42:40+00:00',
},
{
url: 'https://purl.org/elab/fhir/StructureDefinition/Creator-crew-version1',
valueString: 'daemon',
},
],
identifier: [
{
id: '1f0ad7a1-430f-4397-b571-59ea654a52db',
use: 'secondary',
system: 'Old Identification Number',
value: '100732HE',
},
{
id: '1f0ad7a1-430f-4397-b571-59ea654a52db',
use: 'usual',
system: 'OpenMRS ID',
value: '100GEJ',
},
],
active: true,
name: [
{
id: 'efdb246f-4142-4c12-a27a-9be60b9592e9',
use: 'usual',
family: 'Wilson',
given: ['John'],
},
],
gender: 'male',
birthDate: '2020-01-01',
deceasedBoolean: false,
address: [
{
id: '0c244eae-85c8-4cc9-b168-96b51f864e77',
use: 'home',
line: ['Address10351'],
city: 'City0351',
state: 'State0351tested',
postalCode: '60351',
country: 'Country0351',
},
],
telecom: [
{
system: 'Mobile',
value: '+25467388299499',
},
],
};

export const mockPatientAge16 = {
resourceType: 'Patient',
id: 'e13a8696-dc58-4b8c-ae40-2a1e7dd843e7',
extension: [
{
url: 'http://fhir-es.transcendinsights.com/stu3/StructureDefinition/resource-date-created',
valueDateTime: '2017-01-18T09:42:40+00:00',
},
{
url: 'https://purl.org/elab/fhir/StructureDefinition/Creator-crew-version1',
valueString: 'daemon',
},
],
identifier: [
{
id: '1f0ad7a1-430f-4397-b571-59ea654a52db',
use: 'secondary',
system: 'Old Identification Number',
value: '100732HE',
},
{
id: '1f0ad7a1-430f-4397-b571-59ea654a52db',
use: 'usual',
system: 'OpenMRS ID',
value: '100GEJ',
},
],
active: true,
name: [
{
id: 'efdb246f-4142-4c12-a27a-9be60b9592e9',
use: 'usual',
family: 'Wilson',
given: ['John'],
},
],
gender: 'male',
birthDate: '2016-04-04',
deceasedBoolean: false,
address: [
{
id: '0c244eae-85c8-4cc9-b168-96b51f864e77',
use: 'home',
line: ['Address10351'],
city: 'City0351',
state: 'State0351tested',
postalCode: '60351',
country: 'Country0351',
},
],
telecom: [
{
system: 'Mobile',
value: '+25467388299499',
},
],
};
62 changes: 0 additions & 62 deletions src/ohri-form.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import hts_poc_1_1 from "../__mocks__/packages/hiv/forms/hts_poc/1.1.json";
import bmi_form from "../__mocks__/forms/ohri-forms/bmi-test-form.json";
import bsa_form from "../__mocks__/forms/ohri-forms/bsa-test-form.json";
import edd_form from "../__mocks__/forms/ohri-forms/edd-test-form.json";
import wh_zscore from "../__mocks__/forms/ohri-forms/zscore-weight-height-form.json";
import BMI_Zscore from "../__mocks__/forms/ohri-forms/zscore-bmi-for-age-form.json";
import WA_Zscore from "../__mocks__/forms/ohri-forms/zscore-height-for-age-form.json";
import filter_answer_options_form from "../__mocks__/forms/ohri-forms/filter-answer-options-test-form.json";
import test_enrolment_form from "../__mocks__/forms/ohri-forms/test-enrolment-form.json";
import next_visit_form from "../__mocks__/forms/ohri-forms/next-visit-test-form.json";
Expand Down Expand Up @@ -329,65 +326,6 @@ describe("OHRI Forms:", () => {
await act(async () => expect(bsaField.value).toBe("2.24"));
});

it("Should evaluate Weight for Height Zscore result", async () => {
// setup
await act(async () => renderForm(null, wh_zscore));

const WHField = await findNumberInput(screen, "Weight for Height Zscore result");
const heightField = await findNumberInput(screen, "Height");
const weightField = await findNumberInput(screen, "Weight");
await act(async () => expect(heightField.value).toBe(""));
await act(async () => expect(weightField.value).toBe(""));
await act(async () => expect(WHField.value).toBe(""));

// replay
fireEvent.blur(heightField, { target: { value: 110 } });
fireEvent.blur(weightField, { target: { value: 45 } });

// verify
await act(async () => expect(heightField.value).toBe("110"));
await act(async () => expect(weightField.value).toBe("45"));
await act(async () => expect(WHField.value).toBe(""));
});

it("Should evaluate BMI for Age Zscore result", async () => {
// setup
await act(async () => renderForm(null, BMI_Zscore));

const bmiAgeField = await findNumberInput(screen, "BMI for Age Zscore result");
const heightField = await findNumberInput(screen, "Height");
const weightField = await findNumberInput(screen, "Weight");
await act(async () => expect(heightField.value).toBe(""));
await act(async () => expect(weightField.value).toBe(""));
await act(async () => expect(bmiAgeField.value).toBe(""));

// replay
fireEvent.blur(heightField, { target: { value: 150 } });
fireEvent.blur(weightField, { target: { value: 50 } });

// verify
await act(async () => expect(heightField.value).toBe("150"));
await act(async () => expect(weightField.value).toBe("50"));
await act(async () => expect(bmiAgeField.value).toBe(""));
});

xit("Should evaluate Height for Age Zscore result", async () => {

// setup
await act(async () => renderForm(null, WA_Zscore));

const heightField = await findNumberInput(screen, "Height");
await act(async () => expect(heightField.value).toBe(""));
let assumeAgeToBe = "1/01/2006"

// replay
fireEvent.blur(heightField, { target: { value: 150 } });

// verify
await act(async () => expect(assumeAgeToBe).toBe("1/01/2006"));
await act(async () => expect(heightField.value).toBe("150"));
});

it("Should evaluate EDD", async () => {
// setup
await act(async () => renderForm(null, edd_form));
Expand Down
107 changes: 107 additions & 0 deletions src/zscore-tests/bmi-age.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { render, fireEvent, screen, cleanup, act, waitFor } from '@testing-library/react';
import { when } from 'jest-when';
import React from 'react';
import OHRIForm from '../ohri-form.component';
import BMI_Zscore from '../../__mocks__/forms/ohri-forms/zscore-bmi-for-age-form.json';
import { mockPatientAge16 } from '../../__mocks__/patient.mock';
import { mockSessionDataResponse } from '../../__mocks__/session.mock';
import demoHtsOpenmrsForm from '../../__mocks__/forms/omrs-forms/demo_hts-form.json';
import demoHtsOhriForm from '../../__mocks__/forms/ohri-forms/demo_hts-form.json';

import { findNumberInput } from '../utils/test-utils';
import { mockVisit } from '../../__mocks__/visit.mock';

//////////////////////////////////////////
////// Base setup
//////////////////////////////////////////
const mockUrl = `/ws/rest/v1/encounter?v=full`;
const patientUUID = 'e13a8696-dc58-4b8c-ae40-2a1e7dd843e7';
const visit = mockVisit;
const mockOpenmrsFetch = jest.fn();
const formsResourcePath = when((url: string) => url.includes('/ws/rest/v1/form/'));
const clobdataResourcePath = when((url: string) => url.includes('/ws/rest/v1/clobdata/'));
global.ResizeObserver = require('resize-observer-polyfill');
when(mockOpenmrsFetch).calledWith(formsResourcePath).mockReturnValue({ data: demoHtsOpenmrsForm });
when(mockOpenmrsFetch).calledWith(clobdataResourcePath).mockReturnValue({ data: demoHtsOhriForm });

const locale = window.i18next.language == 'en' ? 'en-GB' : window.i18next.language;

//////////////////////////////////////////
////// Mocks
//////////////////////////////////////////
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');

return {
...originalModule,
createErrorHandler: jest.fn(),
showNotification: jest.fn(),
showToast: jest.fn(),
getAsyncLifecycle: jest.fn(),
usePatient: jest.fn().mockImplementation(() => ({ patient: mockPatientAge16 })),
registerExtension: jest.fn(),
useSession: jest.fn().mockImplementation(() => mockSessionDataResponse.data),
openmrsFetch: jest.fn().mockImplementation((args) => mockOpenmrsFetch(args)),
};
});

jest.mock('../../src/api/api', () => {
const originalModule = jest.requireActual('../../src/api/api');

return {
...originalModule,
getPreviousEncounter: jest.fn().mockImplementation(() => Promise.resolve(null)),
fetchConceptNameByUuid: jest.fn().mockImplementation(() => Promise.resolve(null)),
getConcept: jest.fn().mockImplementation(() => Promise.resolve(null)),
getLatestObs: jest.fn().mockImplementation(() => Promise.resolve({ valueNumeric: 60 })),
saveEncounter: jest.fn(),
};
});

describe('OHRI Forms:', () => {
afterEach(() => {
cleanup();
jest.useRealTimers();
});

describe('Calcuated values', () => {
afterEach(() => {
cleanup();
});

it('Should evaluate BMI for Age Zscore result', async () => {
// setup
await act(async () => renderForm(null, BMI_Zscore));

const bmiAgeField = await findNumberInput(screen, 'BMI for Age Zscore result');
const heightField = await findNumberInput(screen, 'Height');
const weightField = await findNumberInput(screen, 'Weight');
await act(async () => expect(heightField.value).toBe(''));
await act(async () => expect(weightField.value).toBe(''));
await act(async () => expect(bmiAgeField.value).toBe(''));

// replay
fireEvent.blur(heightField, { target: { value: 100 } });
fireEvent.blur(weightField, { target: { value: 45 } });

// verify
await act(async () => expect(heightField.value).toBe('100'));
await act(async () => expect(weightField.value).toBe('45'));
await act(async () => expect(bmiAgeField.value).toBe('4'));
});
});

function renderForm(formUUID, formJson, intent?: string) {
return act(() => {
render(
<OHRIForm
formJson={formJson as any}
formUUID={formUUID}
patientUUID={patientUUID}
formSessionIntent={intent}
visit={visit}
/>,
);
});
}
});
Loading

0 comments on commit c564b93

Please sign in to comment.