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

[RFR] Migrate tests to react-testing-library #4446

Merged
merged 13 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
23 changes: 13 additions & 10 deletions packages/ra-core/src/core/RoutesWithLayout.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from 'react';
import { Route, MemoryRouter } from 'react-router-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { mount } from 'enzyme';
import { render, cleanup } from '@testing-library/react';
import assert from 'assert';

import RoutesWithLayout from './RoutesWithLayout';

describe('<RoutesWithLayout>', () => {
afterEach(cleanup);

const Dashboard = () => <div>Dashboard</div>;
const Custom = ({ name }) => <div>Custom</div>;
const FirstResource = ({ name }) => <div>Default</div>;
Expand All @@ -20,7 +22,7 @@ describe('<RoutesWithLayout>', () => {
}));

it('should show dashboard on / when provided', () => {
const wrapper = mount(
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout dashboard={Dashboard}>
Expand All @@ -31,11 +33,12 @@ describe('<RoutesWithLayout>', () => {
</MemoryRouter>
</Provider>
);
assert.equal(wrapper.find(Dashboard).length, 1);

assert.notEqual(queryByText('Dashboard'), null);
});

it('should show the first resource on / when there is only one resource and no dashboard', () => {
const wrapper = mount(
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
Expand All @@ -45,11 +48,11 @@ describe('<RoutesWithLayout>', () => {
</Provider>
);

assert.equal(wrapper.find(FirstResource).length, 1);
assert.notEqual(queryByText('Default'), null);
});

it('should show the first resource on / when there are multiple resource and no dashboard', () => {
const wrapper = mount(
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
Expand All @@ -61,15 +64,15 @@ describe('<RoutesWithLayout>', () => {
</Provider>
);

assert.equal(wrapper.find(FirstResource).length, 1);
assert.equal(wrapper.find(Resource).length, 0);
assert.notEqual(queryByText('Default'), null);
assert.equal(queryByText('Resource'), null);
});

it('should accept custom routes', () => {
const customRoutes = [
<Route key="custom" path="/custom" component={Custom} />,
]; // eslint-disable-line react/jsx-key
const wrapper = mount(
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/custom']}>
<RoutesWithLayout customRoutes={customRoutes}>
Expand All @@ -80,6 +83,6 @@ describe('<RoutesWithLayout>', () => {
</MemoryRouter>
</Provider>
);
assert.equal(wrapper.find(Custom).length, 1);
assert.notEqual(queryByText('Custom'), null);
});
});
46 changes: 35 additions & 11 deletions packages/ra-core/src/form/FormField.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
import assert from 'assert';
import { shallow } from 'enzyme';
import { render, fireEvent, cleanup } from '@testing-library/react';
import { Form } from 'react-final-form';
import React from 'react';
import FormField from './FormField';

describe('<FormField>', () => {
const Foo = () => <div />;
afterEach(cleanup);

const Foo = ({ input }) => <input type="text" {...input} />;

it('should render a <Field/> component for the input component', () => {
const wrapper = shallow(<FormField source="title" component={Foo} />);
const component = wrapper.find('Field');
assert.equal(component.length, 1);
assert.equal(wrapper.prop('component'), Foo);
let formApi;
const { getByRole } = render(
<Form
onSubmit={jest.fn()}
render={({ form }) => {
formApi = form;
return <FormField source="title" component={Foo} />;
}}
/>
);
const input = getByRole('textbox');
fireEvent.change(input, { target: { value: 'Lorem' } });
assert.equal(formApi.getState().values.title, 'Lorem');
});
it('should not render a <Field /> component the field has an input', () => {
const wrapper = shallow(
<FormField source="title" component={Foo} input={{}} />

it('should not render a <Field /> component if the field has an input', () => {
let formApi;
const { getByRole } = render(
<Form
onSubmit={jest.fn()}
render={({ form }) => {
formApi = form;
return (
<FormField source="title" component={Foo} input={{}} />
);
}}
/>
);
const component = wrapper.find('Field');
assert.equal(component.length, 0);
const input = getByRole('textbox');
fireEvent.change(input, { target: { value: 'Lorem' } });
assert.notEqual(formApi.getState().values.title, 'Lorem');
});
});
23 changes: 16 additions & 7 deletions packages/ra-ui-materialui/src/button/CloneButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import expect from 'expect';
import { shallow } from 'enzyme';
import { ThemeProvider, createMuiTheme } from '@material-ui/core';
import { render } from '@testing-library/react';
import React from 'react';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';

import { CloneButton } from './CloneButton';

const theme = createMuiTheme();

describe('<CloneButton />', () => {
it('should pass a clone of the record in the location state', () => {
const wrapper = shallow(
<CloneButton record={{ id: 123, foo: 'bar' }} basePath="" />
const history = createMemoryHistory();
const { getByRole } = render(
<Router history={history}>
<ThemeProvider theme={theme}>
<CloneButton record={{ id: 123, foo: 'bar' }} basePath="" />
</ThemeProvider>
</Router>
);

expect(wrapper.prop('to')).toEqual(
expect.objectContaining({
search: 'source=%7B%22foo%22%3A%22bar%22%7D',
})
const button = getByRole('button');
expect(button.getAttribute('href')).toEqual(
'/create?source=%7B%22foo%22%3A%22bar%22%7D'
);
});
});
10 changes: 5 additions & 5 deletions packages/ra-ui-materialui/src/detail/SimpleShowLayout.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from 'react';
import { shallow } from 'enzyme';
import { render } from '@testing-library/react';

import SimpleShowLayout from './SimpleShowLayout';
import TextField from '../field/TextField';

describe('<SimpleShowLayout />', () => {
it('should display children inputs of SimpleShowLayout', () => {
const wrapper = shallow(
<SimpleShowLayout>
const { queryByText } = render(
<SimpleShowLayout record={{ foo: 'foo', bar: 'bar' }}>
<TextField source="foo" />
<TextField source="bar" />
</SimpleShowLayout>
);
const inputs = wrapper.find('EnhancedTextField');
expect(inputs.map(i => i.prop('source'))).toEqual(['foo', 'bar']);
expect(queryByText('foo')).not.toBeNull();
expect(queryByText('bar')).not.toBeNull();
});
});
29 changes: 15 additions & 14 deletions packages/ra-ui-materialui/src/field/FunctionField.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import React from 'react';
import assert from 'assert';
import { render, shallow } from 'enzyme';
import { render, cleanup } from '@testing-library/react';
import FunctionField from './FunctionField';

describe('<FunctionField />', () => {
afterEach(cleanup);

it('should render using the render function', () => {
const record = { foo: 'bar' };
const wrapper = render(
const { queryByText } = render(
<FunctionField record={record} render={r => r.foo.substr(0, 2)} />
);
assert.equal(wrapper.text(), 'ba');
assert.notEqual(queryByText('ba'), null);
});

it('should use custom className', () =>
assert.deepEqual(
shallow(
<FunctionField
record={{ foo: true }}
render={r => r.foo.substr(0, 2)}
className="foo"
/>
).prop('className'),
'foo'
));
it('should use custom className', () => {
const { queryByText } = render(
<FunctionField
record={{ foo: 'bar' }}
render={r => r.foo}
className="foo"
/>
);
assert.ok(queryByText('bar').classList.contains('foo'));
});
});
32 changes: 20 additions & 12 deletions packages/ra-ui-materialui/src/list/DatagridCell.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import assert from 'assert';
import React from 'react';
import PropTypes from 'prop-types';
import { shallow } from 'enzyme';
import { render, cleanup } from '@testing-library/react';

import DatagridCell from './DatagridCell';

const renderWithTable = element =>
render(
<table>
<tbody>
<tr>{element}</tr>
</tbody>
</table>
);

describe('<DatagridCell />', () => {
const Field = () => <div />;
const Field = ({ basePath }) => <div>{basePath}</div>;
Field.propTypes = {
type: PropTypes.string,
basePath: PropTypes.string,
Expand All @@ -16,25 +25,24 @@ describe('<DatagridCell />', () => {
type: 'foo',
};

it('should render as a mui <TableRowColumn /> component', () => {
const wrapper = shallow(<DatagridCell field={<Field />} />);
const col = wrapper.find('WithStyles(ForwardRef(TableCell))');
assert.equal(col.length, 1);
it('should render as a mui <TableCell /> component', () => {
const { getByRole } = renderWithTable(
<DatagridCell field={<Field />} />
);
assert.equal(getByRole('cell').className, 'MuiTableCell-root');
});

it('should pass the Datagrid basePath by default', () => {
const wrapper = shallow(
const { queryByText } = renderWithTable(
<DatagridCell basePath="default" field={<Field />} />
);
const col = wrapper.find('Field');
assert.equal(col.prop('basePath'), 'default');
assert.notEqual(queryByText('default'), null);
});

it('should allow to overwrite the `basePath` field', () => {
const wrapper = shallow(
const { queryByText } = renderWithTable(
<DatagridCell basePath="default" field={<Field basePath="new" />} />
);
const col = wrapper.find('Field');
assert.equal(col.prop('basePath'), 'new');
assert.notEqual(queryByText('new'), null);
});
});
20 changes: 10 additions & 10 deletions packages/ra-ui-materialui/src/list/PaginationActions.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import { shallow } from 'enzyme';
import { render, cleanup } from '@testing-library/react';

import PaginationActions from './PaginationActions';

describe('<PaginationActions />', () => {
afterEach(cleanup);

it('should not render any actions when no pagination is necessary', () => {
const wrapper = shallow(
const { queryAllByRole } = render(
<PaginationActions
page={0}
rowsPerPage={20}
Expand All @@ -15,13 +17,11 @@ describe('<PaginationActions />', () => {
classes={{}}
/>
);
expect(wrapper.find('WithStyles(ForwardRef(Button))')).toHaveLength(0);
expect(wrapper.find('WithStyles(ForwardRef(Typography))')).toHaveLength(
0
);
expect(queryAllByRole('button')).toHaveLength(0);
});

it('should render action buttons when pagination is necessary', () => {
const wrapper = shallow(
const { queryAllByRole } = render(
<PaginationActions
page={0}
rowsPerPage={5}
Expand All @@ -32,11 +32,11 @@ describe('<PaginationActions />', () => {
/>
);
// 1 2 3 next
expect(wrapper.find('WithStyles(ForwardRef(Button))')).toHaveLength(4);
expect(queryAllByRole('button')).toHaveLength(4);
});

it('should skip page action buttons when there are too many', () => {
const wrapper = shallow(
const { queryAllByRole } = render(
<PaginationActions
page={7}
rowsPerPage={1}
Expand All @@ -47,6 +47,6 @@ describe('<PaginationActions />', () => {
/>
);
// prev 1 ... 7 8 9 ... 15 next
expect(wrapper.find('WithStyles(ForwardRef(Button))')).toHaveLength(7);
expect(queryAllByRole('button')).toHaveLength(7);
});
});
Loading