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
/
Incidents.test.tsx
120 lines (106 loc) · 3.77 KB
/
Incidents.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
import '../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import { act } from '@testing-library/react'
import Permissions from 'model/Permissions'
import ViewIncident from '../../incidents/view/ViewIncident'
import Incidents from '../../incidents/Incidents'
import ReportIncident from '../../incidents/report/ReportIncident'
import Incident from '../../model/Incident'
import IncidentRepository from '../../clients/db/IncidentRepository'
const mockStore = configureMockStore([thunk])
describe('Incidents', () => {
describe('routing', () => {
describe('/incidents/new', () => {
it('should render the new incident screen when /incidents/new is accessed', () => {
const expectedIncident = {
id: '1234',
code: '1234',
} as Incident
jest.spyOn(IncidentRepository, 'find').mockResolvedValue(expectedIncident)
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ReportIncident] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
incident: {
incident: expectedIncident,
},
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents/new']}>
<Incidents />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(ReportIncident)).toHaveLength(1)
})
it('should not navigate to /incidents/new if the user does not have ReportIncident permissions', () => {
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents/new']}>
<Incidents />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(ReportIncident)).toHaveLength(0)
})
})
describe('/incidents/:id', () => {
it('should render the view incident screen when /incidents/:id is accessed', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewIncident] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
incident: {
incident: {
id: '1234',
code: '1234 ',
date: new Date().toISOString(),
reportedOn: new Date().toISOString(),
},
},
})
let wrapper: any
await act(async () => {
wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents/1234']}>
<Incidents />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(ViewIncident)).toHaveLength(1)
})
})
it('should not navigate to /incidents/:id if the user does not have ViewIncident permissions', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents/1234']}>
<Incidents />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(ViewIncident)).toHaveLength(0)
})
})
})
})