Skip to content

Commit c86e751

Browse files
committed
refactor: updated DiscussionSettings tests
1 parent 15ddf21 commit c86e751

File tree

1 file changed

+66
-57
lines changed

1 file changed

+66
-57
lines changed

src/pages-and-resources/discussions/DiscussionsSettings.test.jsx

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,20 @@ describe('DiscussionsSettings', () => {
196196
// content has been loaded - prior to proceeding with our expectations.
197197
await waitForElementToBeRemoved(screen.getByRole('status'));
198198

199-
userEvent.click(queryByLabelText(container, 'Select Piazza'));
199+
act(async () => {
200+
userEvent.click(queryByLabelText(container, 'Select Piazza'));
200201

201-
userEvent.click(getByRole(container, 'button', { name: 'Next' }));
202+
userEvent.click(getByRole(container, 'button', { name: 'Next' }));
202203

203-
userEvent.click(await findByRole(container, 'button', { name: 'Save' }));
204+
userEvent.click(await findByRole(container, 'button', { name: 'Save' }));
204205

205-
// This is an important line that ensures the Close button has been removed, which implies that
206-
// the full screen modal has been closed following our click of Apply. Once this has happened,
207-
// then it's safe to proceed with our expectations.
208-
await waitFor(() => expect(screen.queryByRole(container, 'button', { name: 'Close' })).toBeNull());
206+
// This is an important line that ensures the Close button has been removed, which implies that
207+
// the full screen modal has been closed following our click of Apply. Once this has happened,
208+
// then it's safe to proceed with our expectations.
209+
await waitFor(() => expect(screen.queryByRole(container, 'button', { name: 'Close' })).toBeNull());
209210

210-
setTimeout(() => expect(window.location.pathname).toEqual(`/course/${courseId}/pages-and-resources`), 0);
211+
await waitFor(() => expect(window.location.pathname).toEqual(`/course/${courseId}/pages-and-resources`));
212+
});
211213
});
212214

213215
test('requires confirmation if changing provider', async () => {
@@ -219,16 +221,18 @@ describe('DiscussionsSettings', () => {
219221
// content has been loaded - prior to proceeding with our expectations.
220222
await waitForElementToBeRemoved(screen.getByRole('status'));
221223

222-
userEvent.click(getByRole(container, 'checkbox', { name: 'Select Discourse' }));
223-
userEvent.click(getByRole(container, 'button', { name: 'Next' }));
224+
act(async () => {
225+
userEvent.click(getByRole(container, 'checkbox', { name: 'Select Discourse' }));
226+
userEvent.click(getByRole(container, 'button', { name: 'Next' }));
224227

225-
await findByRole(container, 'button', { name: 'Save' });
226-
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'key');
227-
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
228-
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
229-
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
228+
await findByRole(container, 'button', { name: 'Save' });
229+
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'key');
230+
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
231+
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
232+
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
230233

231-
setTimeout(() => expect(screen.getByRole(container, 'dialog', { name: 'OK' })).toBeInTheDocument(), 5000);
234+
await waitFor(() => expect(queryByRole(container, 'dialog', { name: 'OK' })).toBeInTheDocument());
235+
});
232236
});
233237

234238
test('can cancel confirmation', async () => {
@@ -248,20 +252,20 @@ describe('DiscussionsSettings', () => {
248252

249253
await waitFor(() => expect(screen.queryByRole('status')).toBeNull());
250254

251-
expect(getByRole(container, 'heading', { name: 'Discourse' })).toBeInTheDocument();
255+
act(async () => {
256+
expect(await findByRole(container, 'heading', { name: 'Discourse' })).toBeInTheDocument();
252257

253-
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'a');
254-
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
255-
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
256-
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
258+
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'a');
259+
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
260+
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
261+
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
257262

258-
setTimeout(() => {
259263
waitFor(() => expect(getByRole(container, 'dialog', { name: 'OK' })).toBeInTheDocument());
260264
userEvent.click(getByRole(container, 'button', { name: 'Cancel' }));
261265

262266
expect(queryByRole(container, 'dialog', { name: 'Confirm' })).not.toBeInTheDocument();
263267
expect(queryByRole(container, 'dialog', { name: 'Configure discussion' }));
264-
}, 4000);
268+
});
265269
});
266270
});
267271

@@ -316,18 +320,16 @@ describe('DiscussionsSettings', () => {
316320
await waitForElementToBeRemoved(screen.getByRole('status'));
317321

318322
// Apply causes an async action to take place
319-
await act(() => {
323+
act(async () => {
320324
userEvent.click(queryByText(container, appMessages.saveButton.defaultMessage));
321-
});
325+
await waitFor(() => expect(axiosMock.history.post.length).toBe(1));
322326

323-
setTimeout(() => {
324-
expect(axiosMock.history.post.length).toBe(1);
325327
expect(queryByTestId(container, 'appConfigForm')).toBeInTheDocument();
326-
const alert = findByRole(container, 'alert');
328+
const alert = await findByRole(container, 'alert');
327329
expect(alert).toBeInTheDocument();
328330
expect(alert.textContent).toEqual(expect.stringContaining('We encountered a technical error when applying changes.'));
329331
expect(alert.innerHTML).toEqual(expect.stringContaining(getConfig().SUPPORT_URL));
330-
}, 4000);
332+
});
331333
});
332334
});
333335

