forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserList.test.js
More file actions
188 lines (171 loc) · 5.5 KB
/
UserList.test.js
File metadata and controls
188 lines (171 loc) · 5.5 KB
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
import React from 'react'
import UserList from '../UserList'
import { UPDATE_USER_ROLES } from '../graphql/mutations'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { MemoryRouter, Router } from 'react-router-dom'
import { ThemeProvider, theme } from '@chakra-ui/core'
import { I18nProvider } from '@lingui/react'
import { createMemoryHistory } from 'history'
import { MockedProvider } from '@apollo/client/testing'
import { UserStateProvider } from '../UserState'
import { setupI18n } from '@lingui/core'
describe('<UserList />', () => {
const data = {
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
},
edges: [
{
node: {
id: 'VXNlckxpc3RJdGVtOigzLCAyKQ==',
userName: 'testuser@testemail.gc.ca',
admin: true,
tfa: false,
displayName: 'Test User Esq.',
},
},
],
}
it('successfully renders with mocked data', async () => {
// Set the inital history item to user-list
const { getAllByText } = render(
<UserStateProvider
initialState={{
userName: 'testuser@testemail.gc.ca',
jwt: 'string',
tfa: false,
}}
>
<ThemeProvider theme={theme}>
<I18nProvider i18n={setupI18n()}>
<MemoryRouter initialEntries={['/']}>
<MockedProvider>
<UserList userListData={data} />
</MockedProvider>
</MemoryRouter>
</I18nProvider>
</ThemeProvider>
</UserStateProvider>,
)
//
// Get all of the mocked user cards, and expect there to be only one entry.
await waitFor(() => {
const userCards = getAllByText('testuser@testemail.gc.ca')
expect(userCards).toHaveLength(1)
})
})
it('redirects to userPage when a list element is clicked', async () => {
// create a history object and inject it so we can inspect it afterwards
// for the side effects of our form submission (a redirect to /!).
const history = createMemoryHistory({
initialEntries: ['/user-list'],
initialIndex: 0,
})
// Set the inital history item to user-list
const { getAllByText } = render(
<UserStateProvider
initialState={{
userName: 'testuser@testemail.gc.ca',
jwt: 'string',
tfa: false,
}}
>
<ThemeProvider theme={theme}>
<I18nProvider i18n={setupI18n()}>
<Router history={history}>
<MockedProvider>
<UserList userListData={data} />
</MockedProvider>
</Router>
</I18nProvider>
</ThemeProvider>
</UserStateProvider>,
)
// Get all of the mocked user cards, and expect there to be only one entry.
const userCards = await waitFor(() =>
getAllByText('testuser@testemail.gc.ca'),
)
expect(userCards).toHaveLength(1)
const leftClick = { button: 0 }
fireEvent.click(userCards[0], leftClick)
// default `button` property for click events is set to `0` which is a left click.
await waitFor(() => {
// Path should be '/user', so expect that value
expect(history.location.pathname).toEqual('/user')
})
})
describe('Admin profile userlist', () => {
it('updateUserRole elements render', async () => {
const history = createMemoryHistory({
initialEntries: ['/user-list'],
initialIndex: 0,
})
const mocks = [
{
request: {
query: UPDATE_USER_ROLES,
variables: {
input: {
orgSlug: 'test-org-slug',
role: 'USER_WRITE',
userName: 'testuser@testemail.gc.ca',
},
},
},
result: {
data: {
updateUserRole: {
status: 'string',
},
},
},
},
]
const { getAllByText, getByDisplayValue, getByText } = render(
<UserStateProvider
initialState={{
userName: 'testadmin@testemail.gc.ca',
jwt: 'string',
tfa: false,
}}
>
<ThemeProvider theme={theme}>
<I18nProvider i18n={setupI18n()}>
<Router history={history}>
<MockedProvider mocks={mocks} addTypename={false}>
<UserList
userListData={data}
permission="SUPER_ADMIN"
orgSlug="test-org-slug"
/>
</MockedProvider>
</Router>
</I18nProvider>
</ThemeProvider>
</UserStateProvider>,
)
const userRole = getByDisplayValue(/READ/i)
await waitFor(() => {
expect(userRole.type).toEqual('select-one')
})
// change input on select to WRITE
fireEvent.change(userRole, { target: { value: 'USER_WRITE' } })
await waitFor(() => {
const newRole = getByDisplayValue(/WRITE/i)
expect(newRole.type).toEqual('select-one')
})
// Apply changes button
const updateButton = await waitFor(() => getAllByText(/Apply/i))
expect(updateButton).toHaveLength(1)
const leftClick = { button: 0 }
fireEvent.click(updateButton[0], leftClick)
// default `button` property for click events is set to `0` which is a left click.
// await changes
await waitFor(() => {
const newRole = getByText(/Role updated/i)
expect(newRole).toBeInTheDocument()
})
})
})
})