-
Notifications
You must be signed in to change notification settings - Fork 227
/
appointments-form.test.tsx
203 lines (170 loc) · 7.26 KB
/
appointments-form.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mockLocations } from '__mocks__';
import { openmrsFetch, showSnackbar } from '@openmrs/esm-framework';
import { mockUseAppointmentServiceData, mockSessionDataResponse } from '__mocks__';
import { mockPatient, renderWithSwr, waitForLoadingToFinish } from 'tools';
import { saveAppointment } from '../appointments.resource';
import AppointmentForm from './appointments-form.component';
const testProps = {
closeWorkspace: jest.fn(),
patientUuid: mockPatient.id,
promptBeforeClosing: jest.fn(),
};
const mockCreateAppointment = saveAppointment as jest.Mock;
const mockOpenmrsFetch = openmrsFetch as jest.Mock;
const mockShowSnackbar = showSnackbar as jest.Mock;
jest.mock('@openmrs/esm-framework', () => {
const originalModule = jest.requireActual('@openmrs/esm-framework');
return {
...originalModule,
useLocations: jest.fn().mockImplementation(() => mockLocations),
useSession: jest.fn().mockImplementation(() => mockSessionDataResponse.data),
};
});
jest.mock('react-hook-form', () => ({
...jest.requireActual('react-hook-form'),
useForm: jest.fn().mockImplementation(() => ({
handleSubmit: () => jest.fn(),
control: {
register: jest.fn(),
unregister: jest.fn(),
getFieldState: jest.fn(),
_names: {
array: new Set('test'),
mount: new Set('test'),
unMount: new Set('test'),
watch: new Set('test'),
focus: 'test',
watchAll: false,
},
_subjects: {
watch: jest.fn(),
array: jest.fn(),
state: jest.fn(),
},
_getWatch: jest.fn(),
_formValues: [],
_defaultValues: [],
},
getValues: () => {
return [];
},
setValue: () => jest.fn(),
formState: () => jest.fn(),
watch: () => jest.fn(),
})),
Controller: ({ render }) =>
render({
field: {
onChange: jest.fn(),
onBlur: jest.fn(),
value: '',
ref: jest.fn(),
},
formState: {
isSubmitted: false,
},
fieldState: {
isTouched: false,
},
}),
useSubscribe: () => ({
r: { current: { subject: { subscribe: () => jest.fn() } } },
}),
}));
jest.mock('../appointments.resource', () => {
const originalModule = jest.requireActual('../appointments.resource');
return {
...originalModule,
saveAppointment: jest.fn(),
};
});
describe('AppointmentForm', () => {
it('renders the appointments form showing all the relevant fields and values', async () => {
mockOpenmrsFetch.mockReturnValue(mockUseAppointmentServiceData);
renderAppointmentsForm();
await waitForLoadingToFinish();
expect(screen.getByLabelText(/Select a location/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Date/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Select a service/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Select the type of appointment/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Write an additional note/i)).toBeInTheDocument();
expect(screen.getByPlaceholderText(/Write any additional points here/i)).toBeInTheDocument();
expect(screen.getByPlaceholderText(/dd\/mm\/yyyy/i)).toBeInTheDocument();
expect(screen.getByRole('option', { name: /Mosoriot/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /Inpatient Ward/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /AM/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /PM/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /Choose appointment type/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /Scheduled/i })).toBeInTheDocument();
expect(screen.getByRole('option', { name: /WalkIn/i })).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /Date/i })).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /Time/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Discard/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Save and close/i })).toBeInTheDocument();
});
it('closes the form and the workspace when the cancel button is clicked', async () => {
const user = userEvent.setup();
mockOpenmrsFetch.mockReturnValueOnce(mockUseAppointmentServiceData);
renderAppointmentsForm();
await waitForLoadingToFinish();
const cancelButton = screen.getByRole('button', { name: /Discard/i });
await user.click(cancelButton);
expect(testProps.closeWorkspace).toHaveBeenCalledTimes(1);
});
it('renders a success snackbar upon successfully scheduling an appointment', async () => {
const user = userEvent.setup();
mockOpenmrsFetch.mockReturnValue({ data: mockUseAppointmentServiceData });
mockCreateAppointment.mockResolvedValue({ status: 200, statusText: 'Ok' });
renderAppointmentsForm();
await waitForLoadingToFinish();
const saveButton = screen.getByRole('button', { name: /Save and close/i });
const dateInput = screen.getByRole('textbox', { name: /Date/i });
const timeInput = screen.getByRole('textbox', { name: /Time/i });
const timeFormat = screen.getByRole('combobox', { name: /Time/i });
const serviceSelect = screen.getByRole('combobox', { name: /Select a service/i });
const appointmentTypeSelect = screen.getByRole('combobox', { name: /Select the type of appointment/i });
expect(saveButton).not.toBeDisabled();
await user.clear(dateInput);
await user.type(dateInput, '4/4/2021');
await user.clear(timeInput);
await user.type(timeInput, '09:30');
await user.selectOptions(timeFormat, 'AM');
await user.selectOptions(serviceSelect, ['Outpatient']);
await user.selectOptions(appointmentTypeSelect, ['Scheduled']);
await user.click(saveButton);
});
it('renders an error snackbar if there was a problem scheduling an appointment', async () => {
const user = userEvent.setup();
const error = {
message: 'Internal Server Error',
response: {
status: 500,
statusText: 'Internal Server Error',
},
};
mockOpenmrsFetch.mockReturnValueOnce({ data: mockUseAppointmentServiceData });
mockCreateAppointment.mockRejectedValueOnce(error);
renderAppointmentsForm();
await waitForLoadingToFinish();
const saveButton = screen.getByRole('button', { name: /Save and close/i });
const dateInput = screen.getByRole('textbox', { name: /Date/i });
const timeInput = screen.getByRole('textbox', { name: /Time/i });
const timeFormat = screen.getByRole('combobox', { name: /Time/i });
const serviceSelect = screen.getByRole('combobox', { name: /Select a service/i });
const appointmentTypeSelect = screen.getByRole('combobox', { name: /Select the type of appointment/i });
await user.clear(dateInput);
await user.type(dateInput, '4/4/2021');
await user.clear(timeInput);
await user.type(timeInput, '09:30');
await user.selectOptions(timeFormat, 'AM');
await user.selectOptions(serviceSelect, ['Outpatient']);
await user.selectOptions(appointmentTypeSelect, ['Scheduled']);
await user.click(saveButton);
});
});
function renderAppointmentsForm() {
renderWithSwr(<AppointmentForm {...testProps} />);
}