-
Notifications
You must be signed in to change notification settings - Fork 227
/
end-visit-dialog.test.tsx
107 lines (88 loc) · 3.56 KB
/
end-visit-dialog.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React from 'react';
import userEvent from '@testing-library/user-event';
import { screen, render } from '@testing-library/react';
import { showSnackbar, updateVisit, useVisit, type Visit, type FetchResponse } from '@openmrs/esm-framework';
import { mockCurrentVisit } from '__mocks__';
import EndVisitDialog from './end-visit-dialog.component';
const endVisitPayload = {
stopDatetime: expect.any(Date),
};
const mockCloseModal = jest.fn();
const mockMutate = jest.fn();
const mockShowSnackbar = jest.mocked(showSnackbar);
const mockUseVisit = jest.mocked(useVisit);
const mockUpdateVisit = jest.mocked(updateVisit);
describe('End visit dialog', () => {
beforeEach(() => {
mockUseVisit.mockReturnValue({
activeVisit: mockCurrentVisit,
currentVisit: mockCurrentVisit,
currentVisitIsRetrospective: false,
error: null,
isLoading: false,
isValidating: false,
mutate: mockMutate,
});
});
test('displays a success snackbar when the visit is ended successfully', async () => {
const user = userEvent.setup();
mockUpdateVisit.mockResolvedValue({
status: 200,
data: {
visitType: {
display: 'Facility Visit',
},
},
} as unknown as FetchResponse<Visit>);
render(<EndVisitDialog patientUuid="some-patient-uuid" closeModal={mockCloseModal} />);
const closeModalButton = screen.getByRole('button', { name: /close/i });
const cancelButton = screen.getByRole('button', { name: /cancel/i });
const endVisitButton = screen.getByRole('button', { name: /end visit/i });
expect(closeModalButton).toBeInTheDocument();
expect(cancelButton).toBeInTheDocument();
expect(endVisitButton).toBeInTheDocument();
expect(
screen.getByRole('heading', { name: /are you sure you want to end this active visit?/i }),
).toBeInTheDocument();
expect(
screen.getByText(
/Ending this visit means that you will no longer be able to add encounters to it. If you need to add an encounter, you can create a new visit for this patient or edit a past one/i,
),
).toBeInTheDocument();
await user.click(endVisitButton);
expect(updateVisit).toHaveBeenCalledWith(mockCurrentVisit.uuid, endVisitPayload, expect.anything());
expect(mockShowSnackbar).toHaveBeenCalledWith({
isLowContrast: true,
subtitle: 'Facility Visit ended successfully',
kind: 'success',
title: 'Visit ended',
});
});
test('displays an error snackbar if there was a problem ending a visit', async () => {
const user = userEvent.setup();
const error = {
message: 'Internal error message',
response: {
status: 500,
statusText: 'Internal server error',
},
};
mockUpdateVisit.mockRejectedValue(error);
render(<EndVisitDialog patientUuid="some-patient-uuid" closeModal={mockCloseModal} />);
expect(
screen.getByText(
/Ending this visit means that you will no longer be able to add encounters to it. If you need to add an encounter, you can create a new visit for this patient or edit a past one/i,
),
).toBeInTheDocument();
const endVisitButton = screen.getByRole('button', { name: /End Visit/i });
expect(endVisitButton).toBeInTheDocument();
await user.click(endVisitButton);
expect(updateVisit).toHaveBeenCalledWith(mockCurrentVisit.uuid, endVisitPayload, new AbortController());
expect(mockShowSnackbar).toHaveBeenCalledWith({
subtitle: 'Internal error message',
kind: 'error',
title: 'Error ending visit',
isLowContrast: false,
});
});
});