-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for end and start visit components
- Loading branch information
1 parent
8735261
commit 2e8bd49
Showing
9 changed files
with
191 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/esm-patient-chart-app/src/visit/visit-prompt/end-visit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react'; | ||
import EndVisit from './end-visit.component'; | ||
import { screen, render, waitFor } from '@testing-library/react'; | ||
import { showNotification, showToast, updateVisit, useVisit } from '@openmrs/esm-framework'; | ||
import { mockCurrentVisit } from '../../../../../__mocks__/visits.mock'; | ||
import * as mockUseVisitDialog from '../useVisitDialog'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { of, throwError } from 'rxjs'; | ||
|
||
const endVisitPayload = { | ||
location: '6351fcf4-e311-4a19-90f9-35667d99a8af', | ||
startDatetime: new Date('2021-03-16T08:16:00.000Z'), | ||
stopDatetime: expect.anything(), | ||
visitType: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed', | ||
}; | ||
|
||
const mockUseVisit = useVisit as jest.Mock; | ||
const mockUpdateVisit = updateVisit as jest.Mock; | ||
const mockShowToast = showToast as jest.Mock; | ||
const mockShowNotification = showNotification as jest.Mock; | ||
const mockMutate = jest.fn(); | ||
|
||
jest.mock('@openmrs/esm-framework', () => { | ||
const originalModule = jest.requireActual('@openmrs/esm-framework'); | ||
return { | ||
...originalModule, | ||
showToast: jest.fn(), | ||
showNotification: jest.fn(), | ||
updateVisit: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('EndVisit', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should end an active visit and display toast message', async () => { | ||
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'end' }); | ||
mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: mockMutate }); | ||
mockUpdateVisit.mockReturnValueOnce(of({ status: 200 })); | ||
render(<EndVisit patientUuid="some-patient-uuid" />); | ||
|
||
expect(screen.getByRole('heading', { name: /End active visit/ })).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('Ending this visit, will not allow you to fill another encounter form for this patient'), | ||
).toBeInTheDocument(); | ||
|
||
const endVisitButton = await screen.findByRole('button', { name: /End Visit/i }); | ||
expect(endVisitButton).toBeInTheDocument(); | ||
|
||
userEvent.click(endVisitButton); | ||
|
||
expect(updateVisit).toHaveBeenCalledWith( | ||
'17f512b4-d264-4113-a6fe-160cb38cb46e', | ||
endVisitPayload, | ||
expect.anything(), | ||
); | ||
|
||
expect(mockShowToast).toHaveBeenCalledWith({ description: 'Ended active visit successfully', kind: 'success' }); | ||
}); | ||
|
||
test('should display error message when rest api call to end visit fails', async () => { | ||
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'end' }); | ||
mockUseVisit.mockReturnValue({ currentVisit: mockCurrentVisit, mutate: mockMutate }); | ||
mockUpdateVisit.mockReturnValueOnce(throwError(new Error('Internal error message'))); | ||
render(<EndVisit patientUuid="some-patient-uuid" />); | ||
|
||
expect(screen.getByRole('heading', { name: /End active visit/ })).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('Ending this visit, will not allow you to fill another encounter form for this patient'), | ||
).toBeInTheDocument(); | ||
|
||
const endVisitButton = await screen.findByRole('button', { name: /End Visit/i }); | ||
expect(endVisitButton).toBeInTheDocument(); | ||
|
||
userEvent.click(endVisitButton); | ||
|
||
expect(updateVisit).toHaveBeenCalledWith( | ||
'17f512b4-d264-4113-a6fe-160cb38cb46e', | ||
endVisitPayload, | ||
expect.anything(), | ||
); | ||
|
||
expect(mockShowNotification).toHaveBeenCalledWith({ | ||
description: 'Internal error message', | ||
kind: 'error', | ||
title: 'Error ending active visit', | ||
critical: true, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/esm-patient-chart-app/src/visit/visit-prompt/start-visit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import StartVisit from './start-visit.component'; | ||
import { render, screen } from '@testing-library/react'; | ||
import * as mockUseVisitDialog from '../useVisitDialog'; | ||
import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
jest.mock('@openmrs/esm-patient-common-lib', () => { | ||
const originalModule = jest.requireActual('@openmrs/esm-patient-common-lib'); | ||
|
||
return { | ||
...originalModule, | ||
launchPatientWorkspace: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('StartVisit', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should launch start visit form', () => { | ||
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'prompt' }); | ||
render(<StartVisit patientUuid="some-uuid" />); | ||
|
||
expect( | ||
screen.getByText( | ||
`You can't add data to the patient chart without an active visit. Choose from one of the options below to continue.`, | ||
), | ||
).toBeInTheDocument(); | ||
|
||
const startNewVisitButton = screen.getByRole('button', { name: /Start new visit/i }); | ||
userEvent.click(startNewVisitButton); | ||
|
||
expect(launchPatientWorkspace).toHaveBeenCalledWith('start-visit-workspace-form'); | ||
}); | ||
|
||
test('should launch edit past visit form', () => { | ||
spyOn(mockUseVisitDialog, 'useVisitDialog').and.returnValue({ type: 'prompt', state: { type: 'past' } }); | ||
render(<StartVisit patientUuid="some-uuid" />); | ||
|
||
expect( | ||
screen.getByText( | ||
`You can add a new past visit or update an old one. Choose from one of the options below to continue.`, | ||
), | ||
).toBeInTheDocument(); | ||
|
||
const editPastVisitButton = screen.getByRole('button', { name: /Edit past visit/i }); | ||
userEvent.click(editPastVisitButton); | ||
|
||
expect(launchPatientWorkspace).toHaveBeenCalledWith('past-visits-overview'); | ||
}); | ||
}); |