Skip to content

Commit

Permalink
Type openmrsFetch mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen committed Jul 27, 2024
1 parent f5f791c commit 465f6a6
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { openmrsFetch } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { renderWithSwr, waitForLoadingToFinish } from 'tools';
import { mockAppointmentsData } from '__mocks__';
import AppointmentTabs from './appointment-tabs.component';

const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockOpenmrsFetch = jest.mocked(openmrsFetch);

describe('AppointmentTabs', () => {
xit(`renders tabs showing different appointment lists`, async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: mockAppointmentsData.data });
mockOpenmrsFetch.mockResolvedValue({ ...mockAppointmentsData } as unknown as FetchResponse);

renderWithSwr(<AppointmentTabs appointmentServiceType="" />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const AppointmentDetails: React.FC<AppointmentDetailsProps> = ({ appointment })
)}
{patient && patient?.telecom
? patient.telecom.map((contact, i) => (
<div className={styles.labelContainer}>
<div key={i} className={styles.labelContainer}>
<p className={styles.labelBold}>{t('Contact', 'Contact {{index}}', { index: i + 1 })}: </p>
<p className={styles.label}>{contact.value}</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
useMutateAppointments,
} from './appointments-form.resource';
import { useProviders } from '../hooks/useProviders';
import Workload from '../workload/workload.component';
import type { Appointment, AppointmentPayload, RecurringPattern } from '../types';
import { type ConfigObject } from '../config-schema';
import {
Expand All @@ -54,9 +53,9 @@ import {
moduleName,
weekDays,
} from '../constants';
import styles from './appointments-form.scss';
import SelectedDateContext from '../hooks/selectedDateContext';
import uniqBy from 'lodash/uniqBy';
import Workload from '../workload/workload.component';
import styles from './appointments-form.scss';

const time12HourFormatRegexPattern = '^(1[0-2]|0?[1-9]):[0-5][0-9]$';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const defaultProps = {
};

const mockCreateAppointment = jest.mocked(saveAppointment);
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockOpenmrsFetch = jest.mocked(openmrsFetch);
const mockUseConfig = jest.mocked(useConfig<ConfigObject>);
const mockUseLocations = jest.mocked(useLocations);
const mockUseSession = jest.mocked(useSession);
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('AppointmentForm', () => {
});

it('renders the appointments form showing all the relevant fields and values', async () => {
mockOpenmrsFetch.mockReturnValue(mockUseAppointmentServiceData);
mockOpenmrsFetch.mockResolvedValue(mockUseAppointmentServiceData as unknown as FetchResponse);

renderWithSwr(<AppointmentForm {...defaultProps} />);

Expand Down Expand Up @@ -137,7 +137,7 @@ describe('AppointmentForm', () => {
it('closes the form and the workspace when the cancel button is clicked', async () => {
const user = userEvent.setup();

mockOpenmrsFetch.mockReturnValueOnce(mockUseAppointmentServiceData);
mockOpenmrsFetch.mockResolvedValueOnce(mockUseAppointmentServiceData as unknown as FetchResponse);

renderWithSwr(<AppointmentForm {...defaultProps} />);

Expand All @@ -152,7 +152,7 @@ describe('AppointmentForm', () => {
it('renders a success snackbar upon successfully scheduling an appointment', async () => {
const user = userEvent.setup();

mockOpenmrsFetch.mockReturnValue({ data: mockUseAppointmentServiceData });
mockOpenmrsFetch.mockResolvedValue({ data: mockUseAppointmentServiceData } as unknown as FetchResponse);
mockCreateAppointment.mockResolvedValue({ status: 200, statusText: 'Ok' } as FetchResponse);

renderWithSwr(<AppointmentForm {...defaultProps} />);
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('AppointmentForm', () => {
},
};

mockOpenmrsFetch.mockReturnValueOnce({ data: mockUseAppointmentServiceData });
mockOpenmrsFetch.mockResolvedValueOnce({ data: mockUseAppointmentServiceData } as unknown as FetchResponse);
mockCreateAppointment.mockRejectedValueOnce(error);

renderWithSwr(<AppointmentForm {...defaultProps} />);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { openmrsFetch } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { mockAppointmentMetrics, mockProvidersCount, mockStartTime } from '__mocks__';
import AppointmentsMetrics from './appointments-metrics.component';

const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockOpenmrsFetch = jest.mocked(openmrsFetch);

jest.mock('../hooks/useClinicalMetrics', () => ({
...jest.requireActual('../hooks/useClinicalMetrics'),
Expand All @@ -28,7 +28,9 @@ jest.mock('../hooks/useClinicalMetrics', () => ({

describe('Appointment metrics', () => {
it('renders metrics from the appointments list', async () => {
mockOpenmrsFetch.mockResolvedValue({ data: [] });
mockOpenmrsFetch.mockResolvedValueOnce({
data: [],
} as unknown as FetchResponse);

render(<AppointmentsMetrics appointmentServiceType="consultation-service-uuid" />);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { openmrsFetch } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { mockAppointmentsData } from '__mocks__';
import { mockPatient, patientChartBasePath, renderWithSwr, waitForLoadingToFinish } from 'tools';
import { type AppointmentsFetchResponse } from '../types';
import AppointmentsBase from './patient-appointments-base.component';

const testProps = {
basePath: patientChartBasePath,
patientUuid: mockPatient.id,
};

const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockOpenmrsFetch = jest.mocked(openmrsFetch);

describe('AppointmensOverview', () => {
it('renders an empty state if appointments data is unavailable', async () => {
mockOpenmrsFetch.mockReturnValueOnce({ data: [] });
mockOpenmrsFetch.mockResolvedValueOnce({
data: [],
} as unknown as FetchResponse<AppointmentsFetchResponse>);

renderWithSwr(<AppointmentsBase {...testProps} />);

await waitForLoadingToFinish();

expect(screen.getByRole('heading', { name: /appointments/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /add/i })).toBeInTheDocument();
expect(screen.getByText(/there are no upcoming appointments to display for this patient/i)).toBeInTheDocument();
});

it('renders an error state if there was a problem fetching appointments data', async () => {
Expand Down Expand Up @@ -51,7 +55,9 @@ describe('AppointmensOverview', () => {
it(`renders a tabular overview of the patient's appointment schedule if available`, async () => {
const user = userEvent.setup();

mockOpenmrsFetch.mockReturnValueOnce(mockAppointmentsData);
mockOpenmrsFetch.mockResolvedValueOnce({
...mockAppointmentsData,
} as unknown as FetchResponse<AppointmentsFetchResponse>);

renderWithSwr(<AppointmentsBase {...testProps} />);

Expand Down
4 changes: 4 additions & 0 deletions packages/esm-appointments-app/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface Appointment {
export interface AppointmentsFetchResponse {
data: Array<Appointment>;
}

export interface AppointmentService {
appointmentServiceId: number;
creatorName: string;
Expand Down Expand Up @@ -133,6 +134,7 @@ export interface AppointmentPayload {
uuid?: string;
providerUuid?: string | OpenmrsResource;
}

export interface AppointmentCountMap {
allAppointmentsCount: number;
missedAppointmentsCount;
Expand All @@ -144,6 +146,7 @@ export interface AppointmentSummary {
appointmentService: OpenmrsResource;
appointmentCountMap: Record<string, AppointmentCountMap>;
}

export interface Provider {
uuid: string;
display: string;
Expand All @@ -158,6 +161,7 @@ export enum DurationPeriod {
weekly,
daily,
}

export interface Identifier {
identifier: string;
identifierName?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { useLocation } from 'react-router-dom';
import { openmrsFetch, restBaseUrl, useSession } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, restBaseUrl, useSession } from '@openmrs/esm-framework';
import { mockSession } from '__mocks__';
import ListsDashboard from './lists-dashboard.component';

const mockOpenmrsFetch = jest.mocked(openmrsFetch);
const mockUseLocation = jest.mocked(useLocation);
const mockUseSession = jest.mocked(useSession);
const mockOpenmrsFetch = openmrsFetch as jest.Mock;

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('ListsDashboard', () => {
},
],
},
});
} as unknown as FetchResponse);
});

it('renders the patient list page UI correctly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const CompactPatientBanner = forwardRef<HTMLDivElement, CompactPatientBannerProp
{getGender(patient.gender)} <span className={styles.middot}>&middot;</span> {age(patient.birthDate)}
<span className={styles.middot}>&middot;</span>
{patientIdentifiers.map((identifier) => (
<IdentifierTag identifier={identifier} />
<IdentifierTag key={identifier.uuid} identifier={identifier} />
))}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mockMetrics, mockServiceTypes, mockLocations, mockSession } from '__moc
import { type ConfigObject, configSchema } from '../config-schema';
import ClinicMetrics from './clinic-metrics.component';

const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockOpenmrsFetch = jest.mocked(openmrsFetch);
const mockUseConfig = jest.mocked(useConfig<ConfigObject>);
const mockUseLocations = jest.mocked(useLocations);
const mockUseSession = jest.mocked(useSession);
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Clinic metrics', () => {
visitQueueNumberAttributeUuid: 'c61ce16f-272a-41e7-9924-4c555d0932c5',
});

mockOpenmrsFetch.mockReturnValue({ data: mockMetrics } as unknown as FetchResponse);
mockOpenmrsFetch.mockResolvedValue({ data: mockMetrics } as unknown as FetchResponse);

render(<ClinicMetrics />);

Expand Down
Loading

0 comments on commit 465f6a6

Please sign in to comment.