Skip to content

Commit

Permalink
(test) Fix tests (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen authored Nov 28, 2023
1 parent 82eb79d commit 98c3cfa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { launchPatientWorkspace, launchStartVisitPrompt } from '@openmrs/esm-pat
import { useVisit } from '@openmrs/esm-framework';
import { PatientCarePrograms } from '../hooks/useCarePrograms';

jest.mock('../hooks/useCarePrograms');

const mockUseVisit = useVisit as jest.Mock;

const testProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import PatientSummary from './patient-summary.component';
import { useConfig } from '@openmrs/esm-framework';
import { usePatientSummary } from '../hooks/usePatientSummary';
import { mockPatient } from '../../../../__mocks__/patient-summary.mock';
import dayjs from 'dayjs';

const mockUseConfig = useConfig as jest.Mock;
jest.mock('../hooks/usePatientSummary');

const mockedUseConfig = useConfig as jest.Mock;
const mockedUsePatientSummary = usePatientSummary as jest.Mock;

jest.mock('@openmrs/esm-framework', () => {
return {
Expand All @@ -16,20 +20,24 @@ jest.mock('@openmrs/esm-framework', () => {
});

describe('PatientSummary', () => {
it('renders skeleton loader when loading', () => {
jest.spyOn(require('../hooks/usePatientSummary'), 'usePatientSummary').mockReturnValue({
beforeEach(() => {
mockedUsePatientSummary.mockReturnValue({
data: null,
isError: false,
isLoading: true,
});
});

afterEach(() => mockedUsePatientSummary.mockReset());

it('renders a skeleton loader when loading', () => {
render(<PatientSummary patientUuid={mockPatient.uuid} />);
const skeletonLoader = screen.getByRole('progressbar');
expect(skeletonLoader).toBeInTheDocument();
});

it('renders error message when data retrieval fails', () => {
jest.spyOn(require('../hooks/usePatientSummary'), 'usePatientSummary').mockReturnValue({
it('renders an error message when data retrieval fails', () => {
mockedUsePatientSummary.mockReturnValue({
data: null,
isError: true,
isLoading: false,
Expand All @@ -41,7 +49,7 @@ describe('PatientSummary', () => {
});

it('renders patient summary data when loaded', () => {
jest.spyOn(require('../hooks/usePatientSummary'), 'usePatientSummary').mockReturnValueOnce({
mockedUsePatientSummary.mockReturnValue({
data: mockPatient,
isError: false,
isLoading: false,
Expand Down Expand Up @@ -69,16 +77,17 @@ describe('PatientSummary', () => {
});

it('triggers print when print button is clicked', async () => {
jest.spyOn(require('../hooks/usePatientSummary'), 'usePatientSummary').mockReturnValueOnce({
mockedUsePatientSummary.mockReturnValue({
data: mockPatient,
isError: false,
isLoading: false,
});

const printFunction = jest.fn();
const useReactToPrintSpy = jest.spyOn(require('react-to-print'), 'useReactToPrint');

useReactToPrintSpy.mockReturnValue(printFunction);
mockUseConfig.mockReturnValue({ logo: {} });
mockedUseConfig.mockReturnValue({ logo: {} });

render(<PatientSummary patientUuid={mockPatient.uuid} />);
const printButton = screen.getByText('Print', { exact: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { useRegimenHistory } from '../hooks/useRegimenHistory';
import RegimenHistory, { RegimenHistoryProps } from './regimen-history.component';

jest.mock('../hooks/useRegimenHistory');

const mockedUseRegimenHistory = jest.mocked(useRegimenHistory);

describe('RegimenHistory Component', () => {
const mockProps: RegimenHistoryProps = {
patientUuid: 'patient-123',
category: 'HIV Program',
};

const mockData = [
{
regimenShortDisplay: 'ShortDisplay',
regimenLine: 'Line1',
changeReasons: '[Reason1]',
changeReasons: ['Reason1'],
regimenUuid: 'regimen-123',
startDate: '2021-01-01',
endDate: '',
regimenLongDisplay: 'LongDisplay',
current: false,
},
];
it('renders without crashing', () => {
jest.spyOn(require('../hooks/useRegimenHistory'), 'useRegimenHistory').mockReturnValue({

beforeEach(() => {
mockedUseRegimenHistory.mockReturnValue({
regimen: mockData,
isLoading: false,
error: false,
});
render(<RegimenHistory {...mockProps} />);
expect(screen.getByText('Regimen History')).toBeInTheDocument();
});

it('displays regimen history details correctly', () => {
jest.spyOn(require('../hooks/useRegimenHistory'), 'useRegimenHistory').mockReturnValue({
regimen: mockData,
isLoading: false,
error: false,
});
render(<RegimenHistory {...mockProps} />);

expect(screen.getByText(mockData[0].regimenLine)).toBeInTheDocument();
expect(screen.getByText(mockData[0].regimenShortDisplay)).toBeInTheDocument();
});
Expand Down

0 comments on commit 98c3cfa

Please sign in to comment.