This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
AddRelatedPersonModal.test.tsx
153 lines (133 loc) · 4.68 KB
/
AddRelatedPersonModal.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
import '../../../__mocks__/matchMediaMock'
import React from 'react'
import { ReactWrapper, mount } from 'enzyme'
import { Modal, Alert, Typeahead } from '@hospitalrun/components'
import { act } from '@testing-library/react'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore, { MockStore } from 'redux-mock-store'
import Patient from 'model/Patient'
import TextInputWithLabelFormGroup from '../../../components/input/TextInputWithLabelFormGroup'
import AddRelatedPersonModal from '../../../patients/related-persons/AddRelatedPersonModal'
const mockStore = configureMockStore([thunk])
describe('Add Related Person Modal', () => {
const patient = {
id: '123',
prefix: 'prefix',
givenName: 'givenName',
familyName: 'familyName',
suffix: 'suffix',
sex: 'male',
type: 'charity',
occupation: 'occupation',
preferredLanguage: 'preferredLanguage',
phoneNumber: 'phoneNumber',
email: 'email@email.com',
address: 'address',
code: 'P00001',
dateOfBirth: new Date().toISOString(),
} as Patient
let store: MockStore
describe('layout', () => {
let wrapper: ReactWrapper
store = mockStore({
patient: { patient },
})
beforeEach(() => {
wrapper = mount(
<Provider store={store}>
<AddRelatedPersonModal
show
onSave={jest.fn()}
onCloseButtonClick={jest.fn()}
toggle={jest.fn()}
/>
</Provider>,
)
})
it('should render a modal', () => {
const modal = wrapper.find(Modal)
expect(modal).toHaveLength(1)
expect(modal.prop('show')).toBeTruthy()
})
it('should render a patient search typeahead', () => {
const patientSearchTypeahead = wrapper.find(Typeahead)
expect(patientSearchTypeahead).toHaveLength(1)
expect(patientSearchTypeahead.prop('placeholder')).toEqual('patient.relatedPerson')
})
it('should render a relationship type text input', () => {
const relationshipTypeTextInput = wrapper.findWhere((w) => w.prop('name') === 'type')
expect(relationshipTypeTextInput).toHaveLength(1)
expect(relationshipTypeTextInput.type()).toBe(TextInputWithLabelFormGroup)
expect(relationshipTypeTextInput.prop('name')).toEqual('type')
expect(relationshipTypeTextInput.prop('isEditable')).toBeTruthy()
expect(relationshipTypeTextInput.prop('label')).toEqual(
'patient.relatedPersons.relationshipType',
)
})
it('should render a cancel button', () => {
const cancelButton = wrapper.findWhere(
(w: { text: () => string }) => w.text() === 'actions.cancel',
)
expect(cancelButton).toHaveLength(1)
})
it('should render an add new related person button button', () => {
const modal = wrapper.find(Modal)
expect(modal.prop('successButton').children).toEqual('patient.relatedPersons.add')
})
})
describe('save', () => {
let wrapper: ReactWrapper
let onSaveSpy = jest.fn()
beforeEach(() => {
onSaveSpy = jest.fn()
wrapper = mount(
<Provider store={store}>
<AddRelatedPersonModal
show
onSave={onSaveSpy}
onCloseButtonClick={jest.fn()}
toggle={jest.fn()}
/>
</Provider>,
)
})
it('should call the save function with the correct data', () => {
act(() => {
const patientTypeahead = wrapper.find(Typeahead)
patientTypeahead.prop('onChange')([{ id: '123' }])
})
wrapper.update()
act(() => {
const relationshipTypeTextInput = wrapper.findWhere((w) => w.prop('name') === 'type')
relationshipTypeTextInput.prop('onChange')({ target: { value: 'relationship' } })
})
wrapper.update()
act(() => {
;(wrapper.find(Modal).prop('successButton') as any).onClick(
{} as React.MouseEvent<HTMLButtonElement, MouseEvent>,
)
})
expect(onSaveSpy).toHaveBeenCalledTimes(1)
expect(onSaveSpy).toHaveBeenCalledWith({
patientId: '123',
type: 'relationship',
})
})
it('should display an error message if given name or relationship type is not entered.', () => {
act(() => {
wrapper
.find(Modal)
.prop('successButton')
.onClick({} as React.MouseEvent<HTMLButtonElement, MouseEvent>)
})
wrapper.update()
const errorAlert = wrapper.find(Alert)
expect(onSaveSpy).not.toHaveBeenCalled()
expect(errorAlert).toHaveLength(1)
expect(errorAlert.prop('message')).toEqual(
'patient.relatedPersons.error.relatedPersonRequired patient.relatedPersons.error.relationshipTypeRequired',
)
})
})
})