Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Moved all form validation logic for vitals and biometrics form into zod #1800

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion __mocks__/react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const useMock = [(key) => key, {}];
useMock.t = (key, defaultValue, options = {}) => {
let translatedString = defaultValue;
Object.keys(options).forEach((key) => {
translatedString = defaultValue.replace(`{{${key}}}`, `${options[key]}`);
translatedString = translatedString.replace(`{{${key}}}`, `${options[key]}`);
});

return translatedString;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"*.{css,scss,ts,tsx}": "prettier --write --list-different"
},
"resolutions": {
"sass": "^1.54.3"
"sass": "^1.54.3",
"@openmrs/openmrs-form-engine-lib": "portal:/Users/vasharma05/Projects/openmrs-form-engine-lib"
},
"packageManager": "yarn@4.1.1",
"dependencies": {
Expand Down
12 changes: 6 additions & 6 deletions packages/esm-patient-vitals-app/src/common/data.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { type KeyedMutator } from 'swr';
import { type ConfigObject } from '../config-schema';
import { assessValue, calculateBodyMassIndex, getReferenceRangesForConcept, interpretBloodPressure } from './helpers';
import type { FHIRSearchBundleResponse, MappedVitals, PatientVitalsAndBiometrics, VitalsResponse } from './types';
import { type VitalsBiometricsFormData } from '../vitals-biometrics-form/vitals-biometrics-form.workspace';
import { type VitalsBiometricsFormData } from '../vitals-biometrics-form/types';

const pageSize = 100;

Expand Down Expand Up @@ -44,6 +44,7 @@ export interface ConceptMetadata {
lowAbsolute: number | null;
lowCritical: number | null;
units: string | null;
allowDecimal: boolean;
}

interface VitalsConceptMetadataResponse {
Expand All @@ -59,7 +60,7 @@ function getInterpretationKey(header: string) {

export function useVitalsConceptMetadata() {
const customRepresentation =
'custom:(setMembers:(uuid,display,hiNormal,hiAbsolute,hiCritical,lowNormal,lowAbsolute,lowCritical,units))';
'custom:(setMembers:(uuid,display,hiNormal,hiAbsolute,hiCritical,lowNormal,lowAbsolute,lowCritical,units,allowDecimal))';

const apiUrl = `${restBaseUrl}/concept/?q=VITALS SIGNS&v=${customRepresentation}`;

Expand All @@ -75,16 +76,17 @@ export function useVitalsConceptMetadata() {
: new Map<string, string>([]);

const conceptRanges = conceptMetadata?.length
? new Map<string, { lowAbsolute: number | null; highAbsolute: number | null }>(
? new Map<string, { lowAbsolute: number | null; highAbsolute: number | null; allowDecimal: boolean }>(
conceptMetadata.map((concept) => [
concept.uuid,
{
lowAbsolute: concept.lowAbsolute ?? null,
highAbsolute: concept.hiAbsolute ?? null,
allowDecimal: concept.allowDecimal ?? false,
},
]),
)
: new Map<string, { lowAbsolute: number | null; highAbsolute: number | null }>([]);
: new Map<string, { lowAbsolute: number | null; highAbsolute: number | null; allowDecimal: boolean }>([]);

return {
data: conceptUnits,
Expand Down Expand Up @@ -309,15 +311,13 @@ export function saveVitalsAndBiometrics(
concepts: ConfigObject['concepts'],
patientUuid: string,
vitals: VitalsBiometricsFormData,
abortController: AbortController,
location: string,
) {
return openmrsFetch<unknown>(`${restBaseUrl}/encounter`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
body: {
patient: patientUuid,
location: location,
Expand Down
1 change: 1 addition & 0 deletions packages/esm-patient-vitals-app/src/common/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ConceptMetadata } from '../common';
import { type VitalsBiometricsFormData } from '../vitals-biometrics-form/types';
import type { ObsReferenceRanges, ObservationInterpretation } from './types';

export function calculateBodyMassIndex(weight: number, height: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface VitalsBiometricsFormData {
systolicBloodPressure: number;
diastolicBloodPressure: number;
respiratoryRate: number;
oxygenSaturation: number;
pulse: number;
temperature: number;
generalPatientNote: string;
weight: number;
height: number;
midUpperArmCircumference: number;
computedBodyMassIndex: number;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import { screen, render, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { type FetchResponse, showSnackbar, useConfig, defineConfigSchema } from '@openmrs/esm-framework';
import { configSchema } from '../config-schema';
import { mockConceptMetadata, mockConceptRanges, mockConceptUnits, mockVitalsConfig } from '__mocks__';
import { mockPatient } from 'tools';
import { getByTextWithMarkup, mockPatient } from 'tools';
import { saveVitalsAndBiometrics } from '../common';
import VitalsAndBiometricsForm from './vitals-biometrics-form.workspace';

Expand Down Expand Up @@ -149,7 +149,6 @@ describe('VitalsBiometricsForm', () => {
temperature: temperatureValue,
weight: weightValue,
}),
new AbortController(),
undefined,
);

Expand All @@ -158,7 +157,7 @@ describe('VitalsBiometricsForm', () => {
expect.objectContaining({
isLowContrast: true,
kind: 'success',
subtitle: 'They are now visible on the Vitals and Biometrics page',
subtitle: 'The latest results are now visible on the Vitals and Biometrics page',
title: 'Vitals and Biometrics saved',
}),
);
Expand Down Expand Up @@ -219,21 +218,17 @@ describe('VitalsBiometricsForm', () => {
const temperature = screen.getByRole('spinbutton', { name: /temperature/i });

await user.type(systolic, '1000');
await user.tab();
await user.tab();
expect(screen.getByText(/Systolic value must be between 0 and 250/i)).toBeInTheDocument();
await user.type(pulse, pulseValue.toString());
await user.type(oxygenSaturation, '200');
await user.tab();
await user.type(temperature, temperatureValue.toString());
expect(screen.getByText(/Oxygen saturation value must be between 0 and 100/i)).toBeInTheDocument();

const saveButton = screen.getByRole('button', { name: /save and close/i });
await user.click(saveButton);

expect(screen.getByText(/Some of the values entered are invalid/i)).toBeInTheDocument();

// close the inline notification --> resubmit --> check for presence of inline notification
const closeInlineNotificationButton = screen.getByTitle(/close notification/i);
await user.click(closeInlineNotificationButton);
expect(screen.queryByText(/some of the values entered are invalid/i)).not.toBeInTheDocument();
await user.click(saveButton);
expect(screen.getByText(/Some of the values entered are invalid/i)).toBeInTheDocument();
});
});

Expand Down