Skip to content

Commit cbd16b4

Browse files
diana-villalvazo-wguandrey-canon
authored andcommitted
test: Deprecate react-unit-test-utils 11/15 (openedx#670)
1 parent 98ac95f commit cbd16b4

File tree

6 files changed

+163
-393
lines changed

6 files changed

+163
-393
lines changed

src/App.test.jsx

Lines changed: 52 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
import React from 'react';
2-
import { Helmet } from 'react-helmet';
3-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render, screen, waitFor } from '@testing-library/react';
42

5-
import { useIntl } from '@edx/frontend-platform/i18n';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
64
import { getConfig } from '@edx/frontend-platform';
75

86
import { RequestKeys } from 'data/constants/requests';
97
import { reduxHooks } from 'hooks';
10-
import Dashboard from 'containers/Dashboard';
11-
import LearnerDashboardHeader from 'containers/LearnerDashboardHeader';
12-
import AppWrapper from 'containers/WidgetContainers/AppWrapper';
138
import { App } from './App';
149
import messages from './messages';
1510

16-
jest.mock('@edx/frontend-component-footer', () => ({ FooterSlot: 'FooterSlot' }));
17-
18-
jest.mock('containers/Dashboard', () => 'Dashboard');
19-
jest.mock('containers/LearnerDashboardHeader', () => 'LearnerDashboardHeader');
20-
jest.mock('containers/WidgetContainers/AppWrapper', () => 'AppWrapper');
11+
jest.mock('@edx/frontend-component-footer', () => ({
12+
FooterSlot: jest.fn(() => <div>FooterSlot</div>),
13+
}));
14+
jest.mock('containers/Dashboard', () => jest.fn(() => <div>Dashboard</div>));
15+
jest.mock('containers/LearnerDashboardHeader', () => jest.fn(() => <div>LearnerDashboardHeader</div>));
16+
jest.mock('containers/WidgetContainers/AppWrapper', () => jest.fn(({ children }) => <div className="AppWrapper">{children}</div>));
2117
jest.mock('data/redux', () => ({
2218
selectors: 'redux.selectors',
2319
actions: 'redux.actions',
@@ -36,119 +32,102 @@ jest.mock('@edx/frontend-platform', () => ({
3632
getConfig: jest.fn(() => ({})),
3733
}));
3834

35+
jest.unmock('@openedx/paragon');
36+
jest.unmock('@edx/frontend-platform/i18n');
37+
jest.unmock('react');
38+
3939
const loadData = jest.fn();
4040
reduxHooks.useLoadData.mockReturnValue(loadData);
4141

42-
let el;
43-
44-
const supportEmail = 'test-support-url';
42+
const supportEmail = 'test@support.com';
4543
reduxHooks.usePlatformSettingsData.mockReturnValue({ supportEmail });
4644

4745
describe('App router component', () => {
48-
const { formatMessage } = useIntl();
4946
describe('component', () => {
5047
const runBasicTests = () => {
51-
test('snapshot', () => { expect(el.snapshot).toMatchSnapshot(); });
52-
it('displays title in helmet component', () => {
53-
const control = el.instance
54-
.findByType(Helmet)[0]
55-
.findByType('title')[0];
56-
expect(control.children[0].el).toEqual(formatMessage(messages.pageTitle));
48+
it('displays title in helmet component', async () => {
49+
await waitFor(() => expect(document.title).toEqual(messages.pageTitle.defaultMessage));
5750
});
5851
it('displays learner dashboard header', () => {
59-
expect(el.instance.findByType(LearnerDashboardHeader).length).toEqual(1);
52+
const learnerDashboardHeader = screen.getByText('LearnerDashboardHeader');
53+
expect(learnerDashboardHeader).toBeInTheDocument();
6054
});
6155
it('wraps the header and main components in an AppWrapper widget container', () => {
62-
const container = el.instance.findByType(AppWrapper)[0];
63-
expect(container.children[0].type).toEqual('LearnerDashboardHeader');
64-
expect(container.children[1].type).toEqual('main');
56+
const appWrapper = screen.getByText('LearnerDashboardHeader').parentElement;
57+
expect(appWrapper).toHaveClass('AppWrapper');
58+
expect(appWrapper.children[1].id).toEqual('main');
59+
});
60+
it('displays footer slot', () => {
61+
const footerSlot = screen.getByText('FooterSlot');
62+
expect(footerSlot).toBeInTheDocument();
6563
});
6664
};
6765
describe('no network failure', () => {
68-
beforeAll(() => {
66+
beforeEach(() => {
67+
jest.clearAllMocks();
6968
reduxHooks.useRequestIsFailed.mockReturnValue(false);
7069
getConfig.mockReturnValue({});
71-
el = shallow(<App />);
70+
render(<IntlProvider locale="en"><App /></IntlProvider>);
7271
});
7372
runBasicTests();
7473
it('loads dashboard', () => {
75-
const main = el.instance.findByType('main')[0];
76-
expect(main.children.length).toEqual(1);
77-
const dashboard = main.children[0];
78-
expect(dashboard.type).toEqual('Dashboard');
79-
expect(
80-
dashboard.matches(shallow(<Dashboard />)),
81-
).toEqual(true);
74+
const dashboard = screen.getByText('Dashboard');
75+
expect(dashboard).toBeInTheDocument();
8276
});
8377
});
8478
describe('no network failure with optimizely url', () => {
85-
beforeAll(() => {
79+
beforeEach(() => {
80+
jest.clearAllMocks();
8681
reduxHooks.useRequestIsFailed.mockReturnValue(false);
8782
getConfig.mockReturnValue({ OPTIMIZELY_URL: 'fake.url' });
88-
el = shallow(<App />);
83+
render(<IntlProvider locale="en"><App /></IntlProvider>);
8984
});
9085
runBasicTests();
9186
it('loads dashboard', () => {
92-
const main = el.instance.findByType('main')[0];
93-
expect(main.children.length).toEqual(1);
94-
const dashboard = main.children[0];
95-
expect(dashboard.type).toEqual('Dashboard');
96-
expect(
97-
dashboard.matches(shallow(<Dashboard />)),
98-
).toEqual(true);
87+
const dashboard = screen.getByText('Dashboard');
88+
expect(dashboard).toBeInTheDocument();
9989
});
10090
});
10191
describe('no network failure with optimizely project id', () => {
102-
beforeAll(() => {
92+
beforeEach(() => {
93+
jest.clearAllMocks();
10394
reduxHooks.useRequestIsFailed.mockReturnValue(false);
10495
getConfig.mockReturnValue({ OPTIMIZELY_PROJECT_ID: 'fakeId' });
105-
el = shallow(<App />);
96+
render(<IntlProvider locale="en"><App /></IntlProvider>);
10697
});
10798
runBasicTests();
10899
it('loads dashboard', () => {
109-
const main = el.instance.findByType('main')[0];
110-
expect(main.children.length).toEqual(1);
111-
const dashboard = main.children[0];
112-
expect(dashboard.type).toEqual('Dashboard');
113-
expect(
114-
dashboard.matches(shallow(<Dashboard />)),
115-
).toEqual(true);
100+
const dashboard = screen.getByText('Dashboard');
101+
expect(dashboard).toBeInTheDocument();
116102
});
117103
});
118104
describe('initialize failure', () => {
119-
beforeAll(() => {
105+
beforeEach(() => {
106+
jest.clearAllMocks();
120107
reduxHooks.useRequestIsFailed.mockImplementation((key) => key === RequestKeys.initialize);
121108
getConfig.mockReturnValue({});
122-
el = shallow(<App />);
109+
render(<IntlProvider locale="en" messages={messages}><App /></IntlProvider>);
123110
});
124111
runBasicTests();
125112
it('loads error page', () => {
126-
const main = el.instance.findByType('main')[0];
127-
expect(main.children.length).toEqual(1);
128-
const alert = main.children[0];
129-
expect(alert.type).toEqual('Alert');
130-
expect(alert.children.length).toEqual(1);
131-
const errorPage = alert.children[0];
132-
expect(errorPage.type).toEqual('ErrorPage');
133-
expect(errorPage.props.message).toEqual(formatMessage(messages.errorMessage, { supportEmail }));
113+
const alert = screen.getByRole('alert');
114+
expect(alert).toBeInTheDocument();
115+
const errorPage = screen.getByText('ErrorPage');
116+
expect(errorPage).toBeInTheDocument();
134117
});
135118
});
136119
describe('refresh failure', () => {
137-
beforeAll(() => {
120+
beforeEach(() => {
138121
reduxHooks.useRequestIsFailed.mockImplementation((key) => key === RequestKeys.refreshList);
139122
getConfig.mockReturnValue({});
140-
el = shallow(<App />);
123+
render(<IntlProvider locale="en"><App /></IntlProvider>);
141124
});
142125
runBasicTests();
143126
it('loads error page', () => {
144-
const main = el.instance.findByType('main')[0];
145-
expect(main.children.length).toEqual(1);
146-
const alert = main.children[0];
147-
expect(alert.type).toEqual('Alert');
148-
expect(alert.children.length).toEqual(1);
149-
const errorPage = alert.children[0];
150-
expect(errorPage.type).toEqual('ErrorPage');
151-
expect(errorPage.props.message).toEqual(formatMessage(messages.errorMessage, { supportEmail }));
127+
const alert = screen.getByRole('alert');
128+
expect(alert).toBeInTheDocument();
129+
const errorPage = screen.getByText('ErrorPage');
130+
expect(errorPage).toBeInTheDocument();
152131
});
153132
});
154133
});

src/__snapshots__/App.test.jsx.snap

Lines changed: 0 additions & 153 deletions
This file was deleted.

0 commit comments

Comments
 (0)