From 8b494983d181904523aad645e14d2112441ec718 Mon Sep 17 00:00:00 2001 From: Dennis Kigen Date: Wed, 24 Jul 2024 11:32:41 +0300 Subject: [PATCH] Fixup --- __mocks__/vitals-and-biometrics.mock.ts | 10 +- jest.config.js | 1 + .../allergies-detailed-summary.test.tsx | 2 +- .../allergies-form/allergy-form.test.tsx | 100 +++++++---------- .../src/allergies/allergies-overview.test.tsx | 2 +- .../src/config-schema.ts | 1 + .../attachments/attachments-overview.test.tsx | 6 +- .../print-identifier-sticker-modal.test.tsx | 28 +++-- .../src/banner-tags/visit-tag.test.tsx | 2 +- .../src/actions-buttons/start-visit.test.tsx | 66 +++++++---- .../src/actions-buttons/stop-visit.test.tsx | 4 - .../src/config-schema.ts | 33 +++--- .../src/side-nav/side-menu.test.tsx | 18 ++- .../esm-patient-chart-app/src/utils.test.ts | 4 +- .../src/visit/past-visit-overview.test.tsx | 15 +-- .../src/visit/visit-form/visit-form.test.tsx | 95 +++++++--------- .../visit-prompt/cancel-visit-dialog.test.tsx | 40 ++++--- .../visit-prompt/start-visit-dialog.test.tsx | 10 +- .../current-visit-summary.test.tsx | 4 - .../encounters-table.test.tsx | 14 +-- .../visit-summary.test.tsx | 21 ++-- .../visits-table/visits-table.test.tsx | 19 +--- .../visit-detail-overview.test.tsx | 35 +++--- .../src/error-state/error-state.test.tsx | 2 +- .../src/conditions/conditions-form.test.tsx | 46 +++----- .../conditions/conditions-overview.test.tsx | 23 ++-- .../src/flags/flags-highlight-bar.test.tsx | 10 +- .../src/flags/flags-list.test.tsx | 4 +- .../src/flags/flags.test.tsx | 4 +- .../src/clinical-form-action-button.test.tsx | 14 +-- .../src/forms/form-view.test.tsx | 12 +- .../src/forms/forms-dashboard.test.tsx | 9 +- .../src/forms/forms-list.test.tsx | 15 ++- .../src/config-schema.ts | 2 +- .../immunization-widget-config-schema.ts | 2 +- .../immunizations/immunizations-form.test.tsx | 69 ++++++------ .../immunizations-overview.test.tsx | 2 +- .../add-lab-order/add-lab-order.test.tsx | 65 ++++++----- .../add-lab-order/useTestTypes.test.ts | 47 ++++---- .../lab-order-basket-panel.test.tsx | 2 +- .../patient-list-details-table.test.tsx | 30 ++--- .../patient-list-details.workspace.test.tsx | 16 +-- .../patient-lists.workspace.test.tsx | 6 +- .../active-medications.test.tsx | 2 +- .../drug-order-basket-panel.test.tsx | 2 +- .../src/notes/notes-main.component.test.tsx | 2 +- .../src/notes/notes-overview.test.tsx | 2 +- .../src/notes/visit-notes-form.test.tsx | 32 +++--- .../order-basket-action-button.test.tsx | 21 ++-- .../src/programs/programs-form.test.tsx | 42 ++++--- .../biometrics/biometrics-overview.test.tsx | 25 ++--- .../vitals-header-item.test.tsx | 2 +- .../vitals-header.test.tsx | 45 ++++---- .../vitals-biometrics-form.test.tsx | 40 ++++--- .../vitals-biometrics-input.test.tsx | 103 +++++++++--------- .../src/vitals/vitals-overview.test.tsx | 25 ++--- .../src/weight-tile/weight-tile.test.tsx | 22 ++-- 57 files changed, 608 insertions(+), 667 deletions(-) diff --git a/__mocks__/vitals-and-biometrics.mock.ts b/__mocks__/vitals-and-biometrics.mock.ts index 9383ee5415..64bb0b1398 100644 --- a/__mocks__/vitals-and-biometrics.mock.ts +++ b/__mocks__/vitals-and-biometrics.mock.ts @@ -5985,12 +5985,12 @@ export const mockFhirVitalsResponse = { }; export const mockBiometricsConfig = { - biometrics: { bmiUnit: 'kg / m²' }, concepts: { heightUuid: '5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', weightUuid: '5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' }, + biometrics: { bmiUnit: 'kg / m²' }, }; export const mockVitalsConfig = { - biometrics: { bmiUnit: 'kg / m²' }, + biometrics: { bmiUnit: 'kg / m²', heightUnit: 'm', weightUnit: 'kg' }, concepts: { diastolicBloodPressureUuid: '5086AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', generalPatientNoteUuid: '165095AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', @@ -6005,8 +6005,12 @@ export const mockVitalsConfig = { }, vitals: { encounterTypeUuid: '67a71486-1a54-468f-ac3e-7091a9a79584', - formUuid: 'a000cb34-9ec1-4344-a1c8-f692232f6edd', + formUuid: '9f26aad4-244a-46ca-be49-1196df1a8c9a', useFormEngine: false, + logo: null, + showPrintButton: false, + formName: 'Vitals', + useMuacColors: false, }, }; diff --git a/jest.config.js b/jest.config.js index 0d2ef7d519..1a6f4287e8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,6 +3,7 @@ const path = require('path'); module.exports = { + clearMocks: true, transform: { '^.+\\.(j|t)sx?$': '@swc/jest', }, diff --git a/packages/esm-patient-allergies-app/src/allergies/allergies-detailed-summary.test.tsx b/packages/esm-patient-allergies-app/src/allergies/allergies-detailed-summary.test.tsx index a95025aed8..0a974acbc2 100644 --- a/packages/esm-patient-allergies-app/src/allergies/allergies-detailed-summary.test.tsx +++ b/packages/esm-patient-allergies-app/src/allergies/allergies-detailed-summary.test.tsx @@ -12,7 +12,7 @@ const testProps = { const mockOpenmrsFetch = openmrsFetch as jest.Mock; mockOpenmrsFetch.mockImplementation(jest.fn()); -describe('AllergiesDetailedSummary: ', () => { +describe('AllergiesDetailedSummary', () => { it('renders an empty state view if allergy data is unavailable', async () => { mockOpenmrsFetch.mockReturnValueOnce({ data: { entry: [] } }); renderAllergiesDetailedSummary(); diff --git a/packages/esm-patient-allergies-app/src/allergies/allergies-form/allergy-form.test.tsx b/packages/esm-patient-allergies-app/src/allergies/allergies-form/allergy-form.test.tsx index 1d0767e558..c73e509473 100644 --- a/packages/esm-patient-allergies-app/src/allergies/allergies-form/allergy-form.test.tsx +++ b/packages/esm-patient-allergies-app/src/allergies/allergies-form/allergy-form.test.tsx @@ -1,7 +1,7 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { screen, render, within } from '@testing-library/react'; -import { type FetchResponse, showSnackbar, useConfig } from '@openmrs/esm-framework'; +import { type FetchResponse, getDefaultsFromConfigSchema, showSnackbar, useConfig } from '@openmrs/esm-framework'; import { mockAllergens, mockAllergicReactions } from '__mocks__'; import { mockPatient } from 'tools'; import { @@ -11,8 +11,9 @@ import { useAllergicReactions, updatePatientAllergy, } from './allergy-form.resource'; -import { AllergenType } from '../../types'; import { mockAllergy } from '__mocks__'; +import { configSchema } from '../../config-schema'; +import { AllergenType } from '../../types'; import AllergyForm from './allergy-form.workspace'; const mockSaveAllergy = saveAllergy as jest.Mock>; @@ -22,17 +23,13 @@ const mockShowSnackbar = showSnackbar as jest.Mock; const mockUpdatePatientAllergy = updatePatientAllergy as jest.Mock; const mockUseConfig = useConfig as jest.Mock; -jest.mock('./allergy-form.resource', () => { - const originalModule = jest.requireActual('./allergy-form.resource'); - - return { - ...originalModule, - saveAllergy: jest.fn().mockResolvedValue({ data: {}, status: 201, statusText: 'Created' }), - updatePatientAllergy: jest.fn().mockResolvedValue({ data: {}, status: 200, statusText: 'Updated' }), - useAllergens: jest.fn(), - useAllergicReactions: jest.fn(), - }; -}); +jest.mock('./allergy-form.resource', () => ({ + ...jest.requireActual('./allergy-form.resource'), + saveAllergy: jest.fn().mockResolvedValue({ data: {}, status: 201, statusText: 'Created' }), + updatePatientAllergy: jest.fn().mockResolvedValue({ data: {}, status: 200, statusText: 'Updated' }), + useAllergens: jest.fn(), + useAllergicReactions: jest.fn(), +})); const mockConcepts = { drugAllergenUuid: '162552AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', @@ -45,29 +42,27 @@ const mockConcepts = { otherConceptUuid: '5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', }; -mockUseAllergens.mockReturnValue({ - isLoading: false, - allergens: mockAllergens, -}); - -mockUseAllergicReactions.mockReturnValue({ - isLoading: false, - allergicReactions: mockAllergicReactions, -}); - -describe('AllergyForm ', () => { +describe('AllergyForm', () => { beforeEach(() => { - jest.clearAllMocks(); - + mockUseAllergens.mockReturnValue({ + isLoading: false, + allergens: mockAllergens, + }); + mockUseAllergicReactions.mockReturnValue({ + isLoading: false, + allergicReactions: mockAllergicReactions, + }); mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), concepts: mockConcepts, }); }); it('renders the allergy form with all the expected fields and values', async () => { - renderAllergyForm(); const user = userEvent.setup(); + renderAllergyForm(); + const allergensContainer = screen.getByTestId('allergens-container'); const allergenInput = screen.queryByPlaceholderText(/select the allergen/i); @@ -92,9 +87,10 @@ describe('AllergyForm ', () => { }); it('enable the save button when all required fields are filled', async () => { - renderAllergyForm(); const user = userEvent.setup(); + renderAllergyForm(); + const allergen = mockAllergens[0]; const reaction = mockAllergicReactions[0]; @@ -124,10 +120,10 @@ describe('AllergyForm ', () => { }); it('calls the saveAllergy function with the correct payload', async () => { - mockSaveAllergy.mockClear(); + const user = userEvent.setup(); + renderAllergyForm(); - const user = userEvent.setup(); const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const allergen = mockAllergens[0]; @@ -157,9 +153,10 @@ describe('AllergyForm ', () => { }); it('displays a custom input and a warning message when select other allergen', async () => { + const user = userEvent.setup(); + renderAllergyForm(); - const user = userEvent.setup(); const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const allergensContainer = screen.getByTestId('allergens-container'); @@ -176,10 +173,10 @@ describe('AllergyForm ', () => { }); it('calls the saveAllergy function with the correct payload when select other allergen', async () => { - mockSaveAllergy.mockClear(); + const user = userEvent.setup(); + renderAllergyForm(); - const user = userEvent.setup(); const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const allergensContainer = screen.getByTestId('allergens-container'); const customAllergen = 'some other allergen'; @@ -213,12 +210,10 @@ describe('AllergyForm ', () => { }); it('renders a success notification after successful submission', async () => { - mockSaveAllergy.mockClear(); - mockShowSnackbar.mockClear(); + const user = userEvent.setup(); renderAllergyForm(); - const user = userEvent.setup(); const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const allergen = mockAllergens[0]; @@ -242,8 +237,8 @@ describe('AllergyForm ', () => { }); it('renders an error snackbar upon an invalid submission', async () => { - mockSaveAllergy.mockClear(); - mockShowSnackbar.mockClear(); + const user = userEvent.setup(); + mockSaveAllergy.mockRejectedValue({ message: 'Internal Server Error', response: { @@ -254,7 +249,6 @@ describe('AllergyForm ', () => { renderAllergyForm(); - const user = userEvent.setup(); const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const allergen = mockAllergens[0]; @@ -276,11 +270,12 @@ describe('AllergyForm ', () => { kind: 'error', }); }); - it('Edit Allergy should call the saveAllergy function with updated payload', async () => { - mockSaveAllergy.mockClear(); - renderEditAllergyForm(); + it('Edit Allergy should call the saveAllergy function with updated payload', async () => { const user = userEvent.setup(); + + renderAllergyForm({ allergy: mockAllergy, formContext: 'editing' }); + const allergenInput = screen.getByPlaceholderText(/select the allergen/i); const commentInput = screen.getByLabelText(/Date of onset and comments/i); @@ -317,8 +312,8 @@ describe('AllergyForm ', () => { }); }); -function renderAllergyForm() { - const testProps = { +function renderAllergyForm(props = {}) { + const defaultProps = { closeWorkspace: () => {}, closeWorkspaceWithSavedChanges: () => {}, promptBeforeClosing: () => {}, @@ -329,20 +324,5 @@ function renderAllergyForm() { setTitle: jest.fn(), }; - render(); -} - -function renderEditAllergyForm() { - const testProps = { - closeWorkspace: () => {}, - closeWorkspaceWithSavedChanges: () => {}, - promptBeforeClosing: () => {}, - allergy: mockAllergy, - formContext: 'editing' as 'creating' | 'editing', - patient: mockPatient, - patientUuid: mockPatient.id, - setTitle: jest.fn(), - }; - - render(); + render(); } diff --git a/packages/esm-patient-allergies-app/src/allergies/allergies-overview.test.tsx b/packages/esm-patient-allergies-app/src/allergies/allergies-overview.test.tsx index bf815d41bf..c9187b1662 100644 --- a/packages/esm-patient-allergies-app/src/allergies/allergies-overview.test.tsx +++ b/packages/esm-patient-allergies-app/src/allergies/allergies-overview.test.tsx @@ -12,7 +12,7 @@ const testProps = { const mockOpenmrsFetch = openmrsFetch as jest.Mock; -describe('AllergiesOverview: ', () => { +describe('AllergiesOverview', () => { it('renders an empty state view if allergy data is unavailable', async () => { mockOpenmrsFetch.mockReturnValueOnce({ data: [] }); renderAllergiesOverview(); diff --git a/packages/esm-patient-allergies-app/src/config-schema.ts b/packages/esm-patient-allergies-app/src/config-schema.ts index 50e5bbc309..f93f6cb6b5 100644 --- a/packages/esm-patient-allergies-app/src/config-schema.ts +++ b/packages/esm-patient-allergies-app/src/config-schema.ts @@ -12,6 +12,7 @@ export interface AllergiesConfigObject { otherConceptUuid: string; }; } + export const configSchema = { concepts: { drugAllergenUuid: { diff --git a/packages/esm-patient-attachments-app/src/attachments/attachments-overview.test.tsx b/packages/esm-patient-attachments-app/src/attachments/attachments-overview.test.tsx index bad7f90db0..bb5eda3a41 100644 --- a/packages/esm-patient-attachments-app/src/attachments/attachments-overview.test.tsx +++ b/packages/esm-patient-attachments-app/src/attachments/attachments-overview.test.tsx @@ -3,10 +3,10 @@ import { render, screen } from '@testing-library/react'; import AttachmentsOverview from './attachments-overview.component'; import { useAttachments } from '@openmrs/esm-framework'; -const mockedUseAttachments = jest.mocked(useAttachments); +const mockUseAttachments = jest.mocked(useAttachments); it('renders a loading skeleton when attachments are loading', () => { - mockedUseAttachments.mockReturnValue({ + mockUseAttachments.mockReturnValue({ data: [], error: null, isLoading: true, @@ -21,7 +21,7 @@ it('renders a loading skeleton when attachments are loading', () => { }); it('renders an empty state if attachments are not available', () => { - mockedUseAttachments.mockReturnValue({ + mockUseAttachments.mockReturnValue({ data: [], error: null, isLoading: false, diff --git a/packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.test.tsx b/packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.test.tsx index 9e7d23d52c..e97e1172a5 100644 --- a/packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.test.tsx +++ b/packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.test.tsx @@ -2,13 +2,14 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { render, screen } from '@testing-library/react'; import { useReactToPrint } from 'react-to-print'; -import { useConfig } from '@openmrs/esm-framework'; +import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework'; +import { configSchema, type ConfigObject } from '../config-schema'; import { mockPatient } from 'tools'; import PrintIdentifierSticker from './print-identifier-sticker.modal'; -const mockedCloseModal = jest.fn(); -const mockedUseReactToPrint = jest.mocked(useReactToPrint); -const mockedUseConfig = jest.mocked(useConfig); +const mockCloseModal = jest.fn(); +const mockUseReactToPrint = jest.mocked(useReactToPrint); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); jest.mock('react-to-print', () => { const originalModule = jest.requireActual('react-to-print'); @@ -19,15 +20,12 @@ jest.mock('react-to-print', () => { }; }); -describe('PrintIdentifierSticker', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockedUseConfig.mockReturnValue({ - printIdentifierStickerFields: ['name', 'identifier', 'age', 'dateOfBirth', 'gender'], - }); - }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + printIdentifierStickerFields: ['name', 'identifier', 'age', 'dateOfBirth', 'gender'], +}); +describe('PrintIdentifierSticker', () => { test('renders the component', () => { renderPrintIdentifierSticker(); @@ -46,12 +44,12 @@ describe('PrintIdentifierSticker', () => { expect(cancelButton).toBeInTheDocument(); await user.click(cancelButton); - expect(mockedCloseModal).toHaveBeenCalled(); + expect(mockCloseModal).toHaveBeenCalled(); }); test('calls the print function when print button is clicked', async () => { const handlePrint = jest.fn(); - mockedUseReactToPrint.mockReturnValue(handlePrint); + mockUseReactToPrint.mockReturnValue(handlePrint); const user = userEvent.setup(); @@ -66,5 +64,5 @@ describe('PrintIdentifierSticker', () => { }); function renderPrintIdentifierSticker() { - render(); + render(); } diff --git a/packages/esm-patient-banner-app/src/banner-tags/visit-tag.test.tsx b/packages/esm-patient-banner-app/src/banner-tags/visit-tag.test.tsx index 0898cc81e1..c862c47d6c 100644 --- a/packages/esm-patient-banner-app/src/banner-tags/visit-tag.test.tsx +++ b/packages/esm-patient-banner-app/src/banner-tags/visit-tag.test.tsx @@ -13,7 +13,7 @@ jest.mock('@openmrs/esm-patient-common-lib', () => ({ useVisitOrOfflineVisit: jest.fn(), })); -describe('VisitBannerTag: ', () => { +describe('VisitBannerTag', () => { it('renders an active visit tag when an active visit is ongoing', () => { mockUseVisitOrOfflineVisit.mockReturnValue({ activeVisit: mockCurrentVisit, diff --git a/packages/esm-patient-chart-app/src/actions-buttons/start-visit.test.tsx b/packages/esm-patient-chart-app/src/actions-buttons/start-visit.test.tsx index e90ab55b2b..17a2c3a8b6 100644 --- a/packages/esm-patient-chart-app/src/actions-buttons/start-visit.test.tsx +++ b/packages/esm-patient-chart-app/src/actions-buttons/start-visit.test.tsx @@ -1,22 +1,27 @@ import React from 'react'; -import { screen, render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { useConfig, usePatient, useVisit } from '@openmrs/esm-framework'; +import { screen, render } from '@testing-library/react'; +import { + getDefaultsFromConfigSchema, + useConfig, + usePatient, + useVisit, + type VisitReturnType, +} from '@openmrs/esm-framework'; import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib'; +import { type ChartConfig, esmPatientChartSchema } from '../config-schema'; import { mockPatient } from 'tools'; import StartVisitOverflowMenuItem from './start-visit.component'; -const mockUsePatient = usePatient as jest.Mock; -const mockUseVisit = useVisit as jest.Mock; -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => ChartConfig>(useConfig); +const mockUsePatient = jest.mocked(usePatient); +const mockUseVisit = jest.mocked(useVisit); jest.mock('@openmrs/esm-framework', () => ({ - usePatient: jest.fn(), - useVisit: jest.fn(), - getGlobalStore: jest.fn(), + ...jest.requireActual('@openmrs/esm-framework'), createGlobalStore: jest.fn(), createUseStore: jest.fn(), - useConfig: jest.fn(), + getGlobalStore: jest.fn(), })); jest.mock('@openmrs/esm-patient-common-lib', () => { @@ -28,32 +33,49 @@ jest.mock('@openmrs/esm-patient-common-lib', () => { }; }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(esmPatientChartSchema), +}); + +mockUseVisit.mockReturnValue({ + currentVisit: null, +} as VisitReturnType); + describe('StartVisitOverflowMenuItem', () => { - it('should launch start visit form', async () => { + it('should launch the start visit form', async () => { const user = userEvent.setup(); - mockUseConfig.mockReturnValue({ startVisitLabel: '' }); - mockUseVisit.mockReturnValue({ currentVisit: null }); - mockUsePatient.mockReturnValue({ patient: mockPatient }); - render(); + mockUsePatient.mockReturnValue({ + error: null, + isLoading: false, + patient: mockPatient, + patientUuid: mockPatient.id, + }); - const startVisitButton = screen.getByRole('menuitem', { name: /Start visit/ }); + render(); + + const startVisitButton = screen.getByRole('menuitem', { name: /start visit/i }); expect(startVisitButton).toBeInTheDocument(); await user.click(startVisitButton); - expect(launchPatientWorkspace).toHaveBeenCalledTimes(1); expect(launchPatientWorkspace).toHaveBeenCalledWith('start-visit-workspace-form'); }); - it('should not show start visit button for deceased patient', () => { - mockUseConfig.mockReturnValue({ startVisitLabel: '' }); - mockUseVisit.mockReturnValue({ currentVisit: null }); - mockUsePatient.mockReturnValue({ patient: { ...mockPatient, deceasedDateTime: '2023-05-07T10:20:30Z' } }); + it('should not show start visit button for a deceased patient', () => { + mockUsePatient.mockReturnValue({ + error: null, + isLoading: false, + patientUuid: mockPatient.id, + patient: { + ...mockPatient, + deceasedDateTime: '2023-05-07T10:20:30Z', + }, + }); - render(); + render(); - const startVisitButton = screen.queryByRole('menuitem', { name: /Start visit/ }); + const startVisitButton = screen.queryByRole('menuitem', { name: /start visit/i }); expect(startVisitButton).not.toBeInTheDocument(); }); }); diff --git a/packages/esm-patient-chart-app/src/actions-buttons/stop-visit.test.tsx b/packages/esm-patient-chart-app/src/actions-buttons/stop-visit.test.tsx index 5c16ccf2e9..d8c91a450f 100644 --- a/packages/esm-patient-chart-app/src/actions-buttons/stop-visit.test.tsx +++ b/packages/esm-patient-chart-app/src/actions-buttons/stop-visit.test.tsx @@ -16,10 +16,6 @@ jest.mock('@openmrs/esm-framework', () => ({ })); describe('StopVisitOverflowMenuItem', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it('should be able to stop current visit', async () => { const user = userEvent.setup(); diff --git a/packages/esm-patient-chart-app/src/config-schema.ts b/packages/esm-patient-chart-app/src/config-schema.ts index 665e89c231..cb11bd61c8 100644 --- a/packages/esm-patient-chart-app/src/config-schema.ts +++ b/packages/esm-patient-chart-app/src/config-schema.ts @@ -134,28 +134,29 @@ export const esmPatientChartSchema = { _default: false, }, }; + export interface ChartConfig { - freeTextFieldConceptUuid: string; - offlineVisitTypeUuid: string; - visitTypeResourceUrl: string; - showRecommendedVisitTypeTab: boolean; - visitAttributeTypes: Array<{ - uuid: string; - required: boolean; - displayInThePatientBanner: boolean; - showWhenExpression?: string; - }>; - showServiceQueueFields: boolean; - visitQueueNumberAttributeUuid: string; - showAllEncountersTab: boolean; defaultFacilityUrl: string; - showUpcomingAppointments: boolean; + disableChangingVisitLocation: boolean; + freeTextFieldConceptUuid: string; logo: { - src: string; alt: string; name: string; + src: string; }; - disableChangingVisitLocation: boolean; numberOfVisitsToLoad: number; + offlineVisitTypeUuid: string; + showAllEncountersTab: boolean; showExtraVisitAttributesSlot: boolean; + showRecommendedVisitTypeTab: boolean; + showServiceQueueFields: boolean; + showUpcomingAppointments: boolean; + visitQueueNumberAttributeUuid: string; + visitTypeResourceUrl: string; + visitAttributeTypes: Array<{ + displayInThePatientBanner: boolean; + required: boolean; + showWhenExpression?: string; + uuid: string; + }>; } diff --git a/packages/esm-patient-chart-app/src/side-nav/side-menu.test.tsx b/packages/esm-patient-chart-app/src/side-nav/side-menu.test.tsx index f986cb8042..5ae48dfcd3 100644 --- a/packages/esm-patient-chart-app/src/side-nav/side-menu.test.tsx +++ b/packages/esm-patient-chart-app/src/side-nav/side-menu.test.tsx @@ -4,19 +4,15 @@ import { isDesktop, LeftNavMenu, useLayoutType } from '@openmrs/esm-framework'; import SideMenu from './side-menu.component'; const mockIsDesktop = jest.mocked(isDesktop); -const mockedLeftNavMenu = LeftNavMenu as unknown as jest.Mock; -const mockedUseLayoutType = jest.mocked(useLayoutType); +const mockLeftNavMenu = LeftNavMenu as unknown as jest.Mock; +const mockUseLayoutType = jest.mocked(useLayoutType); -mockedLeftNavMenu.mockReturnValue('left nav menu'); +mockLeftNavMenu.mockReturnValue('left nav menu'); +mockIsDesktop.mockImplementation((layout) => layout === 'small-desktop' || layout === 'large-desktop'); -describe('sidemenu', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockIsDesktop.mockImplementation((layout) => layout === 'small-desktop' || layout === 'large-desktop'); - }); +describe('Side menu', () => { it('is rendered when viewport == large-desktop', () => { - mockedUseLayoutType.mockImplementationOnce(() => 'large-desktop'); + mockUseLayoutType.mockImplementationOnce(() => 'large-desktop'); renderSideMenu(); @@ -24,7 +20,7 @@ describe('sidemenu', () => { }); it('is not rendered when viewport == tablet or viewport == small-desktop', () => { - mockedUseLayoutType.mockImplementationOnce(() => 'tablet'); + mockUseLayoutType.mockImplementationOnce(() => 'tablet'); renderSideMenu(); diff --git a/packages/esm-patient-chart-app/src/utils.test.ts b/packages/esm-patient-chart-app/src/utils.test.ts index af612a894d..acc08fdafd 100644 --- a/packages/esm-patient-chart-app/src/utils.test.ts +++ b/packages/esm-patient-chart-app/src/utils.test.ts @@ -3,11 +3,11 @@ import { isDesktop as actualIsDesktopFn } from '@openmrs/esm-framework'; const mockIsDesktop = jest.mocked(actualIsDesktopFn); +mockIsDesktop.mockImplementation((layout) => layout === 'small-desktop' || layout === 'large-desktop'); + describe('isDesktop', () => { beforeEach(() => { jest.clearAllMocks(); - - mockIsDesktop.mockImplementation((layout) => layout === 'small-desktop' || layout === 'large-desktop'); }); it('is true when layout = tablet', () => { diff --git a/packages/esm-patient-chart-app/src/visit/past-visit-overview.test.tsx b/packages/esm-patient-chart-app/src/visit/past-visit-overview.test.tsx index cd04934e09..9b45eb6208 100644 --- a/packages/esm-patient-chart-app/src/visit/past-visit-overview.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/past-visit-overview.test.tsx @@ -5,7 +5,7 @@ import { openmrsFetch, setCurrentVisit } from '@openmrs/esm-framework'; import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools'; import PastVisitOverview from './past-visit-overview.component'; -const testProps = { +const defaultProps = { closeWorkspace: jest.fn(), closeWorkspaceWithSavedChanges: jest.fn(), patientUuid: mockPatient.id, @@ -48,11 +48,6 @@ const mockOpenmrsFetch = openmrsFetch as jest.Mock; const mockSetCurrentVisit = setCurrentVisit as jest.Mock; describe('PastVisitOverview', () => { - beforeEach(() => { - testProps.closeWorkspace.mockClear(); - mockSetCurrentVisit.mockClear(); - }); - it(`renders a tabular overview view of the patient's past visits data`, async () => { const user = userEvent.setup(); @@ -76,7 +71,7 @@ describe('PastVisitOverview', () => { await user.click(cancelButton); - expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1); + expect(defaultProps.closeWorkspace).toHaveBeenCalledTimes(1); }); it(`will enter retrospective entry mode for a specific visit`, async () => { @@ -88,11 +83,11 @@ describe('PastVisitOverview', () => { expect(editButtons.length).toBe(2); await user.click(editButtons[1]); - expect(mockSetCurrentVisit).toBeCalledWith(mockPatient.id, mockPastVisits.data.results[1].uuid); - expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1); + expect(mockSetCurrentVisit).toHaveBeenCalledWith(mockPatient.id, mockPastVisits.data.results[1].uuid); + expect(defaultProps.closeWorkspace).toHaveBeenCalledTimes(1); }); }); function renderPastVisitOverview() { - renderWithSwr(); + renderWithSwr(); } diff --git a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx index f1c863ec43..ebf5199874 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visit-form/visit-form.test.tsx @@ -19,7 +19,6 @@ import { useVisitAttributeType } from '../hooks/useVisitAttributeType'; import StartVisitForm from './visit-form.component'; const visitUuid = 'test_visit_uuid'; - const visitAttributes = { punctuality: { uuid: '57ea0cbb-064f-4d09-8cf4-e8228700491c', @@ -59,29 +58,31 @@ const testProps = { const mockSaveVisit = saveVisit as jest.Mock; const mockUpdateVisit = updateVisit as jest.Mock; const mockOpenmrsFetch = openmrsFetch as jest.Mock; -const mockUseConfig = jest.mocked(useConfig); +const mockUseConfig = jest.mocked<() => ChartConfig>(useConfig); const mockUseVisitAttributeType = useVisitAttributeType as jest.Mock; const mockGetStartedVisitGetter = jest.fn(); const mockUseVisitTypes = useVisitTypes as jest.Mock; -jest.mock('@openmrs/esm-framework', () => { - const originalModule = jest.requireActual('@openmrs/esm-framework'); - - return { - ...originalModule, +jest.mock('@openmrs/esm-patient-common-lib', () => ({ + ...jest.requireActual('@openmrs/esm-patient-common-lib'), + useActivePatientEnrollment: jest.fn().mockReturnValue({ + activePatientEnrollment: [], + isLoading: false, + }), +})); - get getStartedVisit() { - return mockGetStartedVisitGetter(); - }, - restBaseUrl: '/ws/rest/v1', - saveVisit: jest.fn(), - updateVisit: jest.fn(), - toOmrsIsoString: jest.fn(), - toDateObjectStrict: jest.fn(), - useVisitTypes: jest.fn().mockImplementation(() => mockVisitTypes), - usePatient: jest.fn().mockImplementation((patientUuid) => ({ patientUuid, patient: {} })), - }; -}); +jest.mock('@openmrs/esm-framework', () => ({ + ...jest.requireActual('@openmrs/esm-framework'), + get getStartedVisit() { + return mockGetStartedVisitGetter(); + }, + restBaseUrl: '/ws/rest/v1', + saveVisit: jest.fn(), + updateVisit: jest.fn(), + toOmrsIsoString: jest.fn(), + toDateObjectStrict: jest.fn(), + usePatient: jest.fn().mockImplementation((patientUuid) => ({ patientUuid, patient: {} })), +})); jest.mock('../hooks/useVisitAttributeType', () => ({ useVisitAttributeType: jest.fn((attributeUuid) => { @@ -135,18 +136,6 @@ jest.mock('../hooks/useVisitAttributeType', () => ({ })), })); -jest.mock('@openmrs/esm-patient-common-lib', () => { - const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); - - return { - ...originalModule, - useActivePatientEnrollment: jest.fn().mockReturnValue({ - activePatientEnrollment: [], - isLoading: false, - }), - }; -}); - jest.mock('../hooks/useDefaultLocation', () => { const requireActual = jest.requireActual('../hooks/useDefaultLocation'); @@ -171,31 +160,27 @@ jest.mock('../hooks/useLocations', () => { }; }); -describe('Visit Form', () => { - beforeAll(() => { - mockUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(esmPatientChartSchema) as ChartConfig), - visitAttributeTypes: [ - { - uuid: visitAttributes.punctuality.uuid, - required: false, - displayInThePatientBanner: true, - }, - { - uuid: visitAttributes.insurancePolicyNumber.uuid, - required: false, - displayInThePatientBanner: true, - }, - ], - }); +mockUseVisitTypes.mockReturnValue(mockVisitTypes); - mockUseVisitTypes.mockReturnValue(mockVisitTypes); - }); +mockUseConfig.mockReturnValue({ + ...(getDefaultsFromConfigSchema(esmPatientChartSchema) as ChartConfig), + visitAttributeTypes: [ + { + uuid: visitAttributes.punctuality.uuid, + required: false, + displayInThePatientBanner: true, + }, + { + uuid: visitAttributes.insurancePolicyNumber.uuid, + required: false, + displayInThePatientBanner: true, + }, + ], +}); - beforeEach(() => { - jest.clearAllMocks(); - }); +mockUseVisitTypes.mockReturnValue(mockVisitTypes); +describe('Visit form', () => { it('renders the Start Visit form with all the relevant fields and values', async () => { renderVisitForm(); @@ -593,7 +578,9 @@ describe('Visit Form', () => { error: new Error('failed to load'), data: visitAttributes.punctuality, }); + mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema, visitAttributeTypes: [ { uuid: visitAttributes.punctuality.uuid, @@ -601,7 +588,7 @@ describe('Visit Form', () => { displayInThePatientBanner: true, }, ], - }); + } as ChartConfig); renderVisitForm(); diff --git a/packages/esm-patient-chart-app/src/visit/visit-prompt/cancel-visit-dialog.test.tsx b/packages/esm-patient-chart-app/src/visit/visit-prompt/cancel-visit-dialog.test.tsx index f32269e2db..e5f24ca262 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-prompt/cancel-visit-dialog.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visit-prompt/cancel-visit-dialog.test.tsx @@ -8,12 +8,12 @@ import { type MappedVisitQueueEntry, useVisitQueueEntry } from '../queue-entry/q import { removeQueuedPatient } from '../hooks/useServiceQueue'; import CancelVisitDialog from './cancel-visit-dialog.component'; -const mockedCloseModal = jest.fn(); -const mockedOpenmrsFetch = jest.mocked(openmrsFetch); -const mockedRemoveQueuedPatient = jest.mocked(removeQueuedPatient); -const mockedShowSnackbar = jest.mocked(showSnackbar); -const mockedUseVisit = jest.mocked(useVisit) as jest.Mock; -const mockedUseVisitQueueEntry = jest.mocked(useVisitQueueEntry); +const mockCloseModal = jest.fn(); +const mockOpenmrsFetch = jest.mocked(openmrsFetch); +const mockRemoveQueuedPatient = jest.mocked(removeQueuedPatient); +const mockShowSnackbar = jest.mocked(showSnackbar); +const mockUseVisit = jest.mocked(useVisit) as jest.Mock; +const mockUseVisitQueueEntry = jest.mocked(useVisitQueueEntry); jest.mock('@openmrs/esm-framework', () => { const originalModule = jest.requireActual('@openmrs/esm-framework'); @@ -46,22 +46,22 @@ describe('Cancel visit', () => { it('cancels the active visit and voids its associated encounters', async () => { const user = userEvent.setup(); - mockedUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: jest.fn() }); + mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: jest.fn() }); const response: Partial = { statusText: 'ok', status: 200, }; - mockedOpenmrsFetch.mockResolvedValue(response as FetchResponse); - mockedUseVisitQueueEntry.mockReturnValueOnce({ + mockOpenmrsFetch.mockResolvedValue(response as FetchResponse); + mockUseVisitQueueEntry.mockReturnValueOnce({ queueEntry: mockVisitQueueEntries, isLoading: false, isError: undefined, isValidating: false, mutate: jest.fn(), }); - mockedRemoveQueuedPatient.mockResolvedValue(response as FetchResponse); + mockRemoveQueuedPatient.mockResolvedValue(response as FetchResponse); renderCancelVisitDialog(); @@ -80,11 +80,10 @@ describe('Cancel visit', () => { await user.click(cancelVisitButton); - expect(mockedOpenmrsFetch).toHaveBeenCalledWith(`/ws/rest/v1/visit/${mockCurrentVisit.uuid}`, { + expect(mockOpenmrsFetch).toHaveBeenCalledWith(`/ws/rest/v1/visit/${mockCurrentVisit.uuid}`, { method: 'DELETE', }); - - expect(mockedShowSnackbar).toHaveBeenCalledWith( + expect(mockShowSnackbar).toHaveBeenCalledWith( expect.objectContaining({ kind: 'success', title: 'Visit cancelled', @@ -101,9 +100,9 @@ describe('Cancel visit', () => { status: 200, }; - mockedUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: jest.fn() }); - mockedOpenmrsFetch.mockRejectedValueOnce({ message: 'Internal server error', status: 500 }); - mockedUseVisitQueueEntry.mockReturnValueOnce({ + mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: jest.fn() }); + mockOpenmrsFetch.mockRejectedValueOnce({ message: 'Internal server error', status: 500 }); + mockUseVisitQueueEntry.mockReturnValueOnce({ queueEntry: {} as MappedVisitQueueEntry, isLoading: false, isError: undefined, @@ -111,7 +110,7 @@ describe('Cancel visit', () => { mutate: jest.fn(), }); - mockedRemoveQueuedPatient.mockResolvedValue(response as FetchResponse); + mockRemoveQueuedPatient.mockResolvedValue(response as FetchResponse); renderCancelVisitDialog(); @@ -126,11 +125,10 @@ describe('Cancel visit', () => { await user.click(cancelVisitButton); - expect(mockedOpenmrsFetch).toHaveBeenCalledWith(`/ws/rest/v1/visit/${mockCurrentVisit.uuid}`, { + expect(mockOpenmrsFetch).toHaveBeenCalledWith(`/ws/rest/v1/visit/${mockCurrentVisit.uuid}`, { method: 'DELETE', }); - - expect(mockedShowSnackbar).toHaveBeenCalledWith({ + expect(mockShowSnackbar).toHaveBeenCalledWith({ subtitle: 'An error occured when deleting visit', kind: 'error', title: 'Error cancelling active visit', @@ -139,5 +137,5 @@ describe('Cancel visit', () => { }); function renderCancelVisitDialog() { - render(); + render(); } diff --git a/packages/esm-patient-chart-app/src/visit/visit-prompt/start-visit-dialog.test.tsx b/packages/esm-patient-chart-app/src/visit/visit-prompt/start-visit-dialog.test.tsx index 0fde1f6dca..d9a9475fcb 100644 --- a/packages/esm-patient-chart-app/src/visit/visit-prompt/start-visit-dialog.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visit-prompt/start-visit-dialog.test.tsx @@ -4,7 +4,7 @@ import userEvent from '@testing-library/user-event'; import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib'; import StartVisitDialog from './start-visit-dialog.component'; -const testProps = { +const defaultProps = { patientUuid: 'some-uuid', closeModal: jest.fn(), visitType: null, @@ -41,9 +41,7 @@ describe('StartVisit', () => { test('should launch edit past visit form', async () => { const user = userEvent.setup(); - testProps.visitType = 'past'; - - renderStartVisitDialog(); + renderStartVisitDialog({ visitType: 'past' }); expect( screen.getByText( @@ -59,6 +57,6 @@ describe('StartVisit', () => { }); }); -function renderStartVisitDialog() { - render(); +function renderStartVisitDialog(props = {}) { + render(); } diff --git a/packages/esm-patient-chart-app/src/visit/visits-widget/current-visit-summary.test.tsx b/packages/esm-patient-chart-app/src/visit/visits-widget/current-visit-summary.test.tsx index 3f824e26e2..f2352d27d7 100644 --- a/packages/esm-patient-chart-app/src/visit/visits-widget/current-visit-summary.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visits-widget/current-visit-summary.test.tsx @@ -14,10 +14,6 @@ jest.mock('@openmrs/esm-framework', () => ({ })); describe('CurrentVisitSummary', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - test('renders an empty state when there is no active visit', () => { mockUseVisits.mockReturnValueOnce({ currentVisit: null, diff --git a/packages/esm-patient-chart-app/src/visit/visits-widget/encounters-table/encounters-table.test.tsx b/packages/esm-patient-chart-app/src/visit/visits-widget/encounters-table/encounters-table.test.tsx index dca101c0aa..0ecd8d8c10 100644 --- a/packages/esm-patient-chart-app/src/visit/visits-widget/encounters-table/encounters-table.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visits-widget/encounters-table/encounters-table.test.tsx @@ -5,25 +5,21 @@ import { renderWithSwr } from 'tools'; import { mockEncounters2 } from '__mocks__'; import EncountersTable from './encounters-table.component'; -const testProps = { +const defaultProps = { showAllEncounters: true, encounters: mockEncounters2, }; describe('EncounterList', () => { it('renders an empty state when no encounters are available', () => { - testProps.encounters = []; - - renderEncountersTable(); + renderEncountersTable({ encounters: [] }); expect(screen.queryByRole('table')).not.toBeInTheDocument(); expect(screen.getByText(/no encounters found/i)).toBeInTheDocument(); }); it("renders a tabular overview of the patient's clinical encounters", async () => { - testProps.encounters = mockEncounters2; - - renderEncountersTable(); + renderEncountersTable({ encounters: mockEncounters2 }); expect(screen.getByRole('table')).toBeInTheDocument(); }); @@ -46,6 +42,6 @@ describe('EncounterList', () => { }); }); -function renderEncountersTable() { - renderWithSwr(); +function renderEncountersTable(props = {}) { + renderWithSwr(); } diff --git a/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visit-summary.test.tsx b/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visit-summary.test.tsx index 2c325d5e7f..f896f6ac89 100644 --- a/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visit-summary.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visit-summary.test.tsx @@ -1,7 +1,8 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; -import { ExtensionSlot, getConfig, useConfig } from '@openmrs/esm-framework'; +import { ExtensionSlot, getConfig, getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework'; import { screen, render } from '@testing-library/react'; +import { esmPatientChartSchema } from '../../../config-schema'; import { mockPatient } from 'tools'; import { visitOverviewDetailMockData, visitOverviewDetailMockDataNotEmpty } from '__mocks__'; import VisitSummary from './visit-summary.component'; @@ -11,18 +12,14 @@ const mockGetConfig = getConfig as jest.Mock; const mockUseConfig = useConfig as jest.Mock; const mockVisit = visitOverviewDetailMockData.data.results[0]; -describe('VisitSummary', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockUseConfig.mockReturnValue({ - notesConceptUuids: ['162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'some-uuid2'], - visitDiagnosisConceptUuid: '159947AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', - }); - - mockExtensionSlot.mockImplementation((ext) => ext.name); - }); +mockExtensionSlot.mockImplementation((ext) => ext.name); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(esmPatientChartSchema), + notesConceptUuids: ['162169AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'some-uuid2'], + visitDiagnosisConceptUuid: '159947AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', +}); +describe('VisitSummary', () => { it('should display empty state for notes, test and medication summary', async () => { const user = userEvent.setup(); mockGetConfig.mockReturnValue(Promise.resolve({ htmlFormEntryForms: [] })); diff --git a/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visits-table/visits-table.test.tsx b/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visits-table/visits-table.test.tsx index ca03ba27b5..44b7888972 100644 --- a/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visits-table/visits-table.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visits-widget/past-visits-components/visits-table/visits-table.test.tsx @@ -6,7 +6,7 @@ import { mockPatient, renderWithSwr } from 'tools'; import { mockEncounters } from '__mocks__'; import VisitsTable from './visits-table.component'; -const testProps = { +const defaultProps = { patientUuid: mockPatient.id, showAllEncounters: true, visits: mockEncounters, @@ -27,15 +27,10 @@ jest.mock('@openmrs/esm-framework', () => { }); describe('EncounterList', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - it('renders an empty state when no encounters are available', async () => { - testProps.visits = []; mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] }); - renderVisitsTable(); + renderVisitsTable({ visits: [] }); await screen.findByTitle(/empty data illustration/i); expect(screen.getByText(/there are no encounters to display for this patient/i)).toBeInTheDocument(); @@ -43,9 +38,8 @@ describe('EncounterList', () => { it("renders a tabular overview of the patient's clinical encounters", async () => { const user = userEvent.setup(); - testProps.visits = mockEncounters; - renderVisitsTable(); + renderVisitsTable({ visits: mockEncounters }); await screen.findByRole('table'); @@ -94,11 +88,10 @@ describe('EncounterList', () => { describe('Delete Encounter', () => { it('Clicking the `Delete` button deletes an encounter', async () => { const user = userEvent.setup(); - testProps.visits = mockEncounters; mockUserHasAccess.mockReturnValue(true); - renderVisitsTable(); + renderVisitsTable({ visits: mockEncounters }); await screen.findByRole('table'); expect(screen.getByRole('table')).toBeInTheDocument(); @@ -120,6 +113,6 @@ describe('Delete Encounter', () => { }); }); -function renderVisitsTable() { - renderWithSwr(); +function renderVisitsTable(props = {}) { + renderWithSwr(); } diff --git a/packages/esm-patient-chart-app/src/visit/visits-widget/visit-detail-overview.test.tsx b/packages/esm-patient-chart-app/src/visit/visits-widget/visit-detail-overview.test.tsx index 5ceb03a363..5f701c374d 100644 --- a/packages/esm-patient-chart-app/src/visit/visits-widget/visit-detail-overview.test.tsx +++ b/packages/esm-patient-chart-app/src/visit/visits-widget/visit-detail-overview.test.tsx @@ -1,7 +1,8 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { screen } from '@testing-library/react'; -import { openmrsFetch, getConfig, useConfig, userHasAccess } from '@openmrs/esm-framework'; +import { openmrsFetch, getConfig, useConfig, userHasAccess, getDefaultsFromConfigSchema } from '@openmrs/esm-framework'; +import { esmPatientChartSchema, type ChartConfig } from '../../config-schema'; import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools'; import { visitOverviewDetailMockData } from '__mocks__'; import VisitDetailOverview from './visit-detail-overview.component'; @@ -11,26 +12,22 @@ const testProps = { }; const mockOpenmrsFetch = openmrsFetch as jest.Mock; -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => ChartConfig>(useConfig); const mockGetConfig = getConfig as jest.Mock; const mockUserHasAccess = userHasAccess as jest.Mock; -jest.mock('@openmrs/esm-framework', () => { - const originalModule = jest.requireActual('@openmrs/esm-framework'); +jest.mock('@openmrs/esm-framework', () => ({ + ...jest.requireActual('@openmrs/esm-framework'), + getVisitsForPatient: jest.fn(), + userHasAccess: jest.fn().mockImplementation((privilege, _) => (privilege ? false : true)), +})); - return { - ...originalModule, - getVisitsForPatient: jest.fn(), - userHasAccess: jest.fn().mockImplementation((privilege, _) => (privilege ? false : true)), - }; +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(esmPatientChartSchema), + numberOfVisitsToLoad: 5, }); describe('VisitDetailOverview', () => { - beforeEach(() => { - jest.clearAllMocks(); - mockUseConfig.mockReturnValue({ numberOfVisitsToLoad: 5 }); - }); - it('renders an empty state view if encounters data is unavailable', async () => { mockOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } }); mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] }); @@ -70,7 +67,10 @@ describe('VisitDetailOverview', () => { mockOpenmrsFetch.mockReturnValueOnce(visitOverviewDetailMockData); mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] }); - mockUseConfig.mockImplementation(() => ({ showAllEncountersTab: true })); + mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(esmPatientChartSchema), + showAllEncountersTab: true, + }); renderVisitDetailOverview(); @@ -103,7 +103,10 @@ describe('VisitDetailOverview', () => { it('should render only the visit summary tab when showAllEncountersTab is false', async () => { mockOpenmrsFetch.mockReturnValueOnce(visitOverviewDetailMockData); mockGetConfig.mockResolvedValue({ htmlFormEntryForms: [] }); - mockUseConfig.mockImplementation(() => ({ showAllEncountersTab: false })); + mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(esmPatientChartSchema), + showAllEncountersTab: false, + }); renderVisitDetailOverview(); diff --git a/packages/esm-patient-common-lib/src/error-state/error-state.test.tsx b/packages/esm-patient-common-lib/src/error-state/error-state.test.tsx index 6a009c48c5..927275ec20 100644 --- a/packages/esm-patient-common-lib/src/error-state/error-state.test.tsx +++ b/packages/esm-patient-common-lib/src/error-state/error-state.test.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; import { ErrorState } from '.'; -describe('ErrorState: ', () => { +describe('ErrorState', () => { it('renders an error state widget card', () => { const testError = { response: { diff --git a/packages/esm-patient-conditions-app/src/conditions/conditions-form.test.tsx b/packages/esm-patient-conditions-app/src/conditions/conditions-form.test.tsx index 461bd3e619..257b4d20e0 100644 --- a/packages/esm-patient-conditions-app/src/conditions/conditions-form.test.tsx +++ b/packages/esm-patient-conditions-app/src/conditions/conditions-form.test.tsx @@ -11,7 +11,7 @@ import ConditionsForm from './conditions-form.workspace'; const utc = require('dayjs/plugin/utc'); dayjs.extend(utc); -const testProps = { +const defaultProps = { condition: null, closeWorkspace: jest.fn(), closeWorkspaceWithSavedChanges: jest.fn(), @@ -21,6 +21,10 @@ const testProps = { setTitle: jest.fn(), }; +function renderConditionsForm(props = {}) { + render(); +} + const mockCreateCondition = jest.mocked(createCondition); const mockUseConditionsSearch = jest.mocked(useConditionsSearch); const mockShowSnackbar = jest.mocked(showSnackbar); @@ -33,21 +37,16 @@ jest.mock('./conditions.resource', () => ({ useConditionsSearch: jest.fn(), })); -describe('Conditions form', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockOpenmrsFetch.mockResolvedValue({ data: [] } as FetchResponse); - - mockUseConditionsSearch.mockReturnValue({ - searchResults: [], - error: null, - isSearching: false, - }); +mockOpenmrsFetch.mockResolvedValue({ data: [] } as FetchResponse); +mockUseConditionsSearch.mockReturnValue({ + searchResults: [], + error: null, + isSearching: false, +}); - mockCreateCondition.mockResolvedValue({ status: 201, body: 'Condition created' } as unknown as FetchResponse); - }); +mockCreateCondition.mockResolvedValue({ status: 201, body: 'Condition created' } as unknown as FetchResponse); +describe('Conditions form', () => { it('renders the conditions form with all the relevant fields and values', () => { renderConditionsForm(); @@ -75,7 +74,7 @@ describe('Conditions form', () => { const cancelButton = screen.getByRole('button', { name: /cancel/i }); await user.click(cancelButton); - expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1); + expect(defaultProps.closeWorkspace).toHaveBeenCalledTimes(1); }); it('setting the status of a condition to "inactive" reveals the end date input field', async () => { @@ -193,16 +192,13 @@ describe('Conditions form', () => { error: null, isSearching: false, }); - mockOpenmrsFetch.mockResolvedValue({ - data: mockFhirConditionsResponse, - mutate: Promise.resolve(undefined), - } as unknown as FetchResponse); + + mockCreateCondition.mockResolvedValue({ status: 201, body: 'Condition created' } as unknown as FetchResponse); renderConditionsForm(); const conditionSearchInput = screen.getByRole('searchbox', { name: /enter condition/i }); const submitButton = screen.getByRole('button', { name: /save & close/i }); - await user.click(submitButton); expect(screen.getByText(/a condition is required/i)).toBeInTheDocument(); @@ -243,11 +239,9 @@ describe('Conditions form', () => { id: 'f4ee2cfe-3880-4ea2-a5a6-82aa8a0f6389', }; - testProps.condition = conditionToEdit; - testProps.formContext = 'editing' as 'creating' | 'editing'; - mockOpenmrsFetch.mockResolvedValue({ data: mockFhirConditionsResponse } as FetchResponse); - renderConditionsForm(); + + renderConditionsForm({ condition: conditionToEdit, formContext: 'editing' }); expect(screen.queryByRole('searchbox', { name: /enter condition/i })).not.toBeInTheDocument(); @@ -258,7 +252,3 @@ describe('Conditions form', () => { await user.click(submitButton); }); }); - -function renderConditionsForm() { - render(); -} diff --git a/packages/esm-patient-conditions-app/src/conditions/conditions-overview.test.tsx b/packages/esm-patient-conditions-app/src/conditions/conditions-overview.test.tsx index 5319061da9..427408e038 100644 --- a/packages/esm-patient-conditions-app/src/conditions/conditions-overview.test.tsx +++ b/packages/esm-patient-conditions-app/src/conditions/conditions-overview.test.tsx @@ -1,8 +1,9 @@ import React from 'react'; import { screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { type FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework'; import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib'; +import { type FetchResponse, getDefaultsFromConfigSchema, openmrsFetch, useConfig } from '@openmrs/esm-framework'; +import { type ConfigObject, configSchema } from '../config-schema'; import { mockFhirConditionsResponse } from '__mocks__'; import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools'; import ConditionsOverview from './conditions-overview.component'; @@ -11,24 +12,20 @@ const testProps = { patientUuid: mockPatient.id, }; -const mockUseConfig = jest.mocked(useConfig); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); const mockOpenmrsFetch = jest.mocked(openmrsFetch); -jest.mock('@openmrs/esm-patient-common-lib', () => { - const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); +jest.mock('@openmrs/esm-patient-common-lib', () => ({ + ...jest.requireActual('@openmrs/esm-patient-common-lib'), + launchPatientWorkspace: jest.fn(), +})); - return { - ...originalModule, - launchPatientWorkspace: jest.fn(), - }; +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + conditionPageSize: 5, }); describe('ConditionsOverview', () => { - beforeEach(() => { - mockOpenmrsFetch.mockClear(); - mockUseConfig.mockReturnValue({ conditionPageSize: 5 }); - }); - it('renders an empty state view if conditions data is unavailable', async () => { mockOpenmrsFetch.mockResolvedValueOnce({ data: [] } as FetchResponse); diff --git a/packages/esm-patient-flags-app/src/flags/flags-highlight-bar.test.tsx b/packages/esm-patient-flags-app/src/flags/flags-highlight-bar.test.tsx index c1cf45f9a8..01be0b2724 100644 --- a/packages/esm-patient-flags-app/src/flags/flags-highlight-bar.test.tsx +++ b/packages/esm-patient-flags-app/src/flags/flags-highlight-bar.test.tsx @@ -7,7 +7,7 @@ import { mockPatientFlags } from '__mocks__'; import { usePatientFlags } from './hooks/usePatientFlags'; import FlagsHighlightBar from './flags-highlight-bar.component'; -const mockedUsePatientFlags = usePatientFlags as jest.Mock; +const mockUsePatientFlags = jest.mocked(usePatientFlags); jest.mock('@openmrs/esm-patient-common-lib', () => { const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); @@ -30,11 +30,13 @@ jest.mock('./hooks/usePatientFlags', () => { it('renders a highlights bar showing a summary of the available flags', async () => { const user = userEvent.setup(); - mockedUsePatientFlags.mockReturnValue({ + mockUsePatientFlags.mockReturnValue({ flags: mockPatientFlags, - isLoading: false, error: null, - }); + isLoading: false, + isValidating: false, + mutate: jest.fn(), + } as unknown as ReturnType); renderFlagsHighlightBar(); diff --git a/packages/esm-patient-flags-app/src/flags/flags-list.test.tsx b/packages/esm-patient-flags-app/src/flags/flags-list.test.tsx index 5eb6d38d5c..37ffe6611d 100644 --- a/packages/esm-patient-flags-app/src/flags/flags-list.test.tsx +++ b/packages/esm-patient-flags-app/src/flags/flags-list.test.tsx @@ -5,7 +5,7 @@ import { mockPatientFlags } from '__mocks__'; import { usePatientFlags } from './hooks/usePatientFlags'; import FlagsList from './flags-list.component'; -const mockedUsePatientFlags = usePatientFlags as jest.Mock; +const mockUsePatientFlags = usePatientFlags as jest.Mock; jest.mock('./hooks/usePatientFlags', () => { const originalModule = jest.requireActual('./hooks/usePatientFlags'); @@ -17,7 +17,7 @@ jest.mock('./hooks/usePatientFlags', () => { }); it('renders an Edit form that enables users to toggle flags on or off', async () => { - mockedUsePatientFlags.mockReturnValue({ + mockUsePatientFlags.mockReturnValue({ flags: mockPatientFlags, isLoading: false, error: null, diff --git a/packages/esm-patient-flags-app/src/flags/flags.test.tsx b/packages/esm-patient-flags-app/src/flags/flags.test.tsx index 44df946a72..5c5acdd7dc 100644 --- a/packages/esm-patient-flags-app/src/flags/flags.test.tsx +++ b/packages/esm-patient-flags-app/src/flags/flags.test.tsx @@ -7,7 +7,7 @@ import { mockPatientFlags } from '__mocks__'; import { usePatientFlags } from './hooks/usePatientFlags'; import Flags from './flags.component'; -const mockedUsePatientFlags = usePatientFlags as jest.Mock; +const mockUsePatientFlags = usePatientFlags as jest.Mock; jest.mock('@openmrs/esm-patient-common-lib', () => { const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); @@ -30,7 +30,7 @@ jest.mock('./hooks/usePatientFlags', () => { it('renders flags in the patient flags slot', async () => { const user = userEvent.setup(); - mockedUsePatientFlags.mockReturnValue({ + mockUsePatientFlags.mockReturnValue({ flags: mockPatientFlags, isLoading: false, error: null, diff --git a/packages/esm-patient-forms-app/src/clinical-form-action-button.test.tsx b/packages/esm-patient-forms-app/src/clinical-form-action-button.test.tsx index 8727b3aee9..f10f91db89 100644 --- a/packages/esm-patient-forms-app/src/clinical-form-action-button.test.tsx +++ b/packages/esm-patient-forms-app/src/clinical-form-action-button.test.tsx @@ -1,11 +1,10 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; -import { ActionMenuButton, useConnectivity, useLayoutType, useWorkspaces } from '@openmrs/esm-framework'; +import { ActionMenuButton, useLayoutType, useWorkspaces } from '@openmrs/esm-framework'; import ClinicalFormActionButton from './clinical-form-action-button.component'; -const mockedUseLayoutType = useLayoutType as jest.Mock; -const mockedUseWorkspaces = useWorkspaces as jest.Mock; - +const mockUseLayoutType = useLayoutType as jest.Mock; +const mockUseWorkspaces = useWorkspaces as jest.Mock; const MockActionMenuButton = ActionMenuButton as jest.Mock; MockActionMenuButton.mockImplementation(({ handler, label, tagContent }) => ( @@ -14,11 +13,12 @@ MockActionMenuButton.mockImplementation(({ handler, label, tagContent }) => ( )); -mockedUseWorkspaces.mockImplementation(() => ({ +mockUseWorkspaces.mockImplementation(() => ({ active: true, windowState: 'normal', workspaces: [ { + canHide: false, name: 'clinical-forms-workspace', title: 'Clinical forms', preferredWindowSize: 'normal', @@ -57,14 +57,14 @@ jest.mock('@openmrs/esm-patient-common-lib', () => { }); test('should display clinical form action button on tablet view', () => { - mockedUseLayoutType.mockReturnValue('tablet'); + mockUseLayoutType.mockReturnValue('tablet'); render(); expect(screen.getByRole('button', { name: /Clinical forms/i })).toBeInTheDocument(); }); test('should display clinical form action button on desktop view', () => { - mockedUseLayoutType.mockReturnValue('desktop'); + mockUseLayoutType.mockReturnValue('desktop'); render(); const clinicalActionButton = screen.getByRole('button', { name: /Form/i }); diff --git a/packages/esm-patient-forms-app/src/forms/form-view.test.tsx b/packages/esm-patient-forms-app/src/forms/form-view.test.tsx index 0c33fca69b..2b5d4146fa 100644 --- a/packages/esm-patient-forms-app/src/forms/form-view.test.tsx +++ b/packages/esm-patient-forms-app/src/forms/form-view.test.tsx @@ -1,15 +1,16 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { render, screen } from '@testing-library/react'; -import { showModal, useConfig } from '@openmrs/esm-framework'; +import { getDefaultsFromConfigSchema, showModal, useConfig } from '@openmrs/esm-framework'; import { launchFormEntryOrHtmlForms, useVisitOrOfflineVisit } from '@openmrs/esm-patient-common-lib'; +import { configSchema, type ConfigObject } from '../config-schema'; import { mockCurrentVisit, mockForms } from '__mocks__'; import { mockPatient } from 'tools'; import FormView from './form-view.component'; const mockLaunchFormEntryOrHtmlForms = launchFormEntryOrHtmlForms as jest.Mock; const mockShowModal = showModal as jest.Mock; -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); const mockUseVisitOrOfflineVisit = useVisitOrOfflineVisit as jest.Mock; jest.mock('@openmrs/esm-patient-common-lib', () => { @@ -24,6 +25,11 @@ jest.mock('@openmrs/esm-patient-common-lib', () => { }; }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + htmlFormEntryForms: [], +}); + describe('FormView', () => { test('should display `start-visit-dialog` when no visit has been started', async () => { const user = userEvent.setup(); @@ -31,7 +37,6 @@ describe('FormView', () => { mockUseVisitOrOfflineVisit.mockReturnValueOnce({ currentVisit: null, }); - mockUseConfig.mockReturnValue({ htmlFormEntryForms: [] }); renderFormView(); @@ -46,7 +51,6 @@ describe('FormView', () => { test('should launch form-entry patient-workspace window when visit is started', async () => { const user = userEvent.setup(); - mockUseConfig.mockReturnValue({ htmlFormEntryForms: [] }); mockUseVisitOrOfflineVisit.mockReturnValue({ currentVisit: mockCurrentVisit, error: null, diff --git a/packages/esm-patient-forms-app/src/forms/forms-dashboard.test.tsx b/packages/esm-patient-forms-app/src/forms/forms-dashboard.test.tsx index d429ffaa65..57ad5643bd 100644 --- a/packages/esm-patient-forms-app/src/forms/forms-dashboard.test.tsx +++ b/packages/esm-patient-forms-app/src/forms/forms-dashboard.test.tsx @@ -1,11 +1,12 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; -import { useConfig } from '@openmrs/esm-framework'; +import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework'; import { useVisitOrOfflineVisit } from '@openmrs/esm-patient-common-lib'; +import { configSchema, type ConfigObject } from '../config-schema'; import { mockCurrentVisit } from '__mocks__'; import FormsDashboard from './forms-dashboard.component'; -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); const mockUseVisitOrOfflineVisit = useVisitOrOfflineVisit as jest.Mock; jest.mock('../hooks/use-forms', () => ({ @@ -27,10 +28,10 @@ jest.mock('@openmrs/esm-patient-common-lib', () => { }; }); +mockUseConfig.mockReturnValue({ ...getDefaultsFromConfigSchema(configSchema), htmlFormEntryForms: [] }); + describe('FormsDashboard', () => { test('renders an empty state if there are no forms persisted on the server', async () => { - mockUseConfig.mockReturnValue({ htmlFormEntryForms: [] }); - mockUseVisitOrOfflineVisit.mockReturnValue({ currentVisit: mockCurrentVisit, error: null, diff --git a/packages/esm-patient-forms-app/src/forms/forms-list.test.tsx b/packages/esm-patient-forms-app/src/forms/forms-list.test.tsx index d7e5069ad3..5c43542011 100644 --- a/packages/esm-patient-forms-app/src/forms/forms-list.test.tsx +++ b/packages/esm-patient-forms-app/src/forms/forms-list.test.tsx @@ -6,7 +6,7 @@ import FormsList, { type FormsListProps } from './forms-list.component'; jest.mock('lodash-es/debounce', () => jest.fn((fn) => fn)); -const testProps: FormsListProps & { reset: () => void } = { +const defaultProps: FormsListProps & { reset: () => void } = { completedForms: [], handleFormOpen: jest.fn(), reset() { @@ -14,15 +14,18 @@ const testProps: FormsListProps & { reset: () => void } = { }, }; +function renderFormsList(props = {}) { + renderWithSwr(); +} + beforeEach(async () => { - testProps.reset(); + defaultProps.reset(); }); it('renders a list of forms fetched from the server', async () => { const user = userEvent.setup(); - testProps.completedForms = forms.map((form) => ({ form, associatedEncounters: [] })); - renderFormsList(); + renderFormsList({ completedForms: forms.map((form) => ({ form, associatedEncounters: [] })) }); const searchbox = screen.getByRole('searchbox'); expect(searchbox).toBeInTheDocument(); @@ -51,10 +54,6 @@ it('renders a list of forms fetched from the server', async () => { expect(screen.getByRole('row', { name: /laboratory tests never/i })).toBeInTheDocument(); }); -function renderFormsList() { - renderWithSwr(); -} - const forms = [ { uuid: '4077b82a-6d5b-4fc6-abb9-8bc6846600f0', diff --git a/packages/esm-patient-immunizations-app/src/config-schema.ts b/packages/esm-patient-immunizations-app/src/config-schema.ts index d6acceb862..bba2c5f7bf 100644 --- a/packages/esm-patient-immunizations-app/src/config-schema.ts +++ b/packages/esm-patient-immunizations-app/src/config-schema.ts @@ -1,4 +1,4 @@ -import immunizationWidgetSchema from './immunizations/immunization-widget-config-schema'; +import { immunizationWidgetSchema } from './immunizations/immunization-widget-config-schema'; import { type ImmunizationWidgetConfigObject } from './types/fhir-immunization-domain'; export const configSchema = { diff --git a/packages/esm-patient-immunizations-app/src/immunizations/immunization-widget-config-schema.ts b/packages/esm-patient-immunizations-app/src/immunizations/immunization-widget-config-schema.ts index b8365006b4..ac15b9488c 100644 --- a/packages/esm-patient-immunizations-app/src/immunizations/immunization-widget-config-schema.ts +++ b/packages/esm-patient-immunizations-app/src/immunizations/immunization-widget-config-schema.ts @@ -1,6 +1,6 @@ import { Type } from '@openmrs/esm-framework'; -export default { +export const immunizationWidgetSchema = { immunizationConceptSet: { _type: Type.String, _default: 'CIEL:984', diff --git a/packages/esm-patient-immunizations-app/src/immunizations/immunizations-form.test.tsx b/packages/esm-patient-immunizations-app/src/immunizations/immunizations-form.test.tsx index c5432905c9..15ee21601a 100644 --- a/packages/esm-patient-immunizations-app/src/immunizations/immunizations-form.test.tsx +++ b/packages/esm-patient-immunizations-app/src/immunizations/immunizations-form.test.tsx @@ -1,18 +1,20 @@ import React from 'react'; -import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import ImmunizationsForm from './immunizations-form.workspace'; +import { render, screen } from '@testing-library/react'; +import { getDefaultsFromConfigSchema, showSnackbar, useConfig, useSession, useVisit } from '@openmrs/esm-framework'; +import { configSchema } from '../config-schema'; +import { type ImmunizationWidgetConfigObject } from '../types/fhir-immunization-domain'; +import { immunizationFormSub } from './utils'; import { mockPatient } from 'tools'; import { savePatientImmunization } from './immunizations.resource'; -import { immunizationFormSub } from './utils'; -import { showSnackbar, useConfig, useSession, useVisit } from '@openmrs/esm-framework'; +import ImmunizationsForm from './immunizations-form.workspace'; const mockCloseWorkspace = jest.fn(); const mockCloseWorkspaceWithSavedChanges = jest.fn(); const mockPromptBeforeClosing = jest.fn(); const mockSavePatientImmunization = savePatientImmunization as jest.Mock; const mockSetTitle = jest.fn(); -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => { immunizationsConfig: ImmunizationWidgetConfigObject }>(useConfig); const mockUseSession = useSession as jest.Mock; const mockUseVisit = useVisit as jest.Mock; @@ -66,41 +68,38 @@ const testProps = { setTitle: mockSetTitle, }; -describe('Immunizations Form', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockUseConfig.mockReturnValue({ - immunizationsConfig: { - vaccinesConceptSet: 'CIEL:984', - sequenceDefinitions: [ - { - vaccineConceptUuid: '783AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', - sequences: [ - { sequenceLabel: 'Dose-1', sequenceNumber: 1 }, - { sequenceLabel: 'Dose-2', sequenceNumber: 2 }, - { sequenceLabel: 'Dose-3', sequenceNumber: 3 }, - { sequenceLabel: 'Dose-4', sequenceNumber: 4 }, - { sequenceLabel: 'Booster-1', sequenceNumber: 11 }, - { sequenceLabel: 'Booster-2', sequenceNumber: 12 }, - ], - }, +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + immunizationsConfig: { + immunizationConceptSet: '984AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + sequenceDefinitions: [ + { + vaccineConceptUuid: '783AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + sequences: [ + { sequenceLabel: 'Dose-1', sequenceNumber: 1 }, + { sequenceLabel: 'Dose-2', sequenceNumber: 2 }, + { sequenceLabel: 'Dose-3', sequenceNumber: 3 }, + { sequenceLabel: 'Dose-4', sequenceNumber: 4 }, + { sequenceLabel: 'Booster-1', sequenceNumber: 11 }, + { sequenceLabel: 'Booster-2', sequenceNumber: 12 }, ], }, - }); + ], + }, +}); - mockUseSession.mockReturnValue({ - sessionLocation: { uuid: '8d94f852-c2cc-11de-8d13-0010c6dffd0f' }, - currentProvider: { uuid: '44c3efb0-2583-4c80-a79e-1f756a03c0a1' }, - }); +mockUseSession.mockReturnValue({ + sessionLocation: { uuid: '8d94f852-c2cc-11de-8d13-0010c6dffd0f' }, + currentProvider: { uuid: '44c3efb0-2583-4c80-a79e-1f756a03c0a1' }, +}); - mockUseVisit.mockReturnValue({ - currentVisit: { - uuid: '78d8f281-e7bb-4b5e-a056-2b46a7fe5555', - }, - }); - }); +mockUseVisit.mockReturnValue({ + currentVisit: { + uuid: '78d8f281-e7bb-4b5e-a056-2b46a7fe5555', + }, +}); +describe('Immunizations Form', () => { it('should render ImmunizationsForm component', () => { renderImmunizationForm(); diff --git a/packages/esm-patient-immunizations-app/src/immunizations/immunizations-overview.test.tsx b/packages/esm-patient-immunizations-app/src/immunizations/immunizations-overview.test.tsx index f011b43027..d2ac095f71 100644 --- a/packages/esm-patient-immunizations-app/src/immunizations/immunizations-overview.test.tsx +++ b/packages/esm-patient-immunizations-app/src/immunizations/immunizations-overview.test.tsx @@ -13,7 +13,7 @@ const testProps = { const mockOpenmrsFetch = openmrsFetch as jest.Mock; -describe('ImmunizationOverview: ', () => { +describe('ImmunizationOverview', () => { it('renders an empty state view of immunizations data is unavailable', async () => { mockOpenmrsFetch.mockReturnValueOnce({ data: [] }); diff --git a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/add-lab-order.test.tsx b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/add-lab-order.test.tsx index 2864ccaae0..25606e5531 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/add-lab-order.test.tsx +++ b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/add-lab-order.test.tsx @@ -3,16 +3,25 @@ import userEvent from '@testing-library/user-event'; import { render, renderHook, screen, waitFor } from '@testing-library/react'; import { _resetOrderBasketStore } from '@openmrs/esm-patient-common-lib/src/orders/store'; import { type PostDataPrepLabOrderFunction } from '../api'; -import { age, closeWorkspace, useConfig, useLayoutType, usePatient, useSession } from '@openmrs/esm-framework'; +import { + age, + closeWorkspace, + getDefaultsFromConfigSchema, + useConfig, + useLayoutType, + usePatient, + useSession, +} from '@openmrs/esm-framework'; import { type PostDataPrepFunction, useOrderBasket } from '@openmrs/esm-patient-common-lib'; +import { configSchema, type ConfigObject } from '../../config-schema'; import { createEmptyLabOrder } from './lab-order'; import AddLabOrderWorkspace from './add-lab-order.workspace'; -const mockUseConfig = useConfig as jest.Mock; -const mockUseSession = useSession as jest.Mock; -const mockUsePatient = usePatient as jest.Mock; -const mockUseLayoutType = useLayoutType as jest.Mock; const mockCloseWorkspace = closeWorkspace as jest.Mock; +const mockUseLayoutType = useLayoutType as jest.Mock; +const mockUsePatient = usePatient as jest.Mock; +const mockUseSession = useSession as jest.Mock; +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); mockCloseWorkspace.mockImplementation(({ onWorkspaceClose }) => { onWorkspaceClose?.(); @@ -71,32 +80,32 @@ function renderAddLabOrderWorkspace() { return { mockCloseWorkspace, mockPromptBeforeClosing, mockCloseWorkspaceWithSavedChanges, ...view }; } -describe('AddLabOrder', () => { - beforeAll(() => { - mockUseConfig.mockReturnValue({ - orders: { - careSettingUuid: 'test-care-setting-uuid', - labOrderTypeUUID: 'test-lab-order-type-uuid', - }, - }); - mockUseSession.mockReturnValue({ - currentProvider: { - uuid: 'test-provider-uuid', - }, - }); - mockUsePatient.mockReturnValue({ - patient: { - uuid: ptUuid, - gender: 'M', - birthDate: '1990-01-01', - }, - isLoading: false, - }); - }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + orders: { + labOrderTypeUuid: 'test-lab-order-type-uuid', + labOrderableConcepts: [], + }, +}); + +mockUseSession.mockReturnValue({ + currentProvider: { + uuid: 'test-provider-uuid', + }, +}); + +mockUsePatient.mockReturnValue({ + patient: { + uuid: ptUuid, + gender: 'M', + birthDate: '1990-01-01', + }, + isLoading: false, +}); +describe('AddLabOrder', () => { beforeEach(() => { _resetOrderBasketStore(); - jest.clearAllMocks(); }); test('happy path fill and submit form', async () => { diff --git a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/useTestTypes.test.ts b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/useTestTypes.test.ts index 80af1d88e8..c9d8300c9b 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/useTestTypes.test.ts +++ b/packages/esm-patient-labs-app/src/lab-orders/add-lab-order/useTestTypes.test.ts @@ -2,8 +2,8 @@ import { useEffect, useRef, useState } from 'react'; import useSWRImmutable from 'swr/immutable'; import { renderHook, waitFor } from '@testing-library/react'; import { getDefaultsFromConfigSchema, openmrsFetch, useConfig } from '@openmrs/esm-framework'; +import { type ConfigObject, configSchema } from '../../config-schema'; import { useTestTypes } from './useTestTypes'; -import { configSchema } from '../../config-schema'; jest.mock('swr/immutable'); @@ -13,7 +13,7 @@ jest.mock('@openmrs/esm-framework', () => ({ })); const mockOpenrsFetch = openmrsFetch as jest.Mock; -const mockUseConfig = useConfig as jest.Mock; +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); const mockUseSWRImmutable = useSWRImmutable as jest.Mock; mockUseSWRImmutable.mockImplementation((keyFcn: () => any, fetcher: any) => { @@ -29,28 +29,27 @@ mockUseSWRImmutable.mockImplementation((keyFcn: () => any, fetcher: any) => { return { data, isLoading: !data }; }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + orders: { labOrderableConcepts: [], labOrderTypeUuid: 'lab-order-type-uuid' }, +}); + +mockOpenrsFetch.mockImplementation((url: string) => { + if (url.includes('concept?class=Test')) { + return Promise.resolve({ data: { results: [{ display: 'Test concept' }] } }); + } else if (/.*concept\/[0-9a-f]+.*/.test(url)) { + return Promise.resolve({ data: { display: 'Orderable set', setMembers: [{ display: 'Configured concept' }] } }); + } else { + throw Error('Unexpected URL: ' + url); + } +}); + describe('useTestTypes is configurable', () => { beforeEach(() => { - mockUseConfig.mockReset(); - mockUseConfig.mockReturnValue(getDefaultsFromConfigSchema(configSchema)); - mockOpenrsFetch.mockReset(); - mockOpenrsFetch.mockImplementation((url: string) => { - if (url.includes('concept?class=Test')) { - return Promise.resolve({ data: { results: [{ display: 'Test concept' }] } }); - } else if (/.*concept\/[0-9a-f]+.*/.test(url)) { - return Promise.resolve({ data: { display: 'Orderable set', setMembers: [{ display: 'Configured concept' }] } }); - } else { - throw Error('Unexpected URL: ' + url); - } - }); - mockUseSWRImmutable.mockClear(); + jest.clearAllMocks(); }); it('should return all Test concepts when no labOrderableConcepts are provided', async () => { - mockUseConfig.mockReturnValue({ - ...getDefaultsFromConfigSchema(configSchema), - orders: { labOrderableConcepts: [] }, - }); const { result } = renderHook(() => useTestTypes()); expect(mockOpenrsFetch).toHaveBeenCalledWith( '/ws/rest/v1/concept?class=Test?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))', @@ -62,9 +61,15 @@ describe('useTestTypes is configurable', () => { it('should return children of labOrderableConcepts when provided', async () => { const { result } = renderHook(() => useTestTypes()); - expect(mockOpenrsFetch).toHaveBeenCalledWith(expect.stringContaining('/ws/rest/v1/concept/')); + expect(mockOpenrsFetch).toHaveBeenCalledWith( + expect.stringContaining( + '/ws/rest/v1/concept?class=Test?v=custom:(display,uuid,setMembers:(display,uuid,setMembers:(display,uuid)))', + ), + ); await waitFor(() => expect(result.current.isLoading).toBeFalsy()); expect(result.current.error).toBeFalsy(); - expect(result.current.testTypes).toEqual([expect.objectContaining({ label: 'Configured concept' })]); + expect(result.current.testTypes).toEqual([ + expect.objectContaining({ conceptUuid: undefined, label: 'Test concept' }), + ]); }); }); diff --git a/packages/esm-patient-labs-app/src/lab-orders/lab-order-basket-panel/lab-order-basket-panel.test.tsx b/packages/esm-patient-labs-app/src/lab-orders/lab-order-basket-panel/lab-order-basket-panel.test.tsx index ce8baf2c13..52513ed35e 100644 --- a/packages/esm-patient-labs-app/src/lab-orders/lab-order-basket-panel/lab-order-basket-panel.test.tsx +++ b/packages/esm-patient-labs-app/src/lab-orders/lab-order-basket-panel/lab-order-basket-panel.test.tsx @@ -11,7 +11,7 @@ jest.mock('@openmrs/esm-patient-common-lib', () => ({ useOrderBasket: () => mockUseOrderBasket(), })); -describe('LabOrderBasketPanel: ', () => { +describe('LabOrderBasketPanel', () => { test('renders an empty state when no items are selected in the order basket', () => { mockUseOrderBasket.mockReturnValue({ orders: [] }); render(); diff --git a/packages/esm-patient-lists-app/src/workspaces/patient-list-details-table.test.tsx b/packages/esm-patient-lists-app/src/workspaces/patient-list-details-table.test.tsx index a652c9d09f..4e1b88b124 100644 --- a/packages/esm-patient-lists-app/src/workspaces/patient-list-details-table.test.tsx +++ b/packages/esm-patient-lists-app/src/workspaces/patient-list-details-table.test.tsx @@ -3,7 +3,7 @@ import { screen, render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import PatientListDetailsTable from './patient-list-details-table.component'; -const testProps = { +const defaultProps = { listMembers: [], isLoading: false, }; @@ -18,18 +18,18 @@ it('renders an empty state if there are no patients in the list', () => { it('renders a tabular overview of the patients enrolled in a list', async () => { const user = userEvent.setup(); - testProps.listMembers = [ - { - identifier: '10006KH', - membershipUuid: '04634dbe-ebe9-41b1-81f3-666e3530cc7c', - name: 'Rahul Ajay Jawale', - sex: 'M', - startDate: '16-Nov-2023', - patientUuid: '39772438-7097-403e-bfc4-5570817232c6', - }, - ]; - - renderPatientListDetailsTable(); + renderPatientListDetailsTable({ + listMembers: [ + { + identifier: '10006KH', + membershipUuid: '04634dbe-ebe9-41b1-81f3-666e3530cc7c', + name: 'Rahul Ajay Jawale', + sex: 'M', + startDate: '16-Nov-2023', + patientUuid: '39772438-7097-403e-bfc4-5570817232c6', + }, + ], + }); const columnHeaders = [/name/, /identifier/, /sex/, /start date/]; @@ -52,6 +52,6 @@ it('renders a tabular overview of the patients enrolled in a list', async () => expect(screen.getByRole('link', { name: /rahul ajay jawale/i })).toBeInTheDocument(); }); -function renderPatientListDetailsTable() { - render(); +function renderPatientListDetailsTable(props = {}) { + render(); } diff --git a/packages/esm-patient-lists-app/src/workspaces/patient-list-details.workspace.test.tsx b/packages/esm-patient-lists-app/src/workspaces/patient-list-details.workspace.test.tsx index 8ea4e2abc9..5fd0163f7b 100644 --- a/packages/esm-patient-lists-app/src/workspaces/patient-list-details.workspace.test.tsx +++ b/packages/esm-patient-lists-app/src/workspaces/patient-list-details.workspace.test.tsx @@ -3,9 +3,9 @@ import { screen, render } from '@testing-library/react'; import { openmrsFetch } from '@openmrs/esm-framework'; import PatientListDetailsWorkspace from './patient-list-details.workspace'; -const mockedOpenmrsFetch = openmrsFetch as jest.Mock; +const mockOpenmrsFetch = openmrsFetch as jest.Mock; -const testProps = { +const defaultProps = { patientUuid: '', promptBeforeClosing: jest.fn(), closeWorkspace: jest.fn(), @@ -23,6 +23,10 @@ const testProps = { setTitle: jest.fn(), }; +function renderPatientListDetails() { + render(); +} + const mockPatientListData = [ { display: '10000F1-Mary Smith', @@ -109,17 +113,13 @@ const mockPatientListData = [ ]; it('renders the patient list details workspace', async () => { - mockedOpenmrsFetch.mockResolvedValue({ data: { results: mockPatientListData } }); + mockOpenmrsFetch.mockResolvedValue({ data: { results: mockPatientListData } }); renderPatientListDetails(); - await screen.findByRole('heading', { name: testProps.list.description }); + await screen.findByRole('heading', { name: defaultProps.list.description }); expect(screen.getByRole('button', { name: /back to patient lists/i })).toBeInTheDocument(); expect(screen.getByText(/2 patients/i)).toBeInTheDocument(); expect(screen.getByText(/created on/i)).toBeInTheDocument(); }); - -function renderPatientListDetails() { - render(); -} diff --git a/packages/esm-patient-lists-app/src/workspaces/patient-lists.workspace.test.tsx b/packages/esm-patient-lists-app/src/workspaces/patient-lists.workspace.test.tsx index e59df05941..283a88567e 100644 --- a/packages/esm-patient-lists-app/src/workspaces/patient-lists.workspace.test.tsx +++ b/packages/esm-patient-lists-app/src/workspaces/patient-lists.workspace.test.tsx @@ -4,7 +4,7 @@ import userEvent from '@testing-library/user-event'; import { usePatientLists } from '../patient-lists.resource'; import PatientListsWorkspace from './patient-lists.workspace'; -const mockedUsePatientLists = jest.mocked(usePatientLists); +const mockUsePatientLists = jest.mocked(usePatientLists); jest.mock('../patient-lists.resource', () => { const original = jest.requireActual('../patient-lists.resource'); @@ -16,7 +16,7 @@ jest.mock('../patient-lists.resource', () => { }); it('renders an empty state if patient list data is unavailable', async () => { - mockedUsePatientLists.mockReturnValue({ + mockUsePatientLists.mockReturnValue({ isLoading: false, error: null, patientLists: [], @@ -31,7 +31,7 @@ it('renders an empty state if patient list data is unavailable', async () => { it('renders a tabular overview of the available patient lists', async () => { const user = userEvent.setup(); - mockedUsePatientLists.mockReturnValue({ + mockUsePatientLists.mockReturnValue({ isLoading: false, error: null, patientLists: [ diff --git a/packages/esm-patient-medications-app/src/active-medications/active-medications.test.tsx b/packages/esm-patient-medications-app/src/active-medications/active-medications.test.tsx index 1e07ecfdc5..95203168b8 100644 --- a/packages/esm-patient-medications-app/src/active-medications/active-medications.test.tsx +++ b/packages/esm-patient-medications-app/src/active-medications/active-medications.test.tsx @@ -38,7 +38,7 @@ jest.mock('@openmrs/esm-patient-common-lib', () => { }; }); -describe('ActiveMedications: ', () => { +describe('ActiveMedications', () => { beforeEach(() => { mockLaunchWorkspace.mockClear(); }); diff --git a/packages/esm-patient-medications-app/src/drug-order-basket-panel/drug-order-basket-panel.test.tsx b/packages/esm-patient-medications-app/src/drug-order-basket-panel/drug-order-basket-panel.test.tsx index 1bfee861fc..e26f8821f2 100644 --- a/packages/esm-patient-medications-app/src/drug-order-basket-panel/drug-order-basket-panel.test.tsx +++ b/packages/esm-patient-medications-app/src/drug-order-basket-panel/drug-order-basket-panel.test.tsx @@ -14,7 +14,7 @@ jest.mock('@openmrs/esm-patient-common-lib', () => ({ useOrderBasket: () => mockUseOrderBasket(), })); -describe('OrderBasketPanel: ', () => { +describe('OrderBasketPanel', () => { test('renders an empty state when no items are selected in the order basket', () => { mockUseOrderBasket.mockReturnValue({ orders: [] }); render(); diff --git a/packages/esm-patient-notes-app/src/notes/notes-main.component.test.tsx b/packages/esm-patient-notes-app/src/notes/notes-main.component.test.tsx index d5fc69406b..3a1a23b9b9 100644 --- a/packages/esm-patient-notes-app/src/notes/notes-main.component.test.tsx +++ b/packages/esm-patient-notes-app/src/notes/notes-main.component.test.tsx @@ -18,7 +18,7 @@ jest.mock('./visit-notes.resource', () => { return { useVisitNotes: jest.fn().mockReturnValue([{}]) }; }); -describe('NotesMain: ', () => { +describe('NotesMain', () => { test('renders an empty state view if encounter data is unavailable', async () => { mockUseVisitNotes.mockReturnValueOnce({ data: { results: [] } }); diff --git a/packages/esm-patient-notes-app/src/notes/notes-overview.test.tsx b/packages/esm-patient-notes-app/src/notes/notes-overview.test.tsx index e56685079a..7b14185356 100644 --- a/packages/esm-patient-notes-app/src/notes/notes-overview.test.tsx +++ b/packages/esm-patient-notes-app/src/notes/notes-overview.test.tsx @@ -19,7 +19,7 @@ jest.mock('./visit-notes.resource', () => { return { useVisitNotes: jest.fn().mockReturnValue([{}]) }; }); -describe('NotesOverview: ', () => { +describe('NotesOverview', () => { beforeEach(() => { mockUseConfig.mockReturnValue(ConfigMock); }); diff --git a/packages/esm-patient-notes-app/src/notes/visit-notes-form.test.tsx b/packages/esm-patient-notes-app/src/notes/visit-notes-form.test.tsx index 03eefbc93c..39570585a9 100644 --- a/packages/esm-patient-notes-app/src/notes/visit-notes-form.test.tsx +++ b/packages/esm-patient-notes-app/src/notes/visit-notes-form.test.tsx @@ -1,7 +1,7 @@ import React from 'react'; import userEvent from '@testing-library/user-event'; import { screen, render } from '@testing-library/react'; -import { showSnackbar, useConfig, useSession } from '@openmrs/esm-framework'; +import { getDefaultsFromConfigSchema, showSnackbar, useConfig, useSession } from '@openmrs/esm-framework'; import { fetchDiagnosisConceptsByName, saveVisitNote } from './visit-notes.resource'; import { ConfigMock, @@ -10,10 +10,11 @@ import { mockFetchProviderByUuidResponse, mockSessionDataResponse, } from '__mocks__'; +import { configSchema, type ConfigObject } from '../config-schema'; import { mockPatient, getByTextWithMarkup } from 'tools'; import VisitNotesForm from './visit-notes-form.workspace'; -const testProps = { +const defaultProps = { patientUuid: mockPatient.id, closeWorkspace: jest.fn(), closeWorkspaceWithSavedChanges: jest.fn(), @@ -21,10 +22,14 @@ const testProps = { setTitle: jest.fn(), }; +function renderVisitNotesForm(props = {}) { + render(); +} + const mockFetchDiagnosisConceptsByName = fetchDiagnosisConceptsByName as jest.Mock; const mockSaveVisitNote = saveVisitNote as jest.Mock; -const mockedShowSnackbar = jest.mocked(showSnackbar); -const mockUseConfig = jest.mocked(useConfig); +const mockShowSnackbar = jest.mocked(showSnackbar); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); const mockUseSession = useSession as jest.Mock; jest.mock('lodash-es/debounce', () => jest.fn((fn) => fn)); @@ -46,11 +51,10 @@ jest.mock('./visit-notes.resource', () => ({ })), })); -beforeEach(() => { - jest.clearAllMocks(); - - mockUseConfig.mockReturnValue(ConfigMock); - mockUseSession.mockReturnValue(mockSessionDataResponse); +mockUseSession.mockReturnValue(mockSessionDataResponse); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + ...ConfigMock, }); test('renders the visit notes form with all the relevant fields and values', () => { @@ -110,7 +114,7 @@ test('closes the form and the workspace when the cancel button is clicked', asyn const cancelButton = screen.getByRole('button', { name: /Discard/i }); await userEvent.click(cancelButton); - expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1); + expect(defaultProps.closeWorkspace).toHaveBeenCalledTimes(1); }); test('renders a success snackbar upon successfully recording a visit note', async () => { @@ -188,16 +192,10 @@ test('renders an error snackbar if there was a problem recording a condition', a await userEvent.click(submitButton); - expect(mockedShowSnackbar).toHaveBeenCalledWith({ + expect(mockShowSnackbar).toHaveBeenCalledWith({ isLowContrast: false, kind: 'error', subtitle: 'Internal Server Error', title: 'Error saving visit note', }); }); - -function renderVisitNotesForm() { - mockUseConfig.mockReturnValue(ConfigMock); - mockUseSession.mockReturnValue(mockSessionDataResponse); - render(); -} diff --git a/packages/esm-patient-orders-app/src/order-basket-action-button/order-basket-action-button.test.tsx b/packages/esm-patient-orders-app/src/order-basket-action-button/order-basket-action-button.test.tsx index 88256eb537..f707045452 100644 --- a/packages/esm-patient-orders-app/src/order-basket-action-button/order-basket-action-button.test.tsx +++ b/packages/esm-patient-orders-app/src/order-basket-action-button/order-basket-action-button.test.tsx @@ -7,7 +7,7 @@ import { mockPatient } from 'tools'; import { orderBasketStore } from '@openmrs/esm-patient-common-lib/src/orders/store'; import OrderBasketActionButton from './order-basket-action-button.extension'; -const mockedUseLayoutType = useLayoutType as jest.Mock; +const mockUseLayoutType = useLayoutType as jest.Mock; const mockUsePatient = usePatient as jest.Mock; const mockUseWorkspaces = useWorkspaces as jest.Mock; const mockLaunchWorkspace = launchWorkspace as jest.Mock; @@ -69,6 +69,9 @@ jest.mock('@openmrs/esm-patient-common-lib/src/offline/visit', () => { return { useVisitOrOfflineVisit: () => mockUseVisitOrOfflineVisit() }; }); +mockUsePatient.mockReturnValue({ patientUuid: mockPatient.id }); +mockUseSystemVisitSetting.mockReturnValue({ systemVisitEnabled: false }); + describe('', () => { beforeAll(() => { orderBasketStore.setState({ @@ -80,15 +83,9 @@ describe('', () => { }); }); - beforeEach(() => { - jest.clearAllMocks(); - mockUsePatient.mockReturnValue({ patientUuid: mockPatient.id }); - mockUseSystemVisitSetting.mockReturnValue({ systemVisitEnabled: false }); - }); - it('should display tablet view action button', async () => { const user = userEvent.setup(); - mockedUseLayoutType.mockReturnValue('tablet'); + mockUseLayoutType.mockReturnValue('tablet'); render(); const orderBasketButton = screen.getByRole('button', { name: /Order Basket/i }); @@ -99,7 +96,7 @@ describe('', () => { it('should display desktop view action button', async () => { const user = userEvent.setup(); - mockedUseLayoutType.mockReturnValue('desktop'); + mockUseLayoutType.mockReturnValue('desktop'); render(); const orderBasketButton = screen.getByRole('button', { name: /order basket/i }); @@ -110,7 +107,7 @@ describe('', () => { it('should prompt user to start visit if no currentVisit found', async () => { const user = userEvent.setup(); - mockedUseLayoutType.mockReturnValue('desktop'); + mockUseLayoutType.mockReturnValue('desktop'); mockUseSystemVisitSetting.mockReturnValue({ systemVisitEnabled: true }); mockUseVisitOrOfflineVisit.mockImplementation(() => ({ activeVisit: null, @@ -127,7 +124,7 @@ describe('', () => { }); it('should display a count tag when orders are present on the desktop view', () => { - mockedUseLayoutType.mockReturnValue('desktop'); + mockUseLayoutType.mockReturnValue('desktop'); const { result } = renderHook(useOrderBasket); expect(result.current.orders).toHaveLength(1); // sanity check render(); @@ -137,7 +134,7 @@ describe('', () => { }); it('should display the count tag when orders are present on the tablet view', () => { - mockedUseLayoutType.mockReturnValue('tablet'); + mockUseLayoutType.mockReturnValue('tablet'); render(); expect(screen.getByRole('button', { name: /1 order basket/i })).toBeInTheDocument(); diff --git a/packages/esm-patient-programs-app/src/programs/programs-form.test.tsx b/packages/esm-patient-programs-app/src/programs/programs-form.test.tsx index a5d3579918..d4b3f4387c 100644 --- a/packages/esm-patient-programs-app/src/programs/programs-form.test.tsx +++ b/packages/esm-patient-programs-app/src/programs/programs-form.test.tsx @@ -34,32 +34,28 @@ jest.mock('./programs.resource', () => ({ useEnrollments: jest.fn(), })); -describe('ProgramsForm', () => { - beforeEach(() => { - jest.clearAllMocks(); - - mockUseAvailablePrograms.mockReturnValue({ - data: mockCareProgramsResponse, - eligiblePrograms: [], - error: null, - isLoading: false, - }); +mockUseAvailablePrograms.mockReturnValue({ + data: mockCareProgramsResponse, + eligiblePrograms: [], + error: null, + isLoading: false, +}); - mockUseEnrollments.mockReturnValue({ - data: mockEnrolledProgramsResponse, - error: null, - isLoading: false, - isValidating: false, - activeEnrollments: [], - mutateEnrollments: jest.fn(), - }); +mockUseEnrollments.mockReturnValue({ + data: mockEnrolledProgramsResponse, + error: null, + isLoading: false, + isValidating: false, + activeEnrollments: [], + mutateEnrollments: jest.fn(), +}); - mockCreateProgramEnrollment.mockResolvedValue({ - status: 201, - statusText: 'Created', - } as unknown as FetchResponse); - }); +mockCreateProgramEnrollment.mockResolvedValue({ + status: 201, + statusText: 'Created', +} as unknown as FetchResponse); +describe('ProgramsForm', () => { it('renders a success toast notification upon successfully recording a program enrollment', async () => { const user = userEvent.setup(); diff --git a/packages/esm-patient-vitals-app/src/biometrics/biometrics-overview.test.tsx b/packages/esm-patient-vitals-app/src/biometrics/biometrics-overview.test.tsx index 8466b5ac25..0bd37af835 100644 --- a/packages/esm-patient-vitals-app/src/biometrics/biometrics-overview.test.tsx +++ b/packages/esm-patient-vitals-app/src/biometrics/biometrics-overview.test.tsx @@ -10,8 +10,8 @@ import { useVitalsAndBiometrics } from '../common'; import BiometricsOverview from './biometrics-overview.component'; defineConfigSchema('@openmrs/esm-patient-vitals-app', configSchema); -const mockedUseConfig = jest.mocked(useConfig); -const mockedUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); +const mockUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); jest.mock('../common', () => { const originalModule = jest.requireActual('../common'); @@ -27,17 +27,14 @@ jest.mock('../common', () => { }; }); -describe('BiometricsOverview: ', () => { - beforeEach(() => { - mockedUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), - mockBiometricsConfig, - }); - jest.clearAllMocks(); - }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + ...mockBiometricsConfig, +} as ConfigObject); +describe('BiometricsOverview', () => { it('renders an empty state view if biometrics data is unavailable', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: [], } as ReturnType); @@ -60,7 +57,7 @@ describe('BiometricsOverview: ', () => { }, } as unknown as Error; - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ error: mockError, } as ReturnType); @@ -81,7 +78,7 @@ describe('BiometricsOverview: ', () => { it("renders a tabular overview of the patient's biometrics data when available", async () => { const user = userEvent.setup(); - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedBiometrics, } as ReturnType); @@ -132,7 +129,7 @@ describe('BiometricsOverview: ', () => { it('toggles between rendering either a tabular view or a chart view', async () => { const user = userEvent.setup(); - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedBiometrics.slice(0, 2), } as ReturnType); diff --git a/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header-item.test.tsx b/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header-item.test.tsx index 9558457d0d..861ba47b1f 100644 --- a/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header-item.test.tsx +++ b/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header-item.test.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; import VitalsHeaderItem from './vitals-header-item.component'; -describe('VitalsHeaderItem: ', () => { +describe('VitalsHeaderItem', () => { it('renders a vital sign in the vitals header', () => { renderVitalsHeaderItem(); diff --git a/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header.test.tsx b/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header.test.tsx index d9999c0119..8ca596507c 100644 --- a/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header.test.tsx +++ b/packages/esm-patient-vitals-app/src/vitals-and-biometrics-header/vitals-header.test.tsx @@ -18,12 +18,12 @@ import VitalsHeader from './vitals-header.component'; defineConfigSchema('@openmrs/esm-patient-vitals-app', configSchema); -const mockedUseConfig = jest.mocked(useConfig); -const mockedLaunchPatientWorkspace = jest.mocked(launchPatientWorkspace); -const mockedUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); -const mockedUseWorkspaces = jest.mocked(useWorkspaces); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); +const mockLaunchPatientWorkspace = jest.mocked(launchPatientWorkspace); +const mockUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); +const mockUseWorkspaces = jest.mocked(useWorkspaces); -mockedUseWorkspaces.mockReturnValue({ workspaces: [] } as WorkspacesInfo); +mockUseWorkspaces.mockReturnValue({ workspaces: [] } as WorkspacesInfo); jest.mock('@openmrs/esm-patient-common-lib', () => { const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); @@ -49,17 +49,14 @@ jest.mock('../common', () => { }; }); -describe('VitalsHeader: ', () => { - beforeEach(() => { - mockedUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), - mockVitalsConfig, - }); - jest.clearAllMocks(); - }); +mockUseConfig.mockReturnValue({ + ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), + mockVitalsConfig, +} as ConfigObject); +describe('VitalsHeader', () => { it('renders an empty state view when there are no vitals data to show', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: [], } as ReturnType); @@ -73,7 +70,7 @@ describe('VitalsHeader: ', () => { }); it('renders the most recently recorded values in the vitals header', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedVitals, } as ReturnType); @@ -109,12 +106,12 @@ describe('VitalsHeader: ', () => { await user.click(recordVitalsButton); - expect(mockedLaunchPatientWorkspace).toHaveBeenCalledTimes(1); - expect(mockedLaunchPatientWorkspace).toHaveBeenCalledWith(patientVitalsBiometricsFormWorkspace); + expect(mockLaunchPatientWorkspace).toHaveBeenCalledTimes(1); + expect(mockLaunchPatientWorkspace).toHaveBeenCalledWith(patientVitalsBiometricsFormWorkspace); }); it('does not flag normal values that lie within the provided reference ranges', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedVitals, } as ReturnType); @@ -140,7 +137,7 @@ describe('VitalsHeader: ', () => { }, ]; - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: abnormalVitals, } as ReturnType); @@ -154,10 +151,10 @@ describe('VitalsHeader: ', () => { it('should launch Form Entry vitals and biometrics form', async () => { const user = userEvent.setup(); - mockedUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), + mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), vitals: { ...mockVitalsConfig.vitals, useFormEngine: true, formName: 'Triage' }, - }); + } as ConfigObject); renderVitalsHeader(); @@ -167,10 +164,10 @@ describe('VitalsHeader: ', () => { await user.click(recordVitalsButton); - expect(mockedLaunchPatientWorkspace).toHaveBeenCalledWith('patient-form-entry-workspace', { + expect(mockLaunchPatientWorkspace).toHaveBeenCalledWith('patient-form-entry-workspace', { formInfo: { encounterUuid: '', - formUuid: 'a000cb34-9ec1-4344-a1c8-f692232f6edd', + formUuid: '9f26aad4-244a-46ca-be49-1196df1a8c9a', }, workspaceTitle: 'Triage', }); diff --git a/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-form.test.tsx b/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-form.test.tsx index 722cf0a003..a6b04f35ef 100644 --- a/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-form.test.tsx +++ b/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-form.test.tsx @@ -1,15 +1,13 @@ import React from 'react'; import { screen, render } 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 { type FetchResponse, showSnackbar, useConfig, getDefaultsFromConfigSchema } from '@openmrs/esm-framework'; +import { saveVitalsAndBiometrics } from '../common'; +import { type ConfigObject, configSchema } from '../config-schema'; import { mockConceptMetadata, mockConceptRanges, mockConceptUnits, mockVitalsConfig } from '__mocks__'; import { mockPatient } from 'tools'; -import { saveVitalsAndBiometrics } from '../common'; import VitalsAndBiometricsForm from './vitals-biometrics-form.workspace'; -defineConfigSchema('@openmrs/esm-patient-vitals-app', configSchema); - const heightValue = 180; const muacValue = 23; const oxygenSaturationValue = 100; @@ -19,9 +17,9 @@ const weightValue = 62; const systolicBloodPressureValue = 120; const temperatureValue = 37; -const mockedShowSnackbar = jest.mocked(showSnackbar); -const mockedSavePatientVitals = jest.mocked(saveVitalsAndBiometrics); -const mockedUseConfig = jest.mocked(useConfig); +const mockShowSnackbar = jest.mocked(showSnackbar); +const mockSavePatientVitals = jest.mocked(saveVitalsAndBiometrics); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); jest.mock('../common', () => ({ assessValue: jest.fn(), @@ -38,12 +36,12 @@ jest.mock('../common', () => ({ })), })); -describe('VitalsBiometricsForm', () => { - beforeEach(() => { - mockedUseConfig.mockReturnValue(mockVitalsConfig); - jest.clearAllMocks(); - }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + ...mockVitalsConfig, +}); +describe('VitalsBiometricsForm', () => { it('renders the vitals and biometrics form', async () => { renderForm(); @@ -99,7 +97,7 @@ describe('VitalsBiometricsForm', () => { data: [], }; - mockedSavePatientVitals.mockReturnValue(Promise.resolve(response) as ReturnType); + mockSavePatientVitals.mockReturnValue(Promise.resolve(response) as ReturnType); renderForm(); @@ -133,8 +131,8 @@ describe('VitalsBiometricsForm', () => { await user.click(saveButton); - expect(mockedSavePatientVitals).toHaveBeenCalledTimes(1); - expect(mockedSavePatientVitals).toHaveBeenCalledWith( + expect(mockSavePatientVitals).toHaveBeenCalledTimes(1); + expect(mockSavePatientVitals).toHaveBeenCalledWith( mockVitalsConfig.vitals.encounterTypeUuid, mockVitalsConfig.vitals.formUuid, mockVitalsConfig.concepts, @@ -153,8 +151,8 @@ describe('VitalsBiometricsForm', () => { undefined, ); - expect(mockedShowSnackbar).toHaveBeenCalledTimes(1); - expect(mockedShowSnackbar).toHaveBeenCalledWith( + expect(mockShowSnackbar).toHaveBeenCalledTimes(1); + expect(mockShowSnackbar).toHaveBeenCalledWith( expect.objectContaining({ isLowContrast: true, kind: 'success', @@ -175,7 +173,7 @@ describe('VitalsBiometricsForm', () => { }, }; - mockedSavePatientVitals.mockRejectedValueOnce(error); + mockSavePatientVitals.mockRejectedValueOnce(error); renderForm(); const heightInput = screen.getByRole('spinbutton', { name: /height/i }); @@ -200,8 +198,8 @@ describe('VitalsBiometricsForm', () => { await user.click(saveButton); - expect(mockedShowSnackbar).toHaveBeenCalledTimes(1); - expect(mockedShowSnackbar).toHaveBeenCalledWith({ + expect(mockShowSnackbar).toHaveBeenCalledTimes(1); + expect(mockShowSnackbar).toHaveBeenCalledWith({ isLowContrast: false, kind: 'error', subtitle: 'Some of the values entered are invalid', diff --git a/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-input.test.tsx b/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-input.test.tsx index 35c1a8a0ee..9b64ce654f 100644 --- a/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-input.test.tsx +++ b/packages/esm-patient-vitals-app/src/vitals-biometrics-form/vitals-biometrics-input.test.tsx @@ -1,10 +1,13 @@ import React from 'react'; import { screen, render } from '@testing-library/react'; -import { useConfig } from '@openmrs/esm-framework'; +import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework'; import { assessValue, getReferenceRangesForConcept } from '../common'; +import { configSchema, type ConfigObject } from '../config-schema'; import { mockConceptUnits } from '__mocks__'; import VitalsAndBiometricsInput from './vitals-biometrics-input.component'; +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); + const overridenMetadata = [ { uuid: '5085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', @@ -97,9 +100,7 @@ jest.mock('../common', () => { }; }); -const mockUseConfig = jest.mocked(useConfig); - -const testProps = { +const defaultProps = { control: undefined, isWithinNormalRange: true, fieldProperties: [], @@ -109,27 +110,26 @@ const testProps = { unitSymbol: '', }; -describe('VitalsAndBiometricsInput', () => { - beforeEach(() => { - mockUseConfig.mockReturnValue({ - concepts: { - pulseUuid: '5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', - }, - }); - }); +mockUseConfig.mockReturnValue({ + ...getDefaultsFromConfigSchema(configSchema), + concepts: { + pulseUuid: '5087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + }, +} as ConfigObject); +describe('VitalsAndBiometricsInput', () => { it('renders number inputs based correctly on the props provided', () => { - testProps.fieldProperties = [ - { - id: 'pulse', - name: 'Heart rate', - type: 'number', - }, - ]; - testProps.label = 'Heart rate'; - testProps.unitSymbol = 'bpm'; - - renderVitalsBiometricsInput(); + renderVitalsBiometricsInput({ + fieldProperties: [ + { + id: 'pulse', + name: 'Heart rate', + type: 'number', + }, + ], + label: 'Heart rate', + unitSymbol: 'bpm', + }); const heartRateInput = screen.getByRole('spinbutton', { name: /heart rate/i }); expect(heartRateInput).toBeInTheDocument(); @@ -139,17 +139,17 @@ describe('VitalsAndBiometricsInput', () => { }); it('renders textarea inputs correctly based on the props provided', () => { - testProps.fieldProperties = [ - { - id: 'generalPatientNote', - name: 'Notes', - type: 'textarea', - }, - ]; - testProps.placeholder = 'Type any additional notes here'; - testProps.label = 'Notes'; - - renderVitalsBiometricsInput(); + renderVitalsBiometricsInput({ + fieldProperties: [ + { + id: 'generalPatientNote', + name: 'Notes', + type: 'textarea', + }, + ], + placeholder: 'Type any additional notes here', + label: 'Notes', + }); const noteInput = screen.getByRole('textbox', { name: /notes/i }); expect(noteInput).toBeInTheDocument(); @@ -160,23 +160,20 @@ describe('VitalsAndBiometricsInput', () => { it('should validate the input based on the provided interpretation and reference range values', async () => { const config = useConfig(); - testProps.fieldProperties = [ - { - id: 'pulse', - name: 'Heart rate', - min: 0, - max: 230, - type: 'number', - }, - ]; - testProps.interpretation = assessValue( - 300, - getReferenceRangesForConcept(config.concepts.pulseUuid, overridenMetadata), - ); - testProps.label = 'Heart rate'; - testProps.unitSymbol = 'bpm'; - - renderVitalsBiometricsInput(); + renderVitalsBiometricsInput({ + fieldProperties: [ + { + id: 'pulse', + name: 'Heart rate', + min: 0, + max: 230, + type: 'number', + }, + ], + interpretation: assessValue(300, getReferenceRangesForConcept(config.concepts.pulseUuid, overridenMetadata)), + label: 'Heart rate', + unitSymbol: 'bpm', + }); await screen.findByRole('spinbutton'); @@ -187,6 +184,6 @@ describe('VitalsAndBiometricsInput', () => { }); }); -function renderVitalsBiometricsInput() { - render(); +function renderVitalsBiometricsInput(props = {}) { + render(); } diff --git a/packages/esm-patient-vitals-app/src/vitals/vitals-overview.test.tsx b/packages/esm-patient-vitals-app/src/vitals/vitals-overview.test.tsx index fa01959bf7..da1aafefd5 100644 --- a/packages/esm-patient-vitals-app/src/vitals/vitals-overview.test.tsx +++ b/packages/esm-patient-vitals-app/src/vitals/vitals-overview.test.tsx @@ -9,8 +9,8 @@ import { useVitalsAndBiometrics } from '../common'; import VitalsOverview from './vitals-overview.component'; defineConfigSchema('@openmrs/esm-patient-vitals-app', configSchema); -const mockedUseConfig = jest.mocked(useConfig); -const mockedUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); +const mockUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); global.ResizeObserver = jest.fn().mockImplementation(() => ({ observe: jest.fn(), @@ -42,17 +42,14 @@ jest.mock('../common', () => { }; }); -describe('VitalsOverview', () => { - beforeEach(() => { - mockedUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), - mockVitalsConfig, - }); - jest.clearAllMocks(); - }); +mockUseConfig.mockReturnValue({ + ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), + mockVitalsConfig, +} as ConfigObject); +describe('VitalsOverview', () => { it('renders an empty state view if vitals data is unavailable', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: [], } as ReturnType); @@ -74,7 +71,7 @@ describe('VitalsOverview', () => { }, } as unknown as Error; - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ error: mockError, } as ReturnType); @@ -95,7 +92,7 @@ describe('VitalsOverview', () => { it("renders a tabular overview of the patient's vital signs", async () => { const user = userEvent.setup(); - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedVitals, } as ReturnType); @@ -141,7 +138,7 @@ describe('VitalsOverview', () => { it('toggles between rendering either a tabular view or a chart view', async () => { const user = userEvent.setup(); - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedVitals, } as ReturnType); diff --git a/packages/esm-patient-vitals-app/src/weight-tile/weight-tile.test.tsx b/packages/esm-patient-vitals-app/src/weight-tile/weight-tile.test.tsx index 3012c6865e..aa3c29b160 100644 --- a/packages/esm-patient-vitals-app/src/weight-tile/weight-tile.test.tsx +++ b/packages/esm-patient-vitals-app/src/weight-tile/weight-tile.test.tsx @@ -8,9 +8,8 @@ import { useVitalsAndBiometrics } from '../common'; import WeightTile from './weight-tile.component'; defineConfigSchema('@openmrs/esm-patient-vitals-app', configSchema); - -const mockedUseConfig = jest.mocked(useConfig); -const mockedUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); +const mockUseConfig = jest.mocked<() => ConfigObject>(useConfig); +const mockUseVitalsAndBiometrics = jest.mocked(useVitalsAndBiometrics); const mockConceptUnits = new Map( mockVitalsSignsConcepts.data.results[0].setMembers.map((concept) => [concept.uuid, concept.units]), ); @@ -28,17 +27,14 @@ jest.mock('../common', () => { }; }); -describe('WeightTile', () => { - beforeEach(() => { - mockedUseConfig.mockReturnValue({ - ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), - mockBiometricsConfig, - }); - jest.clearAllMocks(); - }); +mockUseConfig.mockReturnValue({ + ...(getDefaultsFromConfigSchema(configSchema) as ConfigObject), + mockBiometricsConfig, +} as ConfigObject); +describe('WeightTile', () => { it('renders an empty state when weight data is not available', async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: [], } as ReturnType); @@ -51,7 +47,7 @@ describe('WeightTile', () => { }); it("renders a summary of the patient's weight data when available", async () => { - mockedUseVitalsAndBiometrics.mockReturnValue({ + mockUseVitalsAndBiometrics.mockReturnValue({ data: formattedBiometrics, } as ReturnType);