-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4446 from m4theushw/migrate-tests
[RFR] Migrate tests to react-testing-library
- Loading branch information
Showing
13 changed files
with
407 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
import assert from 'assert'; | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import { Form } from 'react-final-form'; | ||
import { render, fireEvent, cleanup } from '@testing-library/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' } }); | ||
expect(formApi.getState().values.title).toEqual('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' } }); | ||
expect(formApi.getState().values.title).not.toEqual('Lorem'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
packages/ra-ui-materialui/src/detail/SimpleShowLayout.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
30 changes: 15 additions & 15 deletions
30
packages/ra-ui-materialui/src/field/FunctionField.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
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'); | ||
expect(queryByText('ba')).not.toBeNull(); | ||
}); | ||
|
||
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" | ||
/> | ||
); | ||
expect(queryByText('bar').classList).toContain('foo'); | ||
}); | ||
}); |
Oops, something went wrong.