Skip to content

Commit 157e246

Browse files
test: fix test code improperly using/missing 'await' (#2560)
1 parent c54c21e commit 157e246

49 files changed

Lines changed: 286 additions & 297 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

plugins/course-apps/ora_settings/Settings.test.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ describe('ORASettings', () => {
125125
});
126126

127127
it('Displays title, helper text and badge when flexible peer grading button is enabled', async () => {
128-
renderComponent();
129128
await mockStore({ apiStatus: 200, enabled: true });
129+
renderComponent();
130+
131+
const checkbox = await screen.getByRole('checkbox', { name: /Flex Peer Grading/ });
132+
expect(checkbox).toBeChecked();
130133

131-
waitFor(() => {
134+
await waitFor(() => {
132135
const label = screen.getByText(messages.enableFlexPeerGradeLabel.defaultMessage);
133136
const enableBadge = screen.getByTestId('enable-badge');
134137

src/advanced-settings/setting-card/SettingCard.test.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react';
21
import { fireEvent, render, waitFor } from '@testing-library/react';
32
import userEvent from '@testing-library/user-event';
43
import { IntlProvider } from '@edx/frontend-platform/i18n';
@@ -22,7 +21,6 @@ jest.mock('react-textarea-autosize', () => jest.fn((props) => (
2221
<textarea
2322
{...props}
2423
onFocus={() => {}}
25-
onBlur={() => {}}
2624
/>
2725
)));
2826

@@ -86,10 +84,10 @@ describe('<SettingCard />', () => {
8684
await waitFor(() => {
8785
expect(inputBox).toHaveValue('3, 2, 1');
8886
});
89-
await (async () => {
87+
await user.tab(); // blur off of the input.
88+
await waitFor(() => {
9089
expect(setEdited).toHaveBeenCalled();
9190
expect(handleBlur).toHaveBeenCalled();
9291
});
93-
fireEvent.focusOut(inputBox);
9492
});
9593
});

src/advanced-settings/setting-card/SettingCard.jsx renamed to src/advanced-settings/setting-card/SettingCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const SettingCard = ({
3030
const { deprecated, help, displayName } = settingData;
3131
const initialValue = JSON.stringify(settingData.value, null, 4);
3232
const [isOpen, open, close] = useToggle(false);
33-
const [target, setTarget] = useState(null);
33+
const [target, setTarget] = useState<HTMLButtonElement | null>(null);
3434
const [newValue, setNewValue] = useState(initialValue);
3535

3636
const handleSettingChange = (e) => {
@@ -118,7 +118,7 @@ SettingCard.propTypes = {
118118
deprecated: PropTypes.bool,
119119
help: PropTypes.string,
120120
displayName: PropTypes.string,
121-
value: PropTypes.PropTypes.oneOfType([
121+
value: PropTypes.oneOfType([
122122
PropTypes.string,
123123
PropTypes.bool,
124124
PropTypes.number,

src/certificates/certificate-details/CertificateDetailsForm.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('CertificateDetails', () => {
6565

6666
await user.type(input, newInputValue);
6767

68-
waitFor(() => {
68+
await waitFor(() => {
6969
expect(input.value).toBe(newInputValue);
7070
});
7171
});

src/certificates/certificate-signatories/CertificateSignatories.test.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('CertificateSignatories', () => {
9696
expect(useCreateSignatory().handleAddSignatory).toHaveBeenCalled();
9797
});
9898

99-
it('calls remove for the correct signatory when delete icon is clicked', async () => {
99+
it.skip('calls remove for the correct signatory when delete icon is clicked', async () => {
100100
const user = userEvent.setup();
101101
const { getAllByRole } = renderComponent(defaultProps);
102102

@@ -105,7 +105,9 @@ describe('CertificateSignatories', () => {
105105

106106
await user.click(deleteIcons[0]);
107107

108-
waitFor(() => {
108+
// FIXME: this isn't called because the whole 'useEditSignatory' hook
109+
// which calls it is mocked out.
110+
await waitFor(() => {
109111
expect(mockArrayHelpers.remove).toHaveBeenCalledWith(0);
110112
});
111113
});

src/certificates/certificate-signatories/signatory/SignatoryForm.test.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, waitFor } from '@testing-library/react';
1+
import { render, screen, waitFor } from '@testing-library/react';
22
import { Provider } from 'react-redux';
33
import userEvent from '@testing-library/user-event';
44
import { IntlProvider } from '@edx/frontend-platform/i18n';
@@ -30,6 +30,7 @@ const initialState = {
3030
};
3131

3232
const defaultProps = {
33+
index: 0,
3334
...signatoriesMock[0],
3435
showDeleteButton: true,
3536
isEdit: true,
@@ -62,15 +63,20 @@ describe('Signatory Component', () => {
6263
it('handles input change', async () => {
6364
const user = userEvent.setup();
6465
const handleChange = jest.fn();
65-
const { getByPlaceholderText } = renderSignatory({ ...defaultProps, handleChange });
66-
const input = getByPlaceholderText(messages.namePlaceholder.defaultMessage);
66+
renderSignatory({ ...defaultProps, handleChange });
67+
const input = screen.getByPlaceholderText(messages.namePlaceholder.defaultMessage);
6768
const newInputValue = 'Jane Doe';
6869

69-
await user.type(input, newInputValue, { name: 'signatories[0].name' });
70+
expect(handleChange).not.toHaveBeenCalled();
71+
expect(input.value).not.toBe(newInputValue);
72+
await user.type(input, newInputValue);
7073

71-
waitFor(() => {
72-
expect(handleChange).toHaveBeenCalledWith(expect.anything());
73-
expect(input.value).toBe(newInputValue);
74+
await waitFor(() => {
75+
// This is not a great test; handleChange() gets called for each key press:
76+
expect(handleChange).toHaveBeenCalledTimes(newInputValue.length);
77+
// And the input value never actually changes because it's a controlled component
78+
// and we pass the name in as a prop, which hasn't changed.
79+
// expect(input.value).toBe(newInputValue);
7480
});
7581
});
7682

src/certificates/layout/header-buttons/HeaderButtons.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('HeaderButtons Component', () => {
6262
const dropdownButton = getByRole('button', { name: certificatesDataMock.courseModes[0] });
6363
await user.click(dropdownButton);
6464

65-
const verifiedMode = await getByRole('button', { name: certificatesDataMock.courseModes[1] });
65+
const verifiedMode = getByRole('button', { name: certificatesDataMock.courseModes[1] });
6666
await user.click(verifiedMode);
6767

6868
await waitFor(() => {

src/course-libraries/CourseLibraries.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('<CourseLibraries />', () => {
114114
const dismissBtn = await screen.findByRole('button', { name: 'Dismiss' });
115115
await user.click(dismissBtn);
116116
expect(allTab).toHaveAttribute('aria-selected', 'true');
117-
waitFor(() => expect(alert).not.toBeInTheDocument());
117+
await waitFor(() => expect(alert).not.toBeInTheDocument());
118118
// review updates button
119119
const reviewActionBtn = await screen.findByRole('button', { name: 'Review Updates' });
120120
await user.click(reviewActionBtn);

src/course-outline/OutlineAddChildButtons.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jest.mock('react-redux', () => ({
3333
expect(newBtn).toBeInTheDocument();
3434
const useBtn = await screen.findByRole('button', { name: `Use ${containerType} from library` });
3535
expect(useBtn).toBeInTheDocument();
36-
userEvent.click(newBtn);
37-
waitFor(() => expect(newClickHandler).toHaveBeenCalled());
38-
userEvent.click(useBtn);
39-
waitFor(() => expect(useFromLibClickHandler).toHaveBeenCalled());
36+
await userEvent.click(newBtn);
37+
await waitFor(() => expect(newClickHandler).toHaveBeenCalled());
38+
await userEvent.click(useBtn);
39+
await waitFor(() => expect(useFromLibClickHandler).toHaveBeenCalled());
4040
});
4141
});
4242
});

src/course-unit/CourseUnit.test.jsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ describe('<CourseUnit />', () => {
22182218
const currentSubSectionName = courseSectionVerticalMock.xblock_info.ancestor_info.ancestors[1].display_name;
22192219
const helpLinkUrl = 'https://edx.readthedocs.io/projects/open-edx-building-and-running-a-course/en/latest/developing_course/course_components.html#components-that-contain-other-components';
22202220

2221-
waitFor(() => {
2221+
await waitFor(() => {
22222222
const unitHeaderTitle = screen.getByTestId('unit-header-title');
22232223
expect(screen.getByText(unitDisplayName)).toBeInTheDocument();
22242224
expect(within(unitHeaderTitle).getByRole('button', { name: headerTitleMessages.altButtonEdit.defaultMessage })).toBeInTheDocument();
@@ -2291,41 +2291,38 @@ describe('<CourseUnit />', () => {
22912291
});
22922292

22932293
it('renders and navigates to the new HTML XBlock editor after xblock duplicating', async () => {
2294-
render(<RootWrapper />);
22952294
const updatedCourseVerticalChildrenMock = JSON.parse(JSON.stringify(courseVerticalChildrenMock));
2296-
const targetBlockId = updatedCourseVerticalChildrenMock.children[1].block_id;
2297-
2298-
updatedCourseVerticalChildrenMock.children = updatedCourseVerticalChildrenMock.children
2299-
.map((child) => (child.block_id === targetBlockId
2300-
? { ...child, block_type: 'html' }
2301-
: child));
2295+
// Convert the second child from drag and drop to HTML:
2296+
const targetChild = updatedCourseVerticalChildrenMock.children[1];
2297+
targetChild.block_type = 'html';
2298+
targetChild.name = 'Test HTML Block';
2299+
targetChild.block_id = 'block-v1:OpenedX+L153+3T2023+type@html+block@test123original';
23022300

23032301
axiosMock
23042302
.onPost(postXBlockBaseApiUrl({
23052303
parent_locator: blockId,
2306-
duplicate_source_locator: courseVerticalChildrenMock.children[0].block_id,
2304+
duplicate_source_locator: targetChild.block_id,
23072305
}))
23082306
.replyOnce(200, { locator: '1234567890' });
23092307

23102308
axiosMock
23112309
.onGet(getCourseVerticalChildrenApiUrl(blockId))
23122310
.reply(200, updatedCourseVerticalChildrenMock);
23132311

2312+
render(<RootWrapper />);
23142313
await executeThunk(fetchCourseVerticalChildrenData(blockId), store.dispatch);
23152314

23162315
await waitFor(() => {
23172316
const iframe = screen.getByTitle(xblockContainerIframeMessages.xblockIframeTitle.defaultMessage);
23182317
expect(iframe).toBeInTheDocument();
2319-
simulatePostMessageEvent(messageTypes.currentXBlockId, {
2320-
id: targetBlockId,
2321-
});
23222318
});
23232319

2324-
waitFor(() => {
2325-
simulatePostMessageEvent(messageTypes.duplicateXBlock, {});
2326-
simulatePostMessageEvent(messageTypes.newXBlockEditor, {});
2327-
expect(mockedUsedNavigate)
2328-
.toHaveBeenCalledWith(`/course/${courseId}/editor/html/${targetBlockId}`, { replace: true });
2320+
// After duplicating, the editor modal will open:
2321+
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
2322+
simulatePostMessageEvent(messageTypes.duplicateXBlock, { usageId: targetChild.block_id });
2323+
simulatePostMessageEvent(messageTypes.newXBlockEditor, { blockType: 'html', usageId: targetChild.block_id });
2324+
await waitFor(() => {
2325+
expect(screen.queryByRole('dialog')).toBeInTheDocument();
23292326
});
23302327
});
23312328

0 commit comments

Comments
 (0)