Skip to content

Commit 31c5445

Browse files
test: Deprecate react-unit-test-utils 11/15 (#670)
1 parent 2acf7fb commit 31c5445

File tree

6 files changed

+163
-387
lines changed

6 files changed

+163
-387
lines changed

src/App.test.jsx

Lines changed: 52 additions & 67 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,113 +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 = '[email protected]';
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].el;
78-
expect(dashboard.type).toEqual('Dashboard');
79-
expect(dashboard).toEqual(shallow(<Dashboard />));
74+
const dashboard = screen.getByText('Dashboard');
75+
expect(dashboard).toBeInTheDocument();
8076
});
8177
});
8278
describe('no network failure with optimizely url', () => {
83-
beforeAll(() => {
79+
beforeEach(() => {
80+
jest.clearAllMocks();
8481
reduxHooks.useRequestIsFailed.mockReturnValue(false);
8582
getConfig.mockReturnValue({ OPTIMIZELY_URL: 'fake.url' });
86-
el = shallow(<App />);
83+
render(<IntlProvider locale="en"><App /></IntlProvider>);
8784
});
8885
runBasicTests();
8986
it('loads dashboard', () => {
90-
const main = el.instance.findByType('main')[0];
91-
expect(main.children.length).toEqual(1);
92-
const dashboard = main.children[0].el;
93-
expect(dashboard.type).toEqual('Dashboard');
94-
expect(dashboard).toEqual(shallow(<Dashboard />));
87+
const dashboard = screen.getByText('Dashboard');
88+
expect(dashboard).toBeInTheDocument();
9589
});
9690
});
9791
describe('no network failure with optimizely project id', () => {
98-
beforeAll(() => {
92+
beforeEach(() => {
93+
jest.clearAllMocks();
9994
reduxHooks.useRequestIsFailed.mockReturnValue(false);
10095
getConfig.mockReturnValue({ OPTIMIZELY_PROJECT_ID: 'fakeId' });
101-
el = shallow(<App />);
96+
render(<IntlProvider locale="en"><App /></IntlProvider>);
10297
});
10398
runBasicTests();
10499
it('loads dashboard', () => {
105-
const main = el.instance.findByType('main')[0];
106-
expect(main.children.length).toEqual(1);
107-
const dashboard = main.children[0].el;
108-
expect(dashboard.type).toEqual('Dashboard');
109-
expect(dashboard).toEqual(shallow(<Dashboard />));
100+
const dashboard = screen.getByText('Dashboard');
101+
expect(dashboard).toBeInTheDocument();
110102
});
111103
});
112104
describe('initialize failure', () => {
113-
beforeAll(() => {
105+
beforeEach(() => {
106+
jest.clearAllMocks();
114107
reduxHooks.useRequestIsFailed.mockImplementation((key) => key === RequestKeys.initialize);
115108
getConfig.mockReturnValue({});
116-
el = shallow(<App />);
109+
render(<IntlProvider locale="en" messages={messages}><App /></IntlProvider>);
117110
});
118111
runBasicTests();
119112
it('loads error page', () => {
120-
const main = el.instance.findByType('main')[0];
121-
expect(main.children.length).toEqual(1);
122-
const alert = main.children[0];
123-
expect(alert.type).toEqual('Alert');
124-
expect(alert.children.length).toEqual(1);
125-
const errorPage = alert.children[0];
126-
expect(errorPage.type).toEqual('ErrorPage');
127-
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();
128117
});
129118
});
130119
describe('refresh failure', () => {
131-
beforeAll(() => {
120+
beforeEach(() => {
132121
reduxHooks.useRequestIsFailed.mockImplementation((key) => key === RequestKeys.refreshList);
133122
getConfig.mockReturnValue({});
134-
el = shallow(<App />);
123+
render(<IntlProvider locale="en"><App /></IntlProvider>);
135124
});
136125
runBasicTests();
137126
it('loads error page', () => {
138-
const main = el.instance.findByType('main')[0];
139-
expect(main.children.length).toEqual(1);
140-
const alert = main.children[0];
141-
expect(alert.type).toEqual('Alert');
142-
expect(alert.children.length).toEqual(1);
143-
const errorPage = alert.children[0];
144-
expect(errorPage.type).toEqual('ErrorPage');
145-
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();
146131
});
147132
});
148133
});

src/__snapshots__/App.test.jsx.snap

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

0 commit comments

Comments
 (0)