@@ -369,20 +371,21 @@ describe('DiscussionsSettings', () => {
369371
// content has been loaded - prior to proceeding with our expectations.
370372
await waitForElementToBeRemoved(screen.getByRole('status'));
371373

372-
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
374+
act(async () => {
375+
userEvent.click(getByRole(container, 'button', { name: 'Save' }));
376+
377+
await waitFor(() => expect(axiosMock.history.post.length).toBe(1));
373378

374-
setTimeout(() => {
375-
expect(axiosMock.history.post.length).toBe(1);
376379
expect(queryByTestId(container, 'appList')).not.toBeInTheDocument();
377380
expect(queryByTestId(container, 'appConfigForm')).not.toBeInTheDocument();
378381

379382
// We don't technically leave the route in this case, though the modal is hidden.
380383
expect(window.location.pathname).toEqual(`/course/${courseId}/pages-and-resources/discussion/configure/piazza`);
381384

382-
const alert = findByRole(container, 'alert');
385+
const alert = await findByRole(container, 'alert');
383386
expect(alert).toBeInTheDocument();
384387
expect(alert.textContent).toEqual(expect.stringContaining('You are not authorized to view this page.'));
385-
}, 3000);
388+
});
386389
});
387390
});
388391
});
@@ -427,19 +430,21 @@ describe.each([
427430

428431
// This is an important line that ensures the spinner has been removed - and thus our main
429432
// content has been loaded - prior to proceeding with our expectations.
430-
await waitForElementToBeRemoved(screen.getByRole('status'));
431-
432-
userEvent.click(queryByLabelText(container, 'Select Piazza'));
433-
userEvent.click(queryByText(container, messages.nextButton.defaultMessage));
434-
await waitForElementToBeRemoved(screen.getByRole('status'));
435-
436-
if (showLTIConfig) {
437-
expect(queryByText(container, ltiMessages.formInstructions.defaultMessage)).toBeInTheDocument();
438-
expect(queryByTestId(container, 'ltiConfigFields')).toBeInTheDocument();
439-
} else {
440-
expect(queryByText(container, ltiMessages.formInstructions.defaultMessage)).not.toBeInTheDocument();
441-
expect(queryByTestId(container, 'ltiConfigFields')).not.toBeInTheDocument();
442-
}
433+
waitForElementToBeRemoved(screen.getByRole('status'));
434+
435+
act(async () => {
436+
userEvent.click(await screen.findByLabelText('Select Piazza'));
437+
userEvent.click(queryByText(container, messages.nextButton.defaultMessage));
438+
waitForElementToBeRemoved(screen.getByRole('status'));
439+
440+
if (showLTIConfig) {
441+
expect(queryByText(container, ltiMessages.formInstructions.defaultMessage)).toBeInTheDocument();
442+
expect(queryByTestId(container, 'ltiConfigFields')).toBeInTheDocument();
443+
} else {
444+
expect(queryByText(container, ltiMessages.formInstructions.defaultMessage)).not.toBeInTheDocument();
445+
expect(queryByTestId(container, 'ltiConfigFields')).not.toBeInTheDocument();
446+
}
447+
});
443448
});
444449
});
445450

@@ -483,15 +488,19 @@ describe.each([
483488

484489
// This is an important line that ensures the spinner has been removed - and thus our main
485490
// content has been loaded - prior to proceeding with our expectations.
486-
await waitForElementToBeRemoved(screen.getByRole('status'));
487-
488-
userEvent.click(queryByLabelText(container, 'Select Piazza'));
489-
userEvent.click(queryByText(container, messages.nextButton.defaultMessage));
490-
await waitForElementToBeRemoved(screen.getByRole('status'));
491-
if (enablePIISharing) {
492-
expect(queryByTestId(container, 'piiSharingFields')).toBeInTheDocument();
493-
} else {
494-
expect(queryByTestId(container, 'piiSharingFields')).not.toBeInTheDocument();
495-
}
491+
waitForElementToBeRemoved(screen.getByRole('status'));
492+
493+
act(async () => {
494+
userEvent.click(await screen.findByLabelText('Select Piazza'));
495+
userEvent.click(await screen.findByText(messages.nextButton.defaultMessage));
496+
497+
waitForElementToBeRemoved(screen.getByRole('status'));
498+
if (enablePIISharing) {
499+
expect(queryByTestId(container, 'piiSharingFields')).toBeInTheDocument();
500+
} else {
501+
expect(queryByTestId(container, 'piiSharingFields')).not.toBeInTheDocument();
502+
}
503+
});
496504
});
497505
});
506+

0 commit comments

Comments
 (0)