Skip to content

Commit ac3efd2

Browse files
committed
test: refactor setup and queries
1 parent 21700c5 commit ac3efd2

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/__tests__/patients/visits/AddVisitModal.test.tsx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { screen, render as rtlRender, fireEvent, waitFor } from '@testing-library/react'
1+
import { screen, render, fireEvent, waitFor, within } from '@testing-library/react'
22
import userEvent from '@testing-library/user-event'
33
import { createMemoryHistory } from 'history'
44
import React from 'react'
@@ -26,32 +26,26 @@ describe('Add Visit Modal', () => {
2626
} as Patient
2727

2828
const onCloseSpy = jest.fn()
29-
const render = () => {
29+
const setup = () => {
3030
jest.spyOn(PatientRepository, 'find').mockResolvedValue(patient)
3131
jest.spyOn(PatientRepository, 'saveOrUpdate')
3232
const history = createMemoryHistory()
3333

34-
// eslint-disable-next-line react/prop-types
35-
const Wrapper: React.FC = ({ children }) => <Router history={history}>{children}</Router>
36-
37-
const results = rtlRender(
38-
<AddVisitModal show onCloseButtonClick={onCloseSpy} patientId={patient.id} />,
39-
{
40-
wrapper: Wrapper,
41-
},
34+
return render(
35+
<Router history={history}>
36+
<AddVisitModal show onCloseButtonClick={onCloseSpy} patientId={patient.id} />
37+
</Router>,
4238
)
43-
44-
return results
4539
}
4640

4741
it('should render a modal and within a form', () => {
48-
render()
42+
setup()
4943

5044
expect(screen.getByRole('dialog').querySelector('form')).toBeInTheDocument()
5145
})
5246

5347
it('should call the on close function when the cancel button is clicked', () => {
54-
render()
48+
setup()
5549
userEvent.click(
5650
screen.getByRole('button', {
5751
name: /close/i,
@@ -61,14 +55,16 @@ describe('Add Visit Modal', () => {
6155
})
6256

6357
it('should save the visit when the save button is clicked', async () => {
64-
render()
58+
setup()
6559
const testPatient = patient.visits[0]
66-
const modal = screen.getByRole('dialog')
6760

6861
/* Date Pickers */
69-
const modalDatePickerWrappers = modal.querySelectorAll('.react-datepicker__input-container')
70-
const startDateInput = modalDatePickerWrappers[0].querySelector('input') as HTMLInputElement
71-
const endDateInput = modalDatePickerWrappers[1].querySelector('input') as HTMLInputElement
62+
const startDateInput = within(screen.getByTestId('startDateTimeDateTimePicker')).getByRole(
63+
'textbox',
64+
)
65+
const endDateInput = within(screen.getByTestId('endDateTimeDateTimePicker')).getByRole(
66+
'textbox',
67+
)
7268

7369
fireEvent.change(startDateInput, { target: { value: testPatient.startDateTime } })
7470
fireEvent.change(endDateInput, { target: { value: testPatient.endDateTime } })
@@ -80,7 +76,7 @@ describe('Add Visit Modal', () => {
8076
const statusInput = screen.getByRole('combobox')
8177
userEvent.type(statusInput, `${testPatient.status}{arrowdown}{enter}`)
8278

83-
const textareaReason = screen.getAllByRole('textbox')[4]
79+
const textareaReason = screen.getByLabelText(/patient\.visits\.reason/i)
8480
userEvent.type(textareaReason, testPatient.reason)
8581

8682
const locationInput = screen.getByLabelText(/patient.visits.location/i)

0 commit comments

Comments
 (0)