Skip to content

Commit

Permalink
(fix) Throw error when queried form is not found (#399)
Browse files Browse the repository at this point in the history
* Throw error when queried form is not found

* Use a more specific error message
  • Loading branch information
samuelmale authored Sep 24, 2024
1 parent e5218d5 commit b0ee6ed
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ export async function fetchOpenMRSForm(nameOrUUID: string): Promise<OpenmrsForm
return openmrsFormResponse;
}

return openmrsFormResponse.results?.length
? openmrsFormResponse.results.find((form) => form.retired === false)
: new Error(`Form with ${nameOrUUID} was not found`);
if (openmrsFormResponse.results?.length) {
const form = openmrsFormResponse.results.find((form) => form.retired === false);
if (form) {
return form;
}
}
throw new Error(`Form with ID "${nameOrUUID}" was not found`);
}

/**
Expand All @@ -121,7 +125,7 @@ export async function fetchClobData(form: OpenmrsForm): Promise<any | null> {
return null;
}

const jsonSchemaResource = form.resources.find(({ name }) => name === 'JSON schema');
const jsonSchemaResource = form.resources?.find(({ name }) => name === 'JSON schema');
if (!jsonSchemaResource) {
return null;
}
Expand Down
31 changes: 26 additions & 5 deletions src/hooks/useFormJson.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const COMPONENT_ART_SCHEMA_VALUE_REF = '74d06044-850f-11ee-b9d1-0242ac120003';
const COMPONENT_PRECLINIC_REVIEW = 'component_preclinic-review';
const COMPONENT_PRECLINIC_REVIEW_UUID = '2f063f32-7f8a-11ee-b962-0242ac120004';
const COMPONENT_PRECLINIC_REVIEW_SCHEMA_VALUE_REF = '74d06044-850f-11ee-b9d1-0242ac120004';
const NON_EXISTENT_FORM_NAME = 'non-existent-form';

// Base setup
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
Expand Down Expand Up @@ -74,6 +75,7 @@ when(mockOpenmrsFetch)
when(mockOpenmrsFetch)
.calledWith(buildPath(COMPONENT_ART))
.mockResolvedValue({ data: { results: [artComponentSkeleton] } });

when(mockOpenmrsFetch).calledWith(buildPath(COMPONENT_ART_UUID)).mockResolvedValue({ data: artComponentSkeleton });
when(mockOpenmrsFetch)
.calledWith(buildPath(COMPONENT_ART_SCHEMA_VALUE_REF))
Expand All @@ -82,13 +84,18 @@ when(mockOpenmrsFetch)
when(mockOpenmrsFetch)
.calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW))
.mockResolvedValue({ data: { results: [preclinicReviewComponentSkeleton] } });

when(mockOpenmrsFetch)
.calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW_UUID))
.mockResolvedValue({ data: preclinicReviewComponentSkeleton });
when(mockOpenmrsFetch)
.calledWith(buildPath(COMPONENT_PRECLINIC_REVIEW_SCHEMA_VALUE_REF))
.mockResolvedValue({ data: preclinicReviewComponentBody });

when(mockOpenmrsFetch)
.calledWith(buildPath(NON_EXISTENT_FORM_NAME))
.mockResolvedValue({ data: { results: [] } });

describe('useFormJson', () => {
it('should fetch basic form by name', async () => {
let hook = null;
Expand All @@ -97,7 +104,7 @@ describe('useFormJson', () => {
});

expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.error).toBe(undefined);
expect(hook.result.current.formError).toBe(undefined);
expect(hook.result.current.formJson.name).toBe(MINI_FORM_NAME);
});

Expand All @@ -108,7 +115,7 @@ describe('useFormJson', () => {
});

expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.error).toBe(undefined);
expect(hook.result.current.formError).toBe(undefined);
expect(hook.result.current.formJson.name).toBe(MINI_FORM_NAME);
});

Expand All @@ -119,7 +126,7 @@ describe('useFormJson', () => {
});

expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.error).toBe(undefined);
expect(hook.result.current.formError).toBe(undefined);
expect(hook.result.current.formJson.name).toBe(PARENT_FORM_NAME);

// verify subforms
Expand All @@ -133,7 +140,7 @@ describe('useFormJson', () => {
});

expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.error).toBe(undefined);
expect(hook.result.current.formError).toBe(undefined);
expect(hook.result.current.formJson.name).toBe(PARENT_FORM_NAME);

// verify subforms
Expand All @@ -146,12 +153,26 @@ describe('useFormJson', () => {
hook = renderHook(() => useFormJson(null, formComponentBody, null, null));
});
expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.error).toBe(undefined);
expect(hook.result.current.formError).toBe(undefined);
expect(hook.result.current.formJson.name).toBe(COMPONENT_FORM_NAME);

// verify form components have been loaded
verifyFormComponents(hook.result.current.formJson);
});

it('should return an error when the form is not found', async () => {
// setup and execute
let hook = null;
await act(async () => {
hook = renderHook(() => useFormJson(NON_EXISTENT_FORM_NAME, null, null, null));
});
// verify
expect(hook.result.current.isLoading).toBe(false);
expect(hook.result.current.formError.message).toBe(
'Error loading form JSON: Form with ID "non-existent-form" was not found',
);
expect(hook.result.current.formJson).toBe(null);
});
});

function buildPath(path: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFormJson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: s

return {
formJson,
isLoading: !formJson,
isLoading: !formJson && !error,
formError: error,
};
}
Expand Down

0 comments on commit b0ee6ed

Please sign in to comment.