Skip to content

Commit

Permalink
Replace to testing-library from enzyme (mastodon#14152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Jun 29, 2020
1 parent d1c6dd2 commit 5e8f51b
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 312 deletions.
10 changes: 5 additions & 5 deletions app/javascript/mastodon/components/__tests__/button-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallow } from 'enzyme';
import { render, fireEvent, screen } from '@testing-library/react';
import React from 'react';
import renderer from 'react-test-renderer';
import Button from '../button';
Expand All @@ -21,16 +21,16 @@ describe('<Button />', () => {

it('handles click events using the given handler', () => {
const handler = jest.fn();
const button = shallow(<Button onClick={handler} />);
button.find('button').simulate('click');
render(<Button onClick={handler}>button</Button>);
fireEvent.click(screen.getByText('button'));

expect(handler.mock.calls.length).toEqual(1);
});

it('does not handle click events if props.disabled given', () => {
const handler = jest.fn();
const button = shallow(<Button onClick={handler} disabled />);
button.find('button').simulate('click');
render(<Button onClick={handler} disabled>button</Button>);
fireEvent.click(screen.getByText('button'));

expect(handler.mock.calls.length).toEqual(0);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { render, fireEvent, screen } from '@testing-library/react';
import React from 'react';
import { mount } from 'enzyme';
import Column from '../column';
import ColumnHeader from '../column_header';

describe('<Column />', () => {
describe('<ColumnHeader /> click handler', () => {
it('runs the scroll animation if the column contains scrollable content', () => {
const wrapper = mount(
const scrollToMock = jest.fn();
const { container } = render(
<Column heading='notifications'>
<div className='scrollable' />
</Column>,
);
const scrollToMock = jest.fn();
wrapper.find(Column).find('.scrollable').getDOMNode().scrollTo = scrollToMock;
wrapper.find(ColumnHeader).find('button').simulate('click');
container.querySelector('.scrollable').scrollTo = scrollToMock;
fireEvent.click(screen.getByText('notifications'));
expect(scrollToMock).toHaveBeenCalledWith({ behavior: 'smooth', top: 0 });
});

it('does not try to scroll if there is no scrollable content', () => {
const wrapper = mount(<Column heading='notifications' />);
wrapper.find(ColumnHeader).find('button').simulate('click');
render(<Column heading='notifications' />);
fireEvent.click(screen.getByText('notifications'));
});
});
});
6 changes: 1 addition & 5 deletions app/javascript/mastodon/test_setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

const adapter = new Adapter();
configure({ adapter });
import '@testing-library/jest-dom/extend-expect';
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
"wicg-inert": "^3.0.3"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.0",
"@testing-library/react": "^10.4.3",
"babel-eslint": "^10.1.0",
"babel-jest": "^25.2.4",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"eslint": "^6.8.0",
"eslint-plugin-import": "~2.21.2",
"eslint-plugin-jsx-a11y": "~6.3.1",
Expand Down
Loading

0 comments on commit 5e8f51b

Please sign in to comment.