1
- import { screen , render as rtlRender , fireEvent , waitFor } from '@testing-library/react'
1
+ import { screen , render , fireEvent , waitFor , within } from '@testing-library/react'
2
2
import userEvent from '@testing-library/user-event'
3
3
import { createMemoryHistory } from 'history'
4
4
import React from 'react'
@@ -26,32 +26,26 @@ describe('Add Visit Modal', () => {
26
26
} as Patient
27
27
28
28
const onCloseSpy = jest . fn ( )
29
- const render = ( ) => {
29
+ const setup = ( ) => {
30
30
jest . spyOn ( PatientRepository , 'find' ) . mockResolvedValue ( patient )
31
31
jest . spyOn ( PatientRepository , 'saveOrUpdate' )
32
32
const history = createMemoryHistory ( )
33
33
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 > ,
42
38
)
43
-
44
- return results
45
39
}
46
40
47
41
it ( 'should render a modal and within a form' , ( ) => {
48
- render ( )
42
+ setup ( )
49
43
50
44
expect ( screen . getByRole ( 'dialog' ) . querySelector ( 'form' ) ) . toBeInTheDocument ( )
51
45
} )
52
46
53
47
it ( 'should call the on close function when the cancel button is clicked' , ( ) => {
54
- render ( )
48
+ setup ( )
55
49
userEvent . click (
56
50
screen . getByRole ( 'button' , {
57
51
name : / c l o s e / i,
@@ -61,14 +55,16 @@ describe('Add Visit Modal', () => {
61
55
} )
62
56
63
57
it ( 'should save the visit when the save button is clicked' , async ( ) => {
64
- render ( )
58
+ setup ( )
65
59
const testPatient = patient . visits [ 0 ]
66
- const modal = screen . getByRole ( 'dialog' )
67
60
68
61
/* 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
+ )
72
68
73
69
fireEvent . change ( startDateInput , { target : { value : testPatient . startDateTime } } )
74
70
fireEvent . change ( endDateInput , { target : { value : testPatient . endDateTime } } )
@@ -80,7 +76,7 @@ describe('Add Visit Modal', () => {
80
76
const statusInput = screen . getByRole ( 'combobox' )
81
77
userEvent . type ( statusInput , `${ testPatient . status } {arrowdown}{enter}` )
82
78
83
- const textareaReason = screen . getAllByRole ( 'textbox' ) [ 4 ]
79
+ const textareaReason = screen . getByLabelText ( / p a t i e n t \. v i s i t s \. r e a s o n / i )
84
80
userEvent . type ( textareaReason , testPatient . reason )
85
81
86
82
const locationInput = screen . getByLabelText ( / p a t i e n t .v i s i t s .l o c a t i o n / i)
0 commit comments