Skip to content

Commit

Permalink
OHRI 2018
Browse files Browse the repository at this point in the history
Added unit tests for patientStatus banner tag in commonsLib.
  • Loading branch information
kirwea committed Jan 16, 2024
1 parent 754ce6d commit aa50af2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { render, act, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { PatientStatusBannerTag } from './patient-status-tag.component';
import { isPatientHivPositive } from './patientHivStatus';

const mockIsPatientHivPositive = isPatientHivPositive as jest.Mock;
jest.mock('./patientHivStatus');

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

const hivPositiveSampleUuid = '703AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

describe('PatientStatusBannerTag', () => {
it('renders red tag when patient is HIV positive', async () => {
mockIsPatientHivPositive.mockResolvedValue(true);
await act(async () => {
render(<PatientStatusBannerTag patientUuid={hivPositiveSampleUuid} />);
});

expect(screen.getByText(/HIV Positive/i)).toBeInTheDocument();
});
});

it('does not render red tag when patient is not HIV positive', async () => {
await act(async () => {
(isPatientHivPositive as jest.Mock).mockResolvedValue(false);
render(<PatientStatusBannerTag patientUuid="sampleUuid" />);
});

expect(screen.queryByText('HIV Positive')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import React from 'react';
import { render, act, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { PatientStatusBannerTag } from './patient-status-tag.component';
import React, { useEffect, useState } from 'react';
import { Tag } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { isPatientHivPositive } from './patientHivStatus';

const mockIsPatientHivPositive = isPatientHivPositive as jest.Mock;
jest.mock('./patientHivStatus');
export function PatientStatusBannerTag({ patientUuid }) {
const { t } = useTranslation();
const [hivPositive, setHivPositive] = useState(false);

describe('PatientStatusBannerTag', () => {
beforeEach(() => {
jest.clearAllMocks();
});
useEffect(() => {
isPatientHivPositive(patientUuid).then((result) => setHivPositive(result));
}, [hivPositive, patientUuid]);

const hivPositiveSampleUuid = '703AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
//TODO: Improve refresh time
// forceRerender();

describe('PatientStatusBannerTag', () => {
it('renders red tag when patient is HIV positive', async () => {
mockIsPatientHivPositive.mockResolvedValue(true);
await act(async () => {
render(<PatientStatusBannerTag patientUuid={hivPositiveSampleUuid} />);
});

expect(screen.getByText(/HIV Positive/i)).toBeInTheDocument();
});
});

it('does not render red tag when patient is not HIV positive', async () => {
await act(async () => {
(isPatientHivPositive as jest.Mock).mockResolvedValue(false);
render(<PatientStatusBannerTag patientUuid="sampleUuid" />);
});

expect(screen.queryByText('HIV Positive')).not.toBeInTheDocument();
});
});
return <>{hivPositive && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>;
}

0 comments on commit aa50af2

Please sign in to comment.