-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathPostEditor.test.jsx
401 lines (353 loc) · 14.1 KB
/
PostEditor.test.jsx
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import React from 'react';
import {
fireEvent, render, screen, waitFor,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import MockAdapter from 'axios-mock-adapter';
import { act } from 'react-dom/test-utils';
import { IntlProvider } from 'react-intl';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { Factory } from 'rosie';
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { AppProvider } from '@edx/frontend-platform/react';
import { getApiBaseUrl, Routes as ROUTES } from '../../../data/constants';
import { initializeStore } from '../../../store';
import executeThunk from '../../../test-utils';
import { getCohortsApiUrl } from '../../cohorts/data/api';
import DiscussionContext from '../../common/context';
import fetchCourseTopics from '../../topics/data/thunks';
import { getThreadsApiUrl } from '../data/api';
import { fetchThread } from '../data/thunks';
import PostEditor from './PostEditor';
import '../../cohorts/data/__factories__';
import '../../data/__factories__';
import '../../topics/data/__factories__';
import '../data/__factories__';
const courseId = 'course-v1:edX+DemoX+Demo_Course';
const topicsApiUrl = `${getApiBaseUrl()}/api/discussion/v1/course_topics/${courseId}`;
const threadsApiUrl = getThreadsApiUrl();
let store;
let axiosMock;
let container;
async function renderComponent(editExisting = false, location = `/${courseId}/posts/`) {
const paths = editExisting ? ROUTES.POSTS.EDIT_POST : [ROUTES.POSTS.NEW_POST];
const wrapper = await render(
<IntlProvider locale="en">
<AppProvider store={store} wrapWithRouter={false}>
<DiscussionContext.Provider
value={{ courseId, category: null }}
>
<MemoryRouter initialEntries={[location]}>
<Routes>
{paths.map((path) => (
<Route path={path} element={<PostEditor editExisting={editExisting} />} />
))}
</Routes>
</MemoryRouter>
</DiscussionContext.Provider>
</AppProvider>
</IntlProvider>,
);
container = wrapper.container;
}
describe('PostEditor', () => {
beforeEach(async () => {
initializeMockApp({
authenticatedUser: {
userId: 3,
username: 'abc123',
administrator: true,
roles: [],
},
});
Factory.resetAll();
axiosMock = new MockAdapter(getAuthenticatedHttpClient());
const cwtopics = Factory.buildList('category', 2);
Factory.reset('topic');
axiosMock.onGet(topicsApiUrl).reply(200, {
courseware_topics: cwtopics,
non_courseware_topics: Factory.buildList('topic', 3, {}, { topicPrefix: 'ncw-' }),
});
});
describe.each([
{
allowAnonymous: false,
allowAnonymousToPeers: false,
},
{
allowAnonymous: false,
allowAnonymousToPeers: true,
},
{
allowAnonymous: true,
allowAnonymousToPeers: false,
},
{
allowAnonymous: true,
allowAnonymousToPeers: true,
},
])('anonymous posting', ({
allowAnonymous,
allowAnonymousToPeers,
}) => {
beforeEach(async () => {
store = initializeStore({
config: {
provider: 'legacy',
allowAnonymous,
allowAnonymousToPeers,
moderationSettings: {},
},
});
await executeThunk(fetchCourseTopics(courseId), store.dispatch, store.getState);
});
test(`new post when anonymous posts are ${allowAnonymous ? '' : 'not'} allowed and anonymous posts to peers are ${
allowAnonymousToPeers ? '' : 'not'} allowed`, async () => {
await renderComponent();
expect(screen.queryByRole('heading')).toHaveTextContent('Add a post');
expect(screen.queryAllByRole('radio')).toHaveLength(2);
// 2 categories with 4 subcategories each
expect(screen.queryAllByText(/category-\d-topic \d/)).toHaveLength(8);
// 3 non courseware topics
expect(screen.queryAllByText(/ncw-topic \d/)).toHaveLength(3);
expect(screen.queryByText('cohort', { exact: false })).not.toBeInTheDocument();
expect(screen.queryByText('Post anonymously')).not.toBeInTheDocument();
if (allowAnonymousToPeers) {
expect(screen.queryByText('Post anonymously to peers')).toBeInTheDocument();
} else {
expect(screen.queryByText('Post anonymously to peers')).not.toBeInTheDocument();
}
});
test('selectThread is not called while creating a new post', async () => {
const mockSelectThread = jest.fn();
jest.mock('../data/selectors', () => ({
selectThread: mockSelectThread,
}));
await renderComponent();
expect(mockSelectThread).not.toHaveBeenCalled();
});
});
describe('cohorting', () => {
const dividedncw = ['ncw-topic-2'];
const dividedcw = ['category-1-topic-2', 'category-2-topic-1', 'category-2-topic-2'];
beforeEach(async () => {
axiosMock.onGet(getCohortsApiUrl(courseId)).reply(200, Factory.buildList('cohort', 3));
});
async function setupData(config = {}, settings = {}) {
store = initializeStore({
config: {
provider: 'legacy',
userRoles: ['Student', 'Moderator'],
hasModerationPrivileges: true,
settings: {
dividedInlineDiscussions: dividedcw,
dividedCourseWideDiscussions: dividedncw,
...settings,
},
...config,
},
});
await executeThunk(fetchCourseTopics(courseId), store.dispatch, store.getState);
}
test('test privileged user', async () => {
await setupData();
await renderComponent();
// Initially the user can't select a cohort
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
// If a cohorted topic is selected, the cohort visibility selector is displayed
['ncw-topic 2', 'category-1-topic 2', 'category-2-topic 1', 'category-2-topic 2'].forEach(async (topicName) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', { name: /topic area/i }),
screen.getByRole('option', { name: topicName }),
);
});
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).toBeInTheDocument();
});
// Now if a non-cohorted topic is selected, the cohort visibility selector is hidden
['ncw-topic 1', 'category-1-topic 1', 'category-2-topic 4'].forEach(async (topicName) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', { name: /topic area/i }),
screen.getByRole('option', { name: topicName }),
);
});
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
});
});
test('test always divided inline', async () => {
await setupData({}, { alwaysDivideInlineDiscussions: true });
await renderComponent();
// Initially the user can't select a cohort
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
// All courseware topics are divided
[1, 2].forEach(catId => {
[1, 2, 3, 4].forEach((topicId) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', { name: /topic area/i }),
screen.getByRole('option', { name: `category-${catId}-topic ${topicId}` }),
);
});
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).toBeInTheDocument();
});
});
// Non-courseware topics can still have cohort visibility hidden
['ncw-topic 1', 'ncw-topic 3'].forEach((topicName) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', { name: /topic area/i }),
screen.getByRole('option', { name: topicName }),
);
});
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
});
});
test('test unprivileged user', async () => {
await setupData({ hasModerationPrivileges: false });
await renderComponent();
['ncw-topic 1', 'ncw-topic 2', 'category-1-topic 1', 'category-2-topic 1'].forEach((topicName) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', { name: /topic area/i }),
screen.getByRole('option', { name: topicName }),
);
});
// If a cohorted topic is selected, the cohort visibility selector is displayed
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
});
});
test('edit existing post should not show cohort selector to unprivileged users', async () => {
const threadId = 'thread-1';
await setupData({ hasModerationPrivileges: false });
await act(async () => {
axiosMock.onGet(`${threadsApiUrl}${threadId}/`).reply(200, Factory.build('thread'));
await executeThunk(fetchThread(threadId), store.dispatch, store.getState);
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
});
['ncw-topic 1', 'ncw-topic 2', 'category-1-topic 1', 'category-2-topic 1'].forEach((topicName) => {
act(() => {
userEvent.selectOptions(
screen.getByRole('combobox', {
name: /topic area/i,
}),
screen.getByRole('option', { name: topicName }),
);
});
// If a cohorted topic is selected, the cohort visibility selector is displayed
expect(screen.queryByRole('combobox', { name: /cohort visibility/i })).not.toBeInTheDocument();
});
});
test('cancel posting of existing post', async () => {
const threadId = 'thread-1';
await setupData({
editReasons: [
{
code: 'reason-1',
label: 'Reason 1',
},
{
code: 'reason-2',
label: 'Reason 2',
},
],
});
await act(async () => {
axiosMock.onGet(`${threadsApiUrl}${threadId}/`).reply(200, Factory.build('thread'));
await executeThunk(fetchThread(threadId), store.dispatch, store.getState);
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
});
const cancelButton = screen.getByRole('button', { name: /cancel/i });
await act(async () => {
fireEvent.click(cancelButton);
});
expect(screen.queryByRole('heading')).not.toBeInTheDocument();
});
});
describe('Edit codes', () => {
const threadId = 'thread-1';
beforeEach(async () => {
const dividedncw = ['ncw-topic-2'];
const dividedcw = ['category-1-topic-2', 'category-2-topic-1', 'category-2-topic-2'];
store = initializeStore({
config: {
provider: 'legacy',
hasModerationPrivileges: true,
editReasons: [
{
code: 'reason-1',
label: 'Reason 1',
},
{
code: 'reason-2',
label: 'Reason 2',
},
],
settings: {
dividedInlineDiscussions: dividedcw,
dividedCourseWideDiscussions: dividedncw,
},
},
});
await executeThunk(fetchCourseTopics(courseId), store.dispatch, store.getState);
axiosMock.onGet(`${threadsApiUrl}${threadId}/`).reply(200, Factory.build('thread'));
await executeThunk(fetchThread(threadId), store.dispatch, store.getState);
});
test('Edit post and see reasons', async () => {
await act(async () => {
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
});
expect(screen.queryByRole('combobox', { name: /reason for editing/i })).toBeInTheDocument();
expect(screen.getAllByRole('option', { name: /reason \d/i })).toHaveLength(2);
});
it('should show Preview Panel', async () => {
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="show-preview-button"]'));
});
await waitFor(() => {
expect(container.querySelector('[data-testid="hide-preview-button"]')).toBeInTheDocument();
expect(container.querySelector('[data-testid="show-preview-button"]')).not.toBeInTheDocument();
});
});
it('should hide Preview Panel', async () => {
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="show-preview-button"]'));
});
await waitFor(() => {
expect(container.querySelector('[data-testid="hide-preview-button"]')).toBeInTheDocument();
});
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="hide-preview-button"]'));
});
await waitFor(() => {
expect(container.querySelector('[data-testid="show-preview-button"]')).toBeInTheDocument();
expect(container.querySelector('[data-testid="hide-preview-button"]')).not.toBeInTheDocument();
});
});
it('should show Help Panel', async () => {
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="help-button"]'));
});
await waitFor(() => {
expect(container.querySelector('[data-testid="hide-help-button"]')).toBeInTheDocument();
});
});
it('should hide Help Panel', async () => {
await renderComponent(true, `/${courseId}/posts/${threadId}/edit`);
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="help-button"]'));
});
await act(async () => {
fireEvent.click(container.querySelector('[data-testid="hide-help-button"]'));
});
await waitFor(() => {
expect(container.querySelector('[data-testid="help-button"]')).toBeInTheDocument();
expect(container.querySelector('[data-testid="hide-help-button"]')).not.toBeInTheDocument();
});
});
});
});