Skip to content

Commit 517b842

Browse files
test: Deprecate react-unit-test-utils 13/15 (#678)
1 parent 49e527d commit 517b842

File tree

20 files changed

+177
-244
lines changed

20 files changed

+177
-244
lines changed

src/App.test.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ jest.mock('@edx/frontend-platform', () => ({
3232
getConfig: jest.fn(() => ({})),
3333
}));
3434

35-
jest.unmock('@openedx/paragon');
36-
jest.unmock('@edx/frontend-platform/i18n');
37-
jest.unmock('react');
35+
jest.mock('@edx/frontend-platform/react', () => ({
36+
...jest.requireActual('@edx/frontend-platform/react'),
37+
ErrorPage: () => 'ErrorPage',
38+
}));
3839

3940
const loadData = jest.fn();
4041
reduxHooks.useLoadData.mockReturnValue(loadData);

src/components/NoticesWrapper/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { getConfig } from '@edx/frontend-platform';
3-
import { useIntl } from 'react-intl';
3+
import { useIntl } from '@edx/frontend-platform/i18n';
44

55
import { StrictDict } from 'utils';
66
import { getNotices } from './api';

src/components/NoticesWrapper/hooks.test.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import React from 'react';
22

3-
import { MockUseState } from 'testUtils';
3+
import { MockUseState, formatMessage } from 'testUtils';
44

55
import { getConfig } from '@edx/frontend-platform';
66
import { getNotices } from './api';
77
import * as hooks from './hooks';
88

99
jest.mock('@edx/frontend-platform', () => ({ getConfig: jest.fn() }));
1010
jest.mock('./api', () => ({ getNotices: jest.fn() }));
11-
const mockFormatMessage = jest.fn(message => message.defaultMessage || 'translated-string');
12-
jest.mock('react-intl', () => ({
13-
useIntl: () => ({
14-
formatMessage: mockFormatMessage,
15-
}),
11+
12+
jest.mock('react', () => ({
13+
...jest.requireActual('react'),
14+
useEffect: jest.fn((cb, prereqs) => ({ useEffect: { cb, prereqs } })),
15+
useContext: jest.fn(context => context),
1616
}));
1717

18+
jest.mock('@edx/frontend-platform/i18n', () => {
19+
const { formatMessage: fn } = jest.requireActual('testUtils');
20+
return {
21+
...jest.requireActual('@edx/frontend-platform/i18n'),
22+
useIntl: () => ({
23+
formatMessage: fn,
24+
}),
25+
};
26+
});
27+
1828
getConfig.mockReturnValue({ ENABLE_NOTICES: true });
1929
const state = new MockUseState(hooks);
2030

@@ -40,7 +50,7 @@ describe('NoticesWrapper hooks', () => {
4050
getConfig.mockReturnValueOnce({ ENABLE_NOTICES: false });
4151
hooks.useNoticesWrapperData();
4252
const [cb, prereqs] = React.useEffect.mock.calls[0];
43-
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
53+
expect(prereqs).toEqual([state.setState.isRedirected, formatMessage]);
4454
cb();
4555
expect(getNotices).not.toHaveBeenCalled();
4656
});
@@ -49,7 +59,7 @@ describe('NoticesWrapper hooks', () => {
4959
hooks.useNoticesWrapperData();
5060
expect(React.useEffect).toHaveBeenCalled();
5161
const [cb, prereqs] = React.useEffect.mock.calls[0];
52-
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
62+
expect(prereqs).toEqual([state.setState.isRedirected, formatMessage]);
5363
cb();
5464
expect(getNotices).toHaveBeenCalled();
5565
const { onLoad } = getNotices.mock.calls[0][0];
@@ -65,7 +75,7 @@ describe('NoticesWrapper hooks', () => {
6575
window.location = { replace: jest.fn(), href: 'test-old-href' };
6676
hooks.useNoticesWrapperData();
6777
const [cb, prereqs] = React.useEffect.mock.calls[0];
68-
expect(prereqs).toEqual([state.setState.isRedirected, mockFormatMessage]);
78+
expect(prereqs).toEqual([state.setState.isRedirected, formatMessage]);
6979
cb();
7080
expect(getNotices).toHaveBeenCalled();
7181
const { onLoad } = getNotices.mock.calls[0][0];

src/containers/CourseCard/components/CourseCardActions/ActionButton/hooks.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import { useWindowSize, breakpoints } from '@openedx/paragon';
22
import useIsCollapsed from './hooks';
33

4+
jest.mock('@openedx/paragon', () => ({
5+
...jest.requireActual('@openedx/paragon'),
6+
useWindowSize: jest.fn(),
7+
breakpoints: {
8+
extraSmall: {
9+
minWidth: 0,
10+
maxWidth: 575,
11+
},
12+
small: {
13+
minWidth: 576,
14+
maxWidth: 767,
15+
},
16+
medium: {
17+
minWidth: 768,
18+
maxWidth: 991,
19+
},
20+
large: {
21+
minWidth: 992,
22+
maxWidth: 1199,
23+
},
24+
extraLarge: {
25+
minWidth: 1200,
26+
maxWidth: 100000,
27+
},
28+
},
29+
}));
30+
431
describe('useIsCollapsed', () => {
532
it('returns true only when it is between medium and small', () => {
633
// make sure all three breakpoints gap is large enough for test

src/containers/CourseCard/components/CourseCardActions/BeginCourseButton.test.jsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
34
import { reduxHooks } from 'hooks';
45
import track from 'tracking';
56
import useActionDisabledState from '../hooks';
67
import BeginCourseButton from './BeginCourseButton';
78

8-
jest.unmock('@openedx/paragon');
9-
109
jest.mock('tracking', () => ({
1110
course: {
1211
enterCourseClicked: jest.fn().mockName('segment.enterCourseClicked'),
@@ -33,46 +32,49 @@ reduxHooks.useTrackCourseEvent.mockImplementation(
3332
(eventName, cardId, url) => ({ trackCourseEvent: { eventName, cardId, url } }),
3433
);
3534

35+
const props = {
36+
cardId: 'cardId',
37+
};
38+
39+
const renderComponent = () => render(<IntlProvider locale="en"><BeginCourseButton {...props} /></IntlProvider>);
40+
3641
describe('BeginCourseButton', () => {
37-
const props = {
38-
cardId: 'cardId',
39-
};
4042
beforeEach(() => {
4143
jest.clearAllMocks();
4244
});
4345
describe('initiliaze hooks', () => {
4446
it('initializes course run data with cardId', () => {
45-
render(<BeginCourseButton {...props} />);
47+
renderComponent();
4648
expect(reduxHooks.useCardCourseRunData).toHaveBeenCalledWith(props.cardId);
4749
});
4850
it('loads exec education path param', () => {
49-
render(<BeginCourseButton {...props} />);
51+
renderComponent();
5052
expect(reduxHooks.useCardExecEdTrackingParam).toHaveBeenCalledWith(props.cardId);
5153
});
5254
it('loads disabled states for begin action from action hooks', () => {
53-
render(<BeginCourseButton {...props} />);
55+
renderComponent();
5456
expect(useActionDisabledState).toHaveBeenCalledWith(props.cardId);
5557
});
5658
});
5759
describe('behavior', () => {
5860
describe('disabled', () => {
5961
it('should be disabled', () => {
6062
useActionDisabledState.mockReturnValueOnce({ disableBeginCourse: true });
61-
render(<BeginCourseButton {...props} />);
63+
renderComponent();
6264
const button = screen.getByRole('button', { name: 'Begin Course' });
6365
expect(button).toHaveClass('disabled');
6466
expect(button).toHaveAttribute('aria-disabled', 'true');
6567
});
6668
});
6769
describe('enabled', () => {
6870
it('should be enabled', () => {
69-
render(<BeginCourseButton {...props} />);
71+
renderComponent();
7072
const button = screen.getByRole('button', { name: 'Begin Course' });
7173
expect(button).not.toHaveClass('disabled');
7274
expect(button).not.toHaveAttribute('aria-disabled', 'true');
7375
});
7476
it('should track enter course clicked event on click, with exec ed param', async () => {
75-
render(<BeginCourseButton {...props} />);
77+
renderComponent();
7678
const user = userEvent.setup();
7779
const button = screen.getByRole('button', { name: 'Begin Course' });
7880
user.click(button);

src/containers/CourseCard/components/CourseCardActions/ResumeButton.test.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
34

45
import { reduxHooks } from 'hooks';
56
import track from 'tracking';
@@ -20,7 +21,7 @@ jest.mock('hooks', () => ({
2021
},
2122
}));
2223
jest.mock('../hooks', () => jest.fn(() => ({ disableResumeCourse: false })));
23-
jest.unmock('@openedx/paragon');
24+
2425
jest.mock('./ActionButton/hooks', () => jest.fn(() => false));
2526

2627
const resumeUrl = 'resume-url';
@@ -36,7 +37,7 @@ describe('ResumeButton', () => {
3637
cardId: 'cardId',
3738
};
3839
describe('initialize hooks', () => {
39-
beforeEach(() => render(<ResumeButton {...props} />));
40+
beforeEach(() => render(<IntlProvider locale="en"><ResumeButton {...props} /></IntlProvider>));
4041
it('initializes course run data with cardId', () => {
4142
expect(reduxHooks.useCardCourseRunData).toHaveBeenCalledWith(props.cardId);
4243
});
@@ -53,22 +54,22 @@ describe('ResumeButton', () => {
5354
useActionDisabledState.mockReturnValueOnce({ disableResumeCourse: true });
5455
});
5556
it('should be disabled', () => {
56-
render(<ResumeButton {...props} />);
57+
render(<IntlProvider locale="en"><ResumeButton {...props} /></IntlProvider>);
5758
const button = screen.getByRole('button', { name: 'Resume' });
5859
expect(button).toHaveClass('disabled');
5960
expect(button).toHaveAttribute('aria-disabled', 'true');
6061
});
6162
});
6263
describe('enabled', () => {
6364
it('should be enabled', () => {
64-
render(<ResumeButton {...props} />);
65+
render(<IntlProvider locale="en"><ResumeButton {...props} /></IntlProvider>);
6566
const button = screen.getByRole('button', { name: 'Resume' });
6667
expect(button).toBeInTheDocument();
6768
expect(button).not.toHaveClass('disabled');
6869
expect(button).not.toHaveAttribute('aria-disabled', 'true');
6970
});
7071
it('should track enter course clicked event on click, with exec ed param', async () => {
71-
render(<ResumeButton {...props} />);
72+
render(<IntlProvider locale="en"><ResumeButton {...props} /></IntlProvider>);
7273
const user = userEvent.setup();
7374
const button = screen.getByRole('button', { name: 'Resume' });
7475
user.click(button);

src/containers/CourseCard/components/CourseCardActions/SelectSessionButton.test.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
34

45
import { reduxHooks } from 'hooks';
56
import useActionDisabledState from '../hooks';
@@ -13,27 +14,26 @@ jest.mock('hooks', () => ({
1314
}));
1415
jest.mock('../hooks', () => jest.fn(() => ({ disableSelectSession: false })));
1516

16-
jest.unmock('@openedx/paragon');
1717
jest.mock('./ActionButton/hooks', () => jest.fn(() => false));
1818

1919
describe('SelectSessionButton', () => {
2020
const props = { cardId: 'cardId' };
2121
it('default render', () => {
22-
render(<SelectSessionButton {...props} />);
22+
render(<IntlProvider locale="en"><SelectSessionButton {...props} /></IntlProvider>);
2323
const button = screen.getByRole('button', { name: 'Select Session' });
2424
expect(button).toBeInTheDocument();
2525
});
2626
describe('if useActionDisabledState is false', () => {
2727
it('should disabled Select Session', () => {
2828
useActionDisabledState.mockReturnValueOnce({ disableSelectSession: true });
29-
render(<SelectSessionButton {...props} />);
29+
render(<IntlProvider locale="en"><SelectSessionButton {...props} /></IntlProvider>);
3030
const button = screen.getByRole('button', { name: 'Select Session' });
3131
expect(button).toBeDisabled();
3232
});
3333
});
3434
describe('on click', () => {
3535
it('should call openSessionModal', async () => {
36-
render(<SelectSessionButton {...props} />);
36+
render(<IntlProvider locale="en"><SelectSessionButton {...props} /></IntlProvider>);
3737
const user = userEvent.setup();
3838
const button = screen.getByRole('button', { name: 'Select Session' });
3939
await user.click(button);

src/containers/CourseCard/components/CourseCardActions/ViewCourseButton.test.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
34

45
import track from 'tracking';
56
import { reduxHooks } from 'hooks';
@@ -20,22 +21,21 @@ jest.mock('hooks', () => ({
2021
}));
2122
jest.mock('../hooks', () => jest.fn(() => ({ disableViewCourse: false })));
2223

23-
jest.unmock('@openedx/paragon');
2424
jest.mock('./ActionButton/hooks', () => jest.fn(() => false));
2525

2626
const defaultProps = { cardId: 'cardId' };
2727
const homeUrl = 'homeUrl';
2828

2929
describe('ViewCourseButton', () => {
3030
it('learner can view course', async () => {
31-
render(<ViewCourseButton {...defaultProps} />);
31+
render(<IntlProvider locale="en"><ViewCourseButton {...defaultProps} /></IntlProvider>);
3232
const button = screen.getByRole('button', { name: 'View Course' });
3333
expect(button).toBeInTheDocument();
3434
expect(button).not.toHaveClass('disabled');
3535
expect(button).not.toHaveAttribute('aria-disabled', 'true');
3636
});
3737
it('calls trackCourseEvent on click', async () => {
38-
render(<ViewCourseButton {...defaultProps} />);
38+
render(<IntlProvider locale="en"><ViewCourseButton {...defaultProps} /></IntlProvider>);
3939
const user = userEvent.setup();
4040
const button = screen.getByRole('button', { name: 'View Course' });
4141
await user.click(button);
@@ -47,7 +47,7 @@ describe('ViewCourseButton', () => {
4747
});
4848
it('learner cannot view course', () => {
4949
useActionDisabledState.mockReturnValueOnce({ disableViewCourse: true });
50-
render(<ViewCourseButton {...defaultProps} />);
50+
render(<IntlProvider locale="en"><ViewCourseButton {...defaultProps} /></IntlProvider>);
5151
const button = screen.getByRole('button', { name: 'View Course' });
5252
expect(button).toHaveClass('disabled');
5353
expect(button).toHaveAttribute('aria-disabled', 'true');

src/containers/CourseCard/components/CourseCardBanners/CreditBanner/views/components/CreditRequestForm/hooks.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import useCreditRequestFormData from './hooks';
44

55
const requestData = 'test-request-data';
66

7+
jest.mock('react', () => ({
8+
...jest.requireActual('react'),
9+
useRef: jest.fn((val) => ({ current: val, useRef: true })),
10+
useEffect: jest.fn((cb, prereqs) => ({ useEffect: { cb, prereqs } })),
11+
}));
12+
713
let out;
814
const ref = {
915
current: { click: jest.fn() },

src/containers/CourseCard/components/CourseCardDetails/hooks.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ jest.mock('hooks', () => ({
2020
},
2121
}));
2222

23+
jest.mock('@edx/frontend-platform/i18n', () => {
24+
const { formatMessage } = jest.requireActual('testUtils');
25+
return {
26+
...jest.requireActual('@edx/frontend-platform/i18n'),
27+
useIntl: () => ({
28+
formatMessage,
29+
}),
30+
};
31+
});
32+
2333
const cardId = 'my-test-card-id';
2434
const courseNumber = 'test-course-number';
2535
const useAccessMessage = 'test-access-message';

0 commit comments

Comments
 (0)