Skip to content

Commit

Permalink
Use mount helper functions from @hypothesis/frontend-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Dec 2, 2024
1 parent eb9e6c4 commit 5ac9c79
Show file tree
Hide file tree
Showing 42 changed files with 69 additions and 176 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.18.6",
"@hypothesis/frontend-build": "^3.0.0",
"@hypothesis/frontend-testing": "^1.2.2",
"@hypothesis/frontend-testing": "^1.3.1",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/AspectRatio-test.js
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 { testSimpleComponent } from '../../test/common-tests';
import AspectRatio from '../AspectRatio';
Expand Down
22 changes: 5 additions & 17 deletions src/components/data/test/DataTable-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';
import { mount as enzymeMount } from 'enzyme';
import { useState } from 'preact/hooks';

import { testCompositeComponent } from '../../test/common-tests';
Expand All @@ -8,8 +9,6 @@ import Scroll from '../Scroll';
describe('DataTable', () => {
let fakeRows;
let fakeColumns;
let container;
let wrappers;

beforeEach(() => {
fakeColumns = [
Expand Down Expand Up @@ -45,15 +44,6 @@ describe('DataTable', () => {
delicious: true,
},
];

wrappers = [];
container = document.createElement('div');
document.body.append(container);
});

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

// DataTable test wrapper which supports selecting multiple rows.
Expand All @@ -75,15 +65,13 @@ describe('DataTable', () => {
}

const createComponent = ({ Component = DataTable, ...props } = {}) => {
const wrapper = mount(
return mount(
<Component columns={fakeColumns} rows={fakeRows} {...props} />,

// Mounting in a connected element is required for arrow key navigation
// to work, as `useArrowKeyNavigation` skips over hidden elements.
{ attachTo: container },
{ connected: true },
);
wrappers.push(wrapper);
return wrapper;
};

it('sets appropriate table attributes', () => {
Expand Down Expand Up @@ -366,7 +354,7 @@ describe('DataTable', () => {
});

it('ensures selected row is visible in scrollable area', async () => {
const wrapper = mount(<TestTableWithScroll />, {
const wrapper = enzymeMount(<TestTableWithScroll />, {
attachTo: constrainedContainer,
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/ScrollBox-test.js
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 { testCompositeComponent } from '../../test/common-tests';
import ScrollBox from '../ScrollBox';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/TableBody-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import TableBody from '../TableBody';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/TableCell-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import TableCell from '../TableCell';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/TableFoot-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import TableFoot from '../TableFoot';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/TableHead-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import TableContext from '../TableContext';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/TableRow-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import TableContext from '../TableContext';
Expand Down
2 changes: 1 addition & 1 deletion src/components/data/test/Thumbnail-test.js
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 { testCompositeComponent } from '../../test/common-tests';
import Thumbnail from '../Thumbnail';
Expand Down
2 changes: 1 addition & 1 deletion src/components/feedback/test/Callout-test.js
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 {
testPresentationalComponent,
Expand Down
56 changes: 12 additions & 44 deletions src/components/feedback/test/Dialog-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { delay } from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { delay, mount } from '@hypothesis/frontend-testing';
import { createRef } from 'preact';

import { testPresentationalComponent } from '../../test/common-tests';
Expand Down Expand Up @@ -55,25 +54,14 @@ describe('Dialog', () => {
});

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

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

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

it('focuses the element referred to by `initialFocus`', () => {
const inputRef = createRef();

mount(
<Dialog initialFocus={inputRef} title="My dialog">
<input ref={inputRef} />
</Dialog>,
{ attachTo: container },
{ connected: true },
);

assert.equal(document.activeElement, inputRef.current);
Expand All @@ -84,7 +72,7 @@ describe('Dialog', () => {
<Dialog title="My dialog">
<div>Test</div>
</Dialog>,
{ attachTo: container },
{ connected: true },
);

assert.equal(
Expand All @@ -99,7 +87,7 @@ describe('Dialog', () => {
<Dialog initialFocus={'manual'} title="My dialog">
<div>Test</div>
</Dialog>,
{ attachTo: container },
{ connected: true },
);

assert.equal(document.activeElement, focusedBefore);
Expand All @@ -112,7 +100,7 @@ describe('Dialog', () => {
<Dialog initialFocus={inputRef} title="My dialog">
<button ref={inputRef} disabled={true} />
</Dialog>,
{ attachTo: container },
{ connected: true },
);

assert.equal(
Expand All @@ -128,7 +116,7 @@ describe('Dialog', () => {
title="My dialog"
transitionComponent={ComponentWithTransition}
/>,
{ attachTo: container },
{ connected: true },
);

// The Dialog is still not focused immediately after mounting it
Expand All @@ -147,26 +135,16 @@ describe('Dialog', () => {
});

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

beforeEach(() => {
container = document.createElement('div');
const button = document.createElement('button');
button.id = 'focus-button';
document.body.appendChild(button);
document.body.appendChild(container);
});

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

it('should not restore focus by default', () => {
const wrapper = mount(
<Dialog id="focus-dialog" title="My dialog" restoreFocus />,
{
attachTo: container,
},
{ connected: true },
);
const dialogElement = document.getElementById('focus-dialog');
// Focus moves to dialog by default when mounted
Expand All @@ -187,9 +165,7 @@ describe('Dialog', () => {

const wrapper = mount(
<Dialog id="focus-dialog" title="My dialog" restoreFocus />,
{
attachTo: container,
},
{ connected: true },
);
const dialogElement = document.getElementById('focus-dialog');
// Focus moves to dialog by default when mounted
Expand Down Expand Up @@ -221,9 +197,7 @@ describe('Dialog', () => {
<Dialog title="Test dialog" onClose={onClose}>
This is my dialog
</Dialog>,
{
attachTo: container,
},
{ connected: true },
);

assert.deepEqual(fakeUseKeyPress.lastCall.args[0], ['Escape']);
Expand All @@ -238,9 +212,7 @@ describe('Dialog', () => {
<Dialog title="Test dialog" closeOnClickAway onClose={onClose}>
This is my dialog
</Dialog>,
{
attachTo: container,
},
{ connected: true },
);

assert.deepEqual(fakeUseClickAway.lastCall.args[2], { enabled: true });
Expand All @@ -251,9 +223,7 @@ describe('Dialog', () => {
<Dialog title="Test dialog" closeOnFocusAway onClose={onClose}>
This is my dialog
</Dialog>,
{
attachTo: container,
},
{ connected: true },
);

assert.deepEqual(fakeUseFocusAway.lastCall.args[2], { enabled: true });
Expand All @@ -267,9 +237,7 @@ describe('Dialog', () => {
onClose={onClose}
transitionComponent={ComponentWithTransition}
/>,
{
attachTo: container,
},
{ connected: true },
);

// We simulate closing the Dialog's Panel
Expand Down
2 changes: 1 addition & 1 deletion src/components/feedback/test/ModalDialog-test.js
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 { testCompositeComponent } from '../../test/common-tests';
import ModalDialog from '../ModalDialog';
Expand Down
18 changes: 3 additions & 15 deletions src/components/feedback/test/ToastMessages-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { mount } from 'enzyme';
import { mount } from '@hypothesis/frontend-testing';

import ToastMessages from '../ToastMessages';

describe('ToastMessages', () => {
let wrappers;
const toastMessages = [
{
id: '1',
Expand All @@ -24,29 +23,18 @@ describe('ToastMessages', () => {
let fakeOnMessageDismiss;

beforeEach(() => {
wrappers = [];
fakeOnMessageDismiss = sinon.stub();
});

afterEach(() => {
wrappers.forEach(wrapper => wrapper.unmount());
});

function createToastMessages(toastMessages, setTimeout) {
const container = document.createElement('div');
document.body.appendChild(container);

const wrapper = mount(
return mount(
<ToastMessages
messages={toastMessages}
onMessageDismiss={fakeOnMessageDismiss}
setTimeout_={setTimeout}
/>,
{ attachTo: container },
{ connected: true },
);
wrappers.push(wrapper);

return wrapper;
}

function triggerAnimationEnd(wrapper, index, direction = 'out') {
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/Button-test.js
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 { CancelIcon } from '../../icons';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/Checkbox-test.js
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 { testCompositeComponent } from '../../test/common-tests';
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/CloseButton-test.js
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 CloseableContext from '../../CloseableContext';
import { testPresentationalComponent } from '../../test/common-tests';
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/IconButton-test.js
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 { CancelIcon } from '../../icons';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/Input-test.js
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 { testPresentationalComponent } from '../../test/common-tests';
import Input from '../Input';
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/OptionButton-test.js
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 { testSimpleComponent } from '../../test/common-tests';
import OptionButton from '../OptionButton';
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/test/RadioButton-test.js
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 { testCompositeComponent } from '../../test/common-tests';
import RadioButton from '../RadioButton';
Expand Down
Loading

0 comments on commit 5ac9c79

Please sign in to comment.