Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen committed Jul 24, 2024
1 parent cc48f42 commit 8b49498
Show file tree
Hide file tree
Showing 57 changed files with 608 additions and 667 deletions.
10 changes: 7 additions & 3 deletions __mocks__/vitals-and-biometrics.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
},
};

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');

module.exports = {
clearMocks: true,
transform: {
'^.+\\.(j|t)sx?$': '@swc/jest',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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 All @@ -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',
Expand All @@ -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);

Expand All @@ -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];

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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');

Expand All @@ -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';
Expand Down Expand Up @@ -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];
Expand All @@ -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: {
Expand All @@ -254,7 +249,6 @@ describe('AllergyForm ', () => {

renderAllergyForm();

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

const allergen = mockAllergens[0];
Expand All @@ -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);

Expand Down Expand Up @@ -317,8 +312,8 @@ describe('AllergyForm ', () => {
});
});

function renderAllergyForm() {
const testProps = {
function renderAllergyForm(props = {}) {
const defaultProps = {
closeWorkspace: () => {},
closeWorkspaceWithSavedChanges: () => {},
promptBeforeClosing: () => {},
Expand All @@ -329,20 +324,5 @@ function renderAllergyForm() {
setTitle: jest.fn(),
};

render(<AllergyForm {...testProps} />);
}

function renderEditAllergyForm() {
const testProps = {
closeWorkspace: () => {},
closeWorkspaceWithSavedChanges: () => {},
promptBeforeClosing: () => {},
allergy: mockAllergy,
formContext: 'editing' as 'creating' | 'editing',
patient: mockPatient,
patientUuid: mockPatient.id,
setTitle: jest.fn(),
};

render(<AllergyForm {...testProps} />);
render(<AllergyForm {...defaultProps} {...props} />);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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,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();

Expand All @@ -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();

Expand All @@ -66,5 +64,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
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 8b49498

Please sign in to comment.