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
/
ContactInfo.test.tsx
231 lines (192 loc) · 7.31 KB
/
ContactInfo.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import '../../__mocks__/matchMediaMock'
import { Column, Spinner } from '@hospitalrun/components'
import { mount } from 'enzyme'
import { createMemoryHistory } from 'history'
import React from 'react'
import { act } from 'react-dom/test-utils'
import { Router } from 'react-router-dom'
import TextInputWithLabelFormGroup from '../../components/input/TextInputWithLabelFormGroup'
import { ContactInfoPiece } from '../../model/ContactInformation'
import ContactInfo from '../../patients/ContactInfo'
import * as uuid from '../../util/uuid'
describe('Contact Info in its Editable mode', () => {
const data = [
{ id: '123', value: '123456', type: 'home' },
{ id: '456', value: '789012', type: undefined },
]
const dataForNoAdd = [
{ id: '123', value: '123456', type: 'home' },
{ id: '456', value: ' ', type: undefined },
]
const errors = ['this is an error', '']
const label = 'this is a label'
const name = 'this is a name'
let onChange: jest.Mock
const setup = (_data?: ContactInfoPiece[], _errors?: string[]) => {
const history = createMemoryHistory()
history.push('/patients/new')
onChange = jest.fn()
const wrapper = mount(
<Router history={history}>
<ContactInfo
component="TextInputWithLabelFormGroup"
data={_data}
errors={_errors}
label={label}
name={name}
isEditable
onChange={onChange}
/>
</Router>,
)
return wrapper
}
it('should show a spinner if no data is present', () => {
const wrapper = setup()
const spinnerWrapper = wrapper.find(Spinner)
expect(spinnerWrapper).toHaveLength(1)
})
it('should call onChange if no data is provided', () => {
const newId = 'newId'
jest.spyOn(uuid, 'uuid').mockReturnValue(newId)
setup()
const expectedNewData = [{ id: newId, value: '' }]
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(expectedNewData)
})
it('should render the labels if data is provided', () => {
const wrapper = setup(data)
const headerWrapper = wrapper.find('.header')
const columnWrappers = headerWrapper.find(Column)
const expectedTypeLabel = 'patient.contactInfoType.label'
expect(columnWrappers.at(0).text()).toEqual(`${expectedTypeLabel} & ${label}`)
expect(columnWrappers.at(1).text()).toEqual(label)
})
it('should display the entries if data is provided', () => {
const wrapper = setup(data)
for (let i = 0; i < wrapper.length; i += 1) {
const inputWrapper = wrapper.findWhere((w: any) => w.prop('name') === `${name}${i}`)
expect(inputWrapper.prop('value')).toEqual(data[i].value)
}
})
it('should display the error if error is provided', () => {
const wrapper = setup(data, errors)
const feedbackWrappers = wrapper.find('.invalid-feedback')
expect(feedbackWrappers).toHaveLength(errors.length * 2)
for (let i = 0; i < feedbackWrappers.length; i += 1) {
if (i % 2 === 1) {
const j = (i - 1) / 2
expect(feedbackWrappers.at(i).text()).toEqual(errors[j])
}
}
})
it('should display the add button', () => {
const wrapper = setup(data)
const buttonWrapper = wrapper.find('button')
expect(buttonWrapper.text().trim()).toEqual('actions.add')
})
it('should call the onChange callback if select is changed', () => {
const wrapper = setup(data)
const select = wrapper.findWhere((w: any) => w.prop('name') === `${name}Type0`).find('select')
select.getDOMNode<HTMLSelectElement>().value = 'mobile'
select.simulate('change')
const expectedNewData = [
{ id: '123', value: '123456', type: 'mobile' },
{ id: '456', value: '789012', type: undefined },
]
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(expectedNewData)
})
it('should call the onChange callback if input is changed', () => {
const wrapper = setup(data)
const input = wrapper.findWhere((w: any) => w.prop('name') === `${name}0`).find('input')
input.getDOMNode<HTMLInputElement>().value = '777777'
input.simulate('change')
const expectedNewData = [
{ id: '123', value: '777777', type: 'home' },
{ id: '456', value: '789012', type: undefined },
]
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(expectedNewData)
})
it('should call the onChange callback if an add button is clicked with valid entries', () => {
const wrapper = setup(data)
const buttonWrapper = wrapper.find('button')
const onClick = buttonWrapper.prop('onClick') as any
const newId = 'newId'
jest.spyOn(uuid, 'uuid').mockReturnValue(newId)
act(() => {
onClick()
})
const expectedNewData = [...data, { id: newId, value: '' }]
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(expectedNewData)
})
it('should call the onChange callback if an add button is clicked with an empty entry', () => {
const wrapper = setup(dataForNoAdd)
const buttonWrapper = wrapper.find('button')
const onClick = buttonWrapper.prop('onClick') as any
const newId = 'newId'
jest.spyOn(uuid, 'uuid').mockReturnValue(newId)
act(() => {
onClick()
})
const expectedNewData = [
{ id: '123', value: '123456', type: 'home' },
{ id: newId, value: '' },
]
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith(expectedNewData)
})
})
describe('Contact Info in its non-Editable mode', () => {
const data = [
{ id: '123', value: '123456', type: 'home' },
{ id: '456', value: '789012', type: undefined },
]
const label = 'this is a label'
const name = 'this is a name'
const setup = (_data?: ContactInfoPiece[]) => {
const history = createMemoryHistory()
history.push('/patients/new')
const wrapper = mount(
<Router history={history}>
<ContactInfo
component="TextInputWithLabelFormGroup"
data={_data}
label={label}
name={name}
/>
</Router>,
)
return wrapper
}
it('should render an empty element if no data is present', () => {
const wrapper = setup()
const contactInfoWrapper = wrapper.find(ContactInfo)
expect(contactInfoWrapper.find('div')).toHaveLength(1)
expect(contactInfoWrapper.containsMatchingElement(<div />)).toEqual(true)
})
it('should render the labels if data is provided', () => {
const wrapper = setup(data)
const headerWrapper = wrapper.find('.header')
const columnWrappers = headerWrapper.find(Column)
const expectedTypeLabel = 'patient.contactInfoType.label'
expect(columnWrappers.at(0).text()).toEqual(`${expectedTypeLabel} & ${label}`)
expect(columnWrappers.at(1).text()).toEqual(label)
})
it('should display the entries if data is provided', () => {
const wrapper = setup(data)
for (let i = 0; i < wrapper.length; i += 1) {
const inputWrapper = wrapper.findWhere((w: any) => w.prop('name') === `${name}${i}`)
expect(inputWrapper.prop('value')).toEqual(data[i].value)
}
})
it('should show inputs that are not editable', () => {
const wrapper = setup(data)
const inputWrappers = wrapper.find(TextInputWithLabelFormGroup)
for (let i = 0; i < inputWrappers.length; i += 1) {
expect(inputWrappers.at(i).prop('isEditable')).toBeFalsy()
}
})
})