-
-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathteams.test.js
More file actions
320 lines (294 loc) · 10.2 KB
/
teams.test.js
File metadata and controls
320 lines (294 loc) · 10.2 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import '@testing-library/jest-dom';
import { screen, waitFor, within, act } from '@testing-library/react';
import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6';
import { QueryParamProvider } from 'use-query-params';
import toast from 'react-hot-toast';
import {
createComponentWithMemoryRouter,
QueryClientProviders,
ReduxIntlProviders,
renderWithRouter,
} from '../../utils/testWithIntl';
import { ManageTeams, EditTeam, CreateTeam, MyTeams } from '../teams';
import { store } from '../../store';
import { setupFaultyHandlers } from '../../network/tests/server';
jest.mock('react-hot-toast', () => ({
success: jest.fn(),
error: jest.fn(),
}));
describe('List Teams', () => {
it('should show loading placeholder when teams are being fetched', async () => {
const { container } = renderWithRouter(
<QueryClientProviders>
<QueryParamProvider adapter={ReactRouter6Adapter}>
<ReduxIntlProviders>
<ManageTeams />
</ReduxIntlProviders>
</QueryParamProvider>
</QueryClientProviders>,
);
act(() => {
store.dispatch({
type: 'SET_USER_DETAILS',
userDetails: { id: 122, username: 'test_user', role: 'ADMIN' },
});
store.dispatch({ type: 'SET_TOKEN', token: 'validToken' });
});
expect(container.getElementsByClassName('show-loading-animation').length).toBe(4 * 9);
});
it('should fetch and list teams', async () => {
const { container } = renderWithRouter(
<QueryClientProviders>
<QueryParamProvider adapter={ReactRouter6Adapter}>
<ReduxIntlProviders>
<ManageTeams />
</ReduxIntlProviders>
</QueryParamProvider>
</QueryClientProviders>,
);
await waitFor(() =>
expect(container.getElementsByClassName('show-loading-animation').length).toBe(0),
);
expect(screen.getByRole('heading', { name: /team test/i })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: /test kll team/i })).toBeInTheDocument();
});
it('should navigate to team detail page on team article click', async () => {
const { user, router, container } = createComponentWithMemoryRouter(
<QueryClientProviders>
<QueryParamProvider adapter={ReactRouter6Adapter}>
<ReduxIntlProviders>
<ManageTeams />
</ReduxIntlProviders>
</QueryParamProvider>
</QueryClientProviders>,
);
await waitFor(() =>
expect(container.getElementsByClassName('show-loading-animation').length).toBe(0),
);
await user.click(screen.getByRole('heading', { name: /team test/i }));
await waitFor(() => expect(router.state.location.pathname).toBe('/teams/1/membership/'));
});
});
describe('Create Team', () => {
const setup = () => {
const { user, router } = createComponentWithMemoryRouter(
<ReduxIntlProviders>
<CreateTeam />
</ReduxIntlProviders>,
);
const createButton = screen.getByRole('button', {
name: /create team/i,
});
return {
user,
createButton,
router,
};
};
it('should disable create team button by default', () => {
const { createButton } = setup();
expect(createButton).toBeDisabled();
});
it('should fill the default manager list by the current user', () => {
setup();
const link = screen.getByRole('link', {
name: /test_user/i,
});
expect(within(link).getByTitle(/test_user/i)).toBeInTheDocument();
});
it('should navigate to the newly created team detail page on creation success', async () => {
const { createButton, router, user } = setup();
const nameInput = screen.getAllByRole('textbox')[0];
const orgSelect = screen.getByRole('combobox');
const joinMethodOption = screen.getAllByRole('radio')[0];
await user.type(nameInput, 'New Team Name');
await user.click(orgSelect);
const orgOption = await screen.findByText('American Red Cross');
await user.click(orgOption);
expect(createButton).toBeEnabled();
await user.click(joinMethodOption);
await user.click(createButton);
await waitFor(() => {
expect(toast.success).toHaveBeenCalledTimes(1);
expect(router.state.location.pathname).toBe('/manage/teams/123');
});
});
});
describe('Edit Team', () => {
jest.retryTimes(2);
it('should display default details of the team before editing', async () => {
await act(() => {
store.dispatch({
type: 'SET_USER_DETAILS',
userDetails: { id: 122, username: 'test_user', role: 'ADMIN' },
});
store.dispatch({ type: 'SET_TOKEN', token: 'validToken' });
});
renderWithRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
);
const nameInput = screen.getAllByRole('textbox')[0];
await waitFor(() => expect(nameInput).toHaveValue('Team Test'));
expect(screen.getAllByRole('textbox')[1].value).toBe('Dummy team test');
await waitFor(() => expect(screen.getByText('Organisation Name 123')).toBeInTheDocument());
expect(screen.getByRole('link', { name: 'sample_user' })).toBeInTheDocument();
});
it('should display save and cancel button when project details are changed', async () => {
const { user } = renderWithRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
);
const nameInput = screen.getAllByRole('textbox')[0];
await waitFor(() => expect(nameInput.value).toBe('Team Test'));
await user.clear(nameInput);
await user.type(nameInput, 'Changed Team Test');
const saveButton = screen.getByRole('button', {
name: /save/i,
});
const cancelButton = screen.getByRole('button', {
name: /cancel/i,
});
expect(saveButton).toBeInTheDocument();
expect(cancelButton).toBeInTheDocument();
});
it('should hide the save button on successful updation of team details', async () => {
const { user } = renderWithRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
);
const nameInput = screen.getAllByRole('textbox')[0];
await waitFor(() => expect(nameInput.value).toBe('Team Test'));
await user.clear(nameInput);
await user.type(nameInput, 'Changed Team Test');
const saveButton = screen.getByRole('button', {
name: /save/i,
});
await user.click(saveButton);
await waitFor(() => expect(toast.success).toHaveBeenCalledTimes(1));
expect(saveButton).not.toBeInTheDocument();
});
it('should display callout alert error when team info cannot be updated', async () => {
setupFaultyHandlers();
const { user } = renderWithRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
);
const nameInput = screen.getAllByRole('textbox')[0];
await waitFor(() => expect(nameInput.value).toBe('Team Test'));
await user.clear(nameInput);
await user.type(nameInput, 'Changed Team Test');
const saveButton = screen.getByRole('button', {
name: /save/i,
});
await user.click(saveButton);
await waitFor(() =>
expect(
screen.getByText(/Failed to update team information. Please try again/i),
).toBeInTheDocument(),
);
});
});
describe('Delete Team', () => {
const setup = async () => {
const { user, history } = renderWithRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
);
const deleteButton = screen.getByRole('button', {
name: /delete/i,
});
await user.click(deleteButton);
return {
user,
history,
deleteButton,
};
};
it('should ask for confirmation when user tries to delete a team', async () => {
await setup();
const deleteDialog = screen.getByRole('dialog');
expect(
within(deleteDialog).getByRole('heading', {
name: 'Are you sure you want to delete this team?',
}),
).toBeInTheDocument();
});
it('should close the confirmation dialog when cancel is clicked', async () => {
const { user } = await setup();
const cancelButton = screen.getByRole('button', {
name: /cancel/i,
});
await user.click(cancelButton);
expect(
screen.queryByText('Are you sure you want to delete this team?'),
).not.toBeInTheDocument();
});
it('should direct to teams list page on successful deletion of a team', async () => {
const { user, router } = createComponentWithMemoryRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
{
route: '/manage/teams/123',
},
);
const deleteButton = screen.getByRole('button', {
name: /delete/i,
});
await user.click(deleteButton);
const dialog = await screen.findByRole('dialog');
const deleteConfirmationButton = within(dialog).getByRole('button', {
name: /delete/i,
});
await user.click(deleteConfirmationButton);
expect(await screen.findByText('Team deleted successfully.')).toBeInTheDocument();
await waitFor(() => expect(router.state.location.pathname).toBe('/manage/teams'));
});
it('should display toast message if the team deletion fails', async () => {
setupFaultyHandlers();
const { user } = createComponentWithMemoryRouter(
<ReduxIntlProviders>
<EditTeam id={123} />
</ReduxIntlProviders>,
{
route: '/manage/teams/123',
},
);
const deleteButton = screen.getByRole('button', {
name: /delete/i,
});
await user.click(deleteButton);
const dialog = await screen.findByRole('dialog');
const deleteConfirmationButton = within(dialog).getByRole('button', {
name: /delete/i,
});
await user.click(deleteConfirmationButton);
await waitFor(() =>
expect(
screen.getByText(/An error occurred when trying to delete this team./i),
).toBeInTheDocument(),
);
});
});
test('MyTeams Component renders its child component', () => {
renderWithRouter(
<QueryClientProviders>
<QueryParamProvider adapter={ReactRouter6Adapter}>
<ReduxIntlProviders>
<MyTeams />
</ReduxIntlProviders>
</QueryParamProvider>
</QueryClientProviders>,
);
expect(
screen.getByRole('heading', {
name: /my teams/i,
}),
).toBeInTheDocument();
});