Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen committed Jul 23, 2024
1 parent cc48f42 commit cb434e8
Show file tree
Hide file tree
Showing 37 changed files with 491 additions and 422 deletions.
4 changes: 2 additions & 2 deletions __mocks__/vitals-and-biometrics.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5985,8 +5985,8 @@ export const mockFhirVitalsResponse = {
};

export const mockBiometricsConfig = {
biometrics: { bmiUnit: 'kg / m²' },
concepts: { heightUuid: '5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', weightUuid: '5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' },
biometrics: { bmiUnit: 'kg / m²' },
};

export const mockVitalsConfig = {
Expand All @@ -6005,7 +6005,7 @@ export const mockVitalsConfig = {
},
vitals: {
encounterTypeUuid: '67a71486-1a54-468f-ac3e-7091a9a79584',
formUuid: 'a000cb34-9ec1-4344-a1c8-f692232f6edd',
formUuid: '9f26aad4-244a-46ca-be49-1196df1a8c9a',
useFormEngine: false,
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<Promise<FetchResponse>>;
Expand Down Expand Up @@ -55,19 +56,21 @@ mockUseAllergicReactions.mockReturnValue({
allergicReactions: mockAllergicReactions,
});

describe('AllergyForm ', () => {
mockUseConfig.mockReturnValue({
...getDefaultsFromConfigSchema(configSchema),
concepts: mockConcepts,
});

describe('AllergyForm', () => {
beforeEach(() => {
jest.clearAllMocks();

mockUseConfig.mockReturnValue({
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);

Expand All @@ -92,9 +95,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];

Expand Down Expand Up @@ -124,10 +128,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];
Expand Down Expand Up @@ -157,9 +161,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');

Expand All @@ -176,10 +181,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';
Expand Down Expand Up @@ -213,12 +218,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];
Expand All @@ -242,8 +245,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: {
Expand All @@ -254,7 +257,6 @@ describe('AllergyForm ', () => {

renderAllergyForm();

const user = userEvent.setup();
const allergenInput = screen.getByPlaceholderText(/select the allergen/i);

const allergen = mockAllergens[0];
Expand All @@ -277,10 +279,10 @@ describe('AllergyForm ', () => {
});
});
it('Edit Allergy should call the saveAllergy function with updated payload', async () => {
mockSaveAllergy.mockClear();
const user = userEvent.setup();

renderEditAllergyForm();

const user = userEvent.setup();
const allergenInput = screen.getByPlaceholderText(/select the allergen/i);
const commentInput = screen.getByLabelText(/Date of onset and comments/i);

Expand Down
1 change: 1 addition & 0 deletions packages/esm-patient-allergies-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface AllergiesConfigObject {
otherConceptUuid: string;
};
}

export const configSchema = {
concepts: {
drugAllergenUuid: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -19,13 +20,14 @@ jest.mock('react-to-print', () => {
};
});

mockUseConfig.mockReturnValue({
...getDefaultsFromConfigSchema(configSchema),
printIdentifierStickerFields: ['name', 'identifier', 'age', 'dateOfBirth', 'gender'],
});

describe('PrintIdentifierSticker', () => {
beforeEach(() => {
jest.clearAllMocks();

mockedUseConfig.mockReturnValue({
printIdentifierStickerFields: ['name', 'identifier', 'age', 'dateOfBirth', 'gender'],
});
});

test('renders the component', () => {
Expand All @@ -46,12 +48,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();

Expand All @@ -66,5 +68,5 @@ describe('PrintIdentifierSticker', () => {
});

function renderPrintIdentifierSticker() {
render(<PrintIdentifierSticker patient={mockPatient} closeModal={mockedCloseModal} />);
render(<PrintIdentifierSticker patient={mockPatient} closeModal={mockCloseModal} />);
}
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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(<StartVisitOverflowMenuItem patientUuid="some-patient-uuid" />);
mockUsePatient.mockReturnValue({
error: null,
isLoading: false,
patient: mockPatient,
patientUuid: mockPatient.id,
});

const startVisitButton = screen.getByRole('menuitem', { name: /Start visit/ });
render(<StartVisitOverflowMenuItem patientUuid={mockPatient.id} />);

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(<StartVisitOverflowMenuItem patientUuid="some-patient-uuid" />);
render(<StartVisitOverflowMenuItem patientUuid={mockPatient.id} />);

const startVisitButton = screen.queryByRole('menuitem', { name: /Start visit/ });
const startVisitButton = screen.queryByRole('menuitem', { name: /start visit/i });
expect(startVisitButton).not.toBeInTheDocument();
});
});
33 changes: 17 additions & 16 deletions packages/esm-patient-chart-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}>;
}
Loading

0 comments on commit cb434e8

Please sign in to comment.