Skip to content

Commit

Permalink
Clean up console errors on tests (#354)
Browse files Browse the repository at this point in the history
* Fail tests on console errors

We want our code to run with out errors
Too many errors in the test output obfusticates real errors

* Add global to jsdom for testing

Allows tests that use global.location to pass without errors

* Clean up CouponDetails errors

* Add match to SupportPage

* Add necessary props to AddUsersModal test

* Avoid warning about attaching to document.body

* Add missing props

* Comment out throwing errors for console errors

We can add this back once all tests are fixed

* Use copies of objects instead the originals

Keeps tests from failing when originals are changed
  • Loading branch information
Lael Birch authored Nov 2, 2020
1 parent 424963a commit 8d378c3
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 264 deletions.
13 changes: 13 additions & 0 deletions jsdom-with-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const JSDOMEnvironment = require('jest-environment-jsdom');

module.exports = class CustomizedJSDomEnvironment extends JSDOMEnvironment {
constructor(config) {
super(config);
this.global.jsdom = this.dom;
}

teardown() {
this.global.jsdom = null;
return super.teardown();
}
};
448 changes: 260 additions & 188 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"file-saver": "^1.3.8",
"font-awesome": "^4.7.0",
"history": "^4.10.1",
"jest-environment-jsdom": "^26.6.1",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.15",
"moment": "^2.25.3",
Expand Down Expand Up @@ -128,6 +129,7 @@
],
"transformIgnorePatterns": [
"/node_modules/(?!(@edx/paragon)/).*/"
]
],
"testEnvironment": "./jsdom-with-global.js"
}
}
1 change: 1 addition & 0 deletions src/components/CodeReminderModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class CodeReminderModal extends React.Component {
name="email-template-subject"
component={RenderField}
label="Customize Email Subject"
type="text"
/>
<Field
id="email-template-greeting"
Expand Down
1 change: 1 addition & 0 deletions src/components/CodeRevokeModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class CodeRevokeModal extends React.Component {
id="email-template-subject"
name="email-template-subject"
component={RenderField}
type="text"
label="Customize Email Subject"
/>
<Field
Expand Down
62 changes: 20 additions & 42 deletions src/components/CodeSearchResults/CodeSearchResults.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ jest.mock('../../data/services/EcommerceApiService');
const enterpriseId = 'test-enterprise';
const mockStore = configureMockStore([thunk]);
const getMockStore = store => mockStore(store);

const sampleEmailTemplate = {
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
};

const emailDefaults = {
'template-id': 0,
'template-name-select': '',
...sampleEmailTemplate,
};

const initialStore = {
portalConfiguration: {
enterpriseId,
Expand All @@ -26,49 +40,13 @@ const initialStore = {
allTemplates: [],
emailTemplateSource: 'new_email',
default: {
assign: {
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
},
remind: {
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
},
revoke: {
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
},
},
assign: {
'template-id': 0,
'template-name-select': '',
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
},
remind: {
'template-id': 0,
'template-name-select': '',
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
},
revoke: {
'template-id': 0,
'template-name-select': '',
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
assign: { ...sampleEmailTemplate },
remind: { ...sampleEmailTemplate },
revoke: { ...sampleEmailTemplate },
},
assign: { ...emailDefaults },
remind: { ...emailDefaults },
revoke: { ...emailDefaults },
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/SupportPage/SupportPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('<SupportPage />', () => {
it('renders correctly', () => {
const tree = renderer
.create((
<SupportPage />
<SupportPage match={{ url: 'example.com' }} />
))
.toJSON();
expect(tree).toMatchSnapshot();
Expand Down
1 change: 1 addition & 0 deletions src/containers/AddUsersModal/AddUsersModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const UserSubscriptionModalWrapper = props => (
availableSubscriptionCount={10}
onClose={() => {}}
onSuccess={() => {}}
subscriptionUUID="foo"
{...props}
/>
</Provider>
Expand Down
4 changes: 1 addition & 3 deletions src/containers/AdminRegisterPage/AdminRegisterPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ AdminRegisterPageWrapper.propTypes = {

describe('<AdminRegisterPage />', () => {
it('renders loading message when not authenticated (redirect to enterprise proxy login)', () => {
// Note: this test does not assert that the redirect to the proxy login works since
// JSdom does not implement global.location. Due to this, JSdom outputs a "Not
// implemented: navigation" warning for this test that can safely be ignored.
const wrapper = mount(<AdminRegisterPageWrapper />);

// verify that the loading message appears during redirect
const LoadingComponent = <LoadingMessage className="admin-register" />;
expect(wrapper.contains(LoadingComponent)).toBeTruthy();
expect(global.location.href).toBeTruthy();
});

it('displays error alert when user is authenticated but no has JWT roles', () => {
Expand Down
14 changes: 14 additions & 0 deletions src/containers/CouponDetails/CouponDetails.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import EcommerceaApiService from '../../data/services/EcommerceApiService';

import CouponDetailsComponent from '../../components/CouponDetails';
import CouponDetails from './index';
import { EMAIL_TEMPLATE_SOURCE_NEW_EMAIL } from '../../data/constants/emailTemplate';

const enterpriseId = 'test-enterprise';
const mockStore = configureMockStore([thunk]);
Expand All @@ -23,6 +24,15 @@ const sampleEmailTemplate = {
'email-template-closing': 'Sample email closing template.. ',
};

const emailDefaults = {
'template-id': 0,
'template-name-select': '',
'email-template-subject': 'Sample email subject.. ',
'email-template-greeting': 'Sample email greeting.. ',
'email-template-body': 'Sample email body template.. ',
'email-template-closing': 'Sample email closing template.. ',
};

const initialState = {
portalConfiguration: {
enterpriseId,
Expand All @@ -42,11 +52,15 @@ const initialState = {
emailTemplate: {
loading: false,
error: null,
emailTemplateSource: EMAIL_TEMPLATE_SOURCE_NEW_EMAIL,
default: {
assign: sampleEmailTemplate,
remind: sampleEmailTemplate,
revoke: sampleEmailTemplate,
},
assign: emailDefaults,
remind: emailDefaults,
revoke: emailDefaults,
},
};

Expand Down
69 changes: 41 additions & 28 deletions src/containers/EnterpriseApp/EnterpriseApp.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,49 @@ describe('EnterpriseApp', () => {
.toJSON();
expect(tree).toMatchSnapshot();
});

it('handles location change properly', () => {
// There is some logic where we collapse the sidebar on menu click on mobile
// so we test that here as well. Note that we need to set the window width
// to mobile first.
const initialWidth = global.innerWidth;
global.innerWidth = breakpoints.small.minWidth;

const store = mockStore({
...initialState,
sidebar: {
...initialState.sidebar,
isExpandedByToggle: true,
},
describe('location changes', () => {
beforeEach(() => {
// Avoid `attachTo: document.body` Warning
const div = document.createElement('div');
div.setAttribute('id', 'container');
document.body.appendChild(div);
});

const wrapper = mount(
<EnterpriseAppWrapper store={store} />,
{ attachTo: document.body },
);

wrapper.setProps({
location: {
pathname: '/test-enterprise-slug/admin/codes',
},
afterEach(() => {
const div = document.getElementById('container');
if (div) {
document.body.removeChild(div);
}
});
it('handles location change properly', () => {
// There is some logic where we collapse the sidebar on menu click on mobile
// so we test that here as well. Note that we need to set the window width
// to mobile first.
const initialWidth = global.innerWidth;
global.innerWidth = breakpoints.small.minWidth;

const store = mockStore({
...initialState,
sidebar: {
...initialState.sidebar,
isExpandedByToggle: true,
},
});

const wrapper = mount(
<EnterpriseAppWrapper store={store} />,
{ attachTo: document.getElementById('container') },
);

wrapper.setProps({
location: {
pathname: '/test-enterprise-slug/admin/codes',
},
});

// ensure focus is set on content wrapper
expect(document.activeElement.className).toEqual('content-wrapper');
global.innerWidth = initialWidth;
});

// ensure focus is set on content wrapper
expect(document.activeElement.className).toEqual('content-wrapper');
global.innerWidth = initialWidth;
});

it('toggles sidebar toggle on componentWillUnmount', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/containers/LicenseRemindModal/LicenseRemindModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const LicenseRemindModalWrapper = props => (
user={user}
onClose={() => {}}
onSuccess={() => {}}
subscriptionUUID="bears4education"
fetchSubscriptionUsers={() => {}}
fetchSubscriptionDetails={() => {}}
{...props}
/>
</Provider>
Expand Down
11 changes: 10 additions & 1 deletion src/setupTest.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
/* eslint-disable import/no-extraneous-dependencies */

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });


// TODO: Once there are no more console errors in tests, uncomment the code below
// const { error } = global.console;

// global.console.error = (...args) => {
// error(...args);
// throw new Error(args.join(' '));
// };


// These configuration values are usually set in webpack's EnvironmentPlugin however
// Jest does not use webpack so we need to set these so for testing
process.env.LMS_BASE_URL = 'http://localhost:18000';
Expand Down

0 comments on commit 8d378c3

Please sign in to comment.