Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mount/unmountAll from @hypothesis/frontend-testing #6872

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lms/static/scripts/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unmountAll } from '@hypothesis/frontend-testing';
import { configure } from 'enzyme';
import { Adapter } from 'enzyme-adapter-preact-pure';
import 'preact/debug';
Expand All @@ -7,6 +8,9 @@ sinon.assert.expose(assert, { prefix: null });

// Configure Enzyme for UI tests.
configure({ adapter: new Adapter() });
afterEach(() => {
unmountAll();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we have a tests bootstrap/init/set-up script in other projects, but if we do, we should do this in all of them, so that we don't have to remember unmounting every time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have a bootstrap.js file in every project to set up Sinon, asserts, the Enzyme adapter etc.


// Ensure that uncaught exceptions between tests result in the tests failing.
// This works around an issue with mocha / karma-mocha, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';
import sinon from 'sinon';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';
import sinon from 'sinon';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import ContentWithBadge from '../ContentWithBadge';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';
import sinon from 'sinon';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { formatDateTime } from '@hypothesis/frontend-shared';
import {
checkAccessibility,
mockImportedComponents,
mount,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import sinon from 'sinon';

import { Config } from '../../../config';
Expand Down Expand Up @@ -64,8 +64,6 @@ describe('DashboardActivityFilters', () => {
let fakeLoadNextCoursesPage;
let fakeLoadNextAssignmentsPage;
let fakeLoadNextStudentsPage;
let wrappers;
let containers;

/**
* @param {object} options
Expand Down Expand Up @@ -109,9 +107,6 @@ describe('DashboardActivityFilters', () => {
}

beforeEach(() => {
wrappers = [];
containers = [];

fakeLoadNextCoursesPage = sinon.stub();
fakeLoadNextAssignmentsPage = sinon.stub();
fakeLoadNextStudentsPage = sinon.stub();
Expand All @@ -121,7 +116,6 @@ describe('DashboardActivityFilters', () => {
onCoursesChange = sinon.stub();
onAssignmentsChange = sinon.stub();
onStudentsChange = sinon.stub();
wrappers = [];

fakeConfig = {
dashboard: {
Expand All @@ -147,8 +141,6 @@ describe('DashboardActivityFilters', () => {
});

afterEach(() => {
wrappers.forEach(w => w.unmount());
containers.forEach(c => c.remove());
$imports.$restore();
});

Expand Down Expand Up @@ -180,24 +172,13 @@ describe('DashboardActivityFilters', () => {
});
}

function createContainerInDOM() {
const container = document.createElement('div');
containers.push(container);
document.body.appendChild(container);

return container;
}

function createComponentWithProps(props) {
const wrapper = mount(
return mount(
<Config.Provider value={fakeConfig}>
<DashboardActivityFilters {...props} />
</Config.Provider>,
{ attachTo: createContainerInDOM() },
{ connected: true },
);
wrappers.push(wrapper);

return wrapper;
}

function getSelect(wrapper, id) {
Expand Down Expand Up @@ -274,7 +255,7 @@ describe('DashboardActivityFilters', () => {
<MultiSelect value={[]} onChange={sinon.stub()}>
{select.props().renderOption(entity)}
</MultiSelect>,
{ attachTo: createContainerInDOM() },
{ connected: true },
);
// The Select needs to be open, otherwise options are not rendered
tempSelect.find('button').simulate('click');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import { Config } from '../../../config';
import DashboardApp, { $imports } from '../DashboardApp';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import DashboardBreadcrumbs from '../DashboardBreadcrumbs';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import DashboardFooter from '../DashboardFooter';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import GradeIndicator from '../GradeIndicator';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import GradeStatusChip from '../GradeStatusChip';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import sinon from 'sinon';

import OrderableActivityTable, { $imports } from '../OrderableActivityTable';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { MultiSelect } from '@hypothesis/frontend-shared';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import PaginatedMultiSelect from '../PaginatedMultiSelect';

describe('PaginatedMultiSelect', () => {
let wrappers;
let containers;

beforeEach(() => {
wrappers = [];
containers = [];
});

afterEach(() => {
wrappers.forEach(w => w.unmount());
containers.forEach(c => c.remove());
});

function createComponent(props) {
const container = document.createElement('div');
containers.push(container);
document.body.appendChild(container);

const wrapper = mount(
return mount(
<PaginatedMultiSelect
entity="courses"
value={[]}
Expand All @@ -32,11 +15,8 @@ describe('PaginatedMultiSelect', () => {
)}
{...props}
/>,
{ attachTo: container },
{ connected: true },
);
wrappers.push(wrapper);

return wrapper;
}

function getSelect(wrapper, id = 'courses') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';
import sinon from 'sinon';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
mockImportedComponents,
waitForElement,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import { useConfig } from '../../config';
import { useService } from '../../services';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import AuthButton, { $imports } from '../AuthButton';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import AuthorizationModal from '../AuthorizationModal';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAccessibility } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import AutoGradingConfigurator from '../AutoGradingConfigurator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
waitFor,
waitForElement,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import { Config } from '../../config';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
waitFor,
waitForElement,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import { VitalSourceService, withServices } from '../../services';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { waitFor, waitForElement } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount, waitFor, waitForElement } from '@hypothesis/frontend-testing';

import { VitalSourceService, withServices } from '../../services';
import BookSelector, { $imports } from '../BookSelector';
Expand Down Expand Up @@ -73,24 +72,13 @@ describe('BookSelector', () => {
};

describe('initial focus', () => {
let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
container.remove();
});

it('focuses the URL text input element', () => {
const beforeFocused = document.activeElement;

const wrapper = mount(
<BookSelectorWrapper onSelectBook={sinon.stub()} />,
{
attachTo: container,
connected: true,
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import Breadcrumbs from '../Breadcrumbs';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
waitFor,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { act } from 'preact/test-utils';

import { Config } from '../../config';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { waitForElement } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { useState } from 'preact/hooks';

import DataLoader from '../DataLoader';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import DocumentList, { $imports } from '../DocumentList';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import EmailPreferences from '../EmailPreferences';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockImportedComponents } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import { Config } from '../../config';
import EmailPreferencesApp, { $imports } from '../EmailPreferencesApp';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import { Config } from '../../config';
import ErrorDialogApp from '../ErrorDialogApp';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
checkAccessibility,
mockImportedComponents,
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import { Config } from '../../config';
import ErrorDisplay, { $imports } from '../ErrorDisplay';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockImportedComponents } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import ErrorModal, { $imports } from '../ErrorModal';

Expand Down
Loading