|
| 1 | +import React from 'react'; |
| 2 | +import { openmrsFetch, useSession } from '@openmrs/esm-framework'; |
| 3 | +import { screen, within } from '@testing-library/react'; |
| 4 | +import { mockPatientDrugOrdersApiData, mockSessionDataResponse } from '__mocks__'; |
| 5 | +import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools'; |
| 6 | +import PastMedications from './past-medications.component'; |
| 7 | + |
| 8 | +const mockUseSession = jest.mocked(useSession); |
| 9 | +const mockOpenmrsFetch = openmrsFetch as jest.Mock; |
| 10 | + |
| 11 | +mockUseSession.mockReturnValue(mockSessionDataResponse.data); |
| 12 | + |
| 13 | +describe('PastMedications', () => { |
| 14 | + test('renders an empty state view when there are no past medications to display', async () => { |
| 15 | + mockOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } }); |
| 16 | + mockOpenmrsFetch.mockReturnValueOnce({ data: { results: [] } }); |
| 17 | + |
| 18 | + renderWithSwr(<PastMedications patient={mockPatient} />); |
| 19 | + |
| 20 | + await waitForLoadingToFinish(); |
| 21 | + |
| 22 | + expect(screen.getByRole('heading', { name: /past medications/i })).toBeInTheDocument(); |
| 23 | + expect(screen.getByTitle(/empty data illustration/i)).toBeInTheDocument(); |
| 24 | + expect(screen.getByText(/There are no past medications to display for this patient/i)).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('renders an error state view if there is a problem fetching medications data', async () => { |
| 28 | + const error = { |
| 29 | + message: 'You are not logged in', |
| 30 | + response: { |
| 31 | + status: 401, |
| 32 | + statusText: 'Unauthorized', |
| 33 | + }, |
| 34 | + }; |
| 35 | + |
| 36 | + mockOpenmrsFetch.mockRejectedValueOnce(error); |
| 37 | + |
| 38 | + renderWithSwr(<PastMedications patient={mockPatient} />); |
| 39 | + |
| 40 | + await waitForLoadingToFinish(); |
| 41 | + |
| 42 | + expect(screen.queryByRole('table')).not.toBeInTheDocument(); |
| 43 | + expect(screen.getByRole('heading', { name: /past medications/i })).toBeInTheDocument(); |
| 44 | + expect(screen.getByText(/Error 401: Unauthorized/i)).toBeInTheDocument(); |
| 45 | + expect(screen.getByText(/Sorry, there was a problem displaying this information/i)).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('renders a tabular overview of the past medications recorded for a patient', async () => { |
| 49 | + mockOpenmrsFetch |
| 50 | + .mockReturnValueOnce({ |
| 51 | + data: { results: mockPatientDrugOrdersApiData }, |
| 52 | + }) |
| 53 | + .mockReturnValueOnce({ |
| 54 | + data: { results: [] }, // simulate no active orders so all become "past" |
| 55 | + }); |
| 56 | + |
| 57 | + renderWithSwr(<PastMedications patient={mockPatient} />); |
| 58 | + |
| 59 | + await waitForLoadingToFinish(); |
| 60 | + |
| 61 | + const headingElements = screen.getAllByText(/Past Medications/i); |
| 62 | + headingElements.forEach((headingElement) => { |
| 63 | + expect(headingElement).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + const table = screen.getByRole('table'); |
| 67 | + expect(table).toBeInTheDocument(); |
| 68 | + |
| 69 | + const expectedColumnHeaders = [/start date/i, /details/i]; |
| 70 | + expectedColumnHeaders.forEach((header) => { |
| 71 | + expect(screen.getByRole('columnheader', { name: header })).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + const expectedTableRows = [ |
| 75 | + /14-Aug-2023 Admin User Acetaminophen 325 mg — 325mg — tablet DOSE 2 tablet — oral — twice daily — indefinite duration — take it sometimes INDICATION Bad boo-boo/i, |
| 76 | + /14-Aug-2023 Admin User Acetaminophen 325 mg — 325mg — tablet 14-Aug-2023 DOSE 2 tablet — oral — twice daily — indefinite duration INDICATION No good 0/i, |
| 77 | + /14-Aug-2023 Admin User Sulfacetamide 0.1 — 10% DOSE 1 application — for 1 weeks — REFILLS 1 — apply it INDICATION Pain — QUANTITY 8 Application/i, |
| 78 | + /14-Aug-2023 Admin User Aspirin 162.5mg — 162.5mg — tablet DOSE 1 tablet — oral — once daily — for 30 days INDICATION Heart — QUANTITY 30 Tablet/i, |
| 79 | + ]; |
| 80 | + |
| 81 | + expectedTableRows.forEach((row) => { |
| 82 | + expect(within(table).getByRole('row', { name: row })).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments