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
/
Patients.test.tsx
136 lines (115 loc) · 4.18 KB
/
Patients.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
import '../../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { TextInput, Spinner } from '@hospitalrun/components'
import { MemoryRouter } from 'react-router-dom'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureStore from 'redux-mock-store'
import { mocked } from 'ts-jest/utils'
import { act } from 'react-dom/test-utils'
import * as ButtonBarProvider from 'page-header/ButtonBarProvider'
import format from 'date-fns/format'
import Patients from '../../../patients/list/Patients'
import PatientRepository from '../../../clients/db/PatientRepository'
import * as patientSlice from '../../../patients/patients-slice'
const middlewares = [thunk]
const mockStore = configureStore(middlewares)
describe('Patients', () => {
const patients = [
{
id: '123',
fullName: 'test test',
givenName: 'test',
familyName: 'test',
code: 'P12345',
sex: 'male',
dateOfBirth: new Date().toISOString(),
},
]
const mockedPatientRepository = mocked(PatientRepository, true)
const setup = (isLoading?: boolean) => {
const store = mockStore({
patients: {
patients,
isLoading,
},
})
return mount(
<Provider store={store}>
<MemoryRouter>
<Patients />
</MemoryRouter>
</Provider>,
)
}
beforeEach(() => {
jest.resetAllMocks()
jest.spyOn(PatientRepository, 'findAll')
mockedPatientRepository.findAll.mockResolvedValue([])
})
describe('layout', () => {
afterEach(() => {
jest.restoreAllMocks()
})
it('should render a loading bar if it is loading', () => {
const wrapper = setup(true)
expect(wrapper.find(Spinner)).toHaveLength(1)
})
it('should render a table of patients', () => {
const wrapper = setup()
const table = wrapper.find('table')
const tableHeaders = table.find('th')
const tableColumns = table.find('td')
expect(table).toHaveLength(1)
expect(tableHeaders).toHaveLength(5)
expect(tableColumns).toHaveLength(5)
expect(tableHeaders.at(0).text()).toEqual('patient.code')
expect(tableHeaders.at(1).text()).toEqual('patient.givenName')
expect(tableHeaders.at(2).text()).toEqual('patient.familyName')
expect(tableHeaders.at(3).text()).toEqual('patient.sex')
expect(tableHeaders.at(4).text()).toEqual('patient.dateOfBirth')
expect(tableColumns.at(0).text()).toEqual(patients[0].code)
expect(tableColumns.at(1).text()).toEqual(patients[0].givenName)
expect(tableColumns.at(2).text()).toEqual(patients[0].familyName)
expect(tableColumns.at(3).text()).toEqual(patients[0].sex)
expect(tableColumns.at(4).text()).toEqual(
format(new Date(patients[0].dateOfBirth), 'yyyy-MM-dd'),
)
})
it('should add a "New Patient" button to the button tool bar', () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
setup()
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual('patients.newPatient')
})
})
describe('search functionality', () => {
beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.useRealTimers())
it('should search for patients after the search text has not changed for 500 milliseconds', () => {
const searchPatientsSpy = jest.spyOn(patientSlice, 'searchPatients')
const wrapper = setup()
searchPatientsSpy.mockClear()
const expectedSearchText = 'search text'
act(() => {
;(wrapper.find(TextInput).prop('onChange') as any)({
target: {
value: expectedSearchText,
},
preventDefault(): void {
// noop
},
} as React.ChangeEvent<HTMLInputElement>)
})
act(() => {
jest.advanceTimersByTime(500)
})
wrapper.update()
expect(searchPatientsSpy).toHaveBeenCalledTimes(1)
expect(searchPatientsSpy).toHaveBeenLastCalledWith(expectedSearchText)
})
})
})