-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[docs] Add testing section #7101
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# Testing | ||
|
||
## Internal | ||
|
||
We take tests seriously, we have written and we maintain **a wide range** of tests so we can | ||
iterate with confidence on the components. For instance, the visual regression tests provided by [Argos-CI](https://www.argos-ci.com/callemall/material-ui) have proven to be really helpful. | ||
To learn more about our internal tests, you can have a look at the [README](https://github.com/callemall/material-ui/blob/next/test/README.md). | ||
|
||
[![Coverage Status](https://img.shields.io/codecov/c/github/callemall/material-ui/next.svg)](https://codecov.io/gh/callemall/material-ui/branch/next) | ||
|
||
## Userspace | ||
|
||
But what about writing tests in userspace? Material-UI styling infrastructure relies on the context feature of React to work correctly. You are going to need that context when testing your react components with ours. | ||
|
||
### Shallow rendering | ||
|
||
Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting on behavior of child components. | ||
We expose a `createShallow()` function for this situation. However, you will most likely not need it most of the time as shallow rendering was created to test components in isolation, without leaking children implementation details, like the context. | ||
|
||
### Full DOM rendering | ||
|
||
Full DOM rendering is ideal for use cases where you have components that may interact with DOM APIs, or may require the full lifecycle in order to fully test the component (i.e., `componentDidMount` etc.). | ||
We expose a `createMount()` function for this situation. | ||
|
||
### Render to string | ||
|
||
Rendering to string is useful to test the behavior of the components that are used on the server. | ||
You can take advantage of it to assert the generated HTML string. | ||
We expose a `createRender()` function for this sitation. | ||
|
||
## API | ||
|
||
### `createShallow([options]) => shallow` | ||
|
||
Generate an enhanced shallow function with the needed context. | ||
Please refer to the [API documentation of enzyme](http://airbnb.io/enzyme/docs/api/shallow.html) for further details of the `shallow` function. | ||
|
||
|
||
#### Arguments | ||
|
||
1. `options` (*Object* [optional]) | ||
- `options.shallow` (*Function* [optional]): The shallow function to enhance, it's using **enzyme by default**. | ||
- `options.otherContext` (*Object* [optional]): Context to be passed into the component. | ||
- `options.dive` (*Boolean* [optional]): Shallow render the one non-DOM child of the current wrapper, and return a wrapper around the result. | ||
|
||
#### Returns | ||
|
||
`shallow` (*shallow*): A shallow function. | ||
|
||
#### Examples | ||
|
||
```js | ||
import { createShallow } from 'material-ui/test-utils'; | ||
|
||
describe('<MyComponent />', () => { | ||
let shallow; | ||
|
||
before(() => { | ||
shallow = createShallow(); | ||
}); | ||
|
||
it('should work', () => { | ||
const wrapper = shallow(<MyComponent />); | ||
}); | ||
}); | ||
``` | ||
|
||
### `createMount([options]) => mount` | ||
|
||
Generate an enhanced mount function with the needed context. | ||
Please refer to the [API documentation of enzyme](http://airbnb.io/enzyme/docs/api/mount.html) for further details of the `mount` function. | ||
|
||
#### Arguments | ||
|
||
1. `options` (*Object* [optional]) | ||
- `options.mount` (*Function* [optional]): The mount function to enhance, it's using **enzyme by default**. | ||
|
||
#### Returns | ||
|
||
`mount` (*mount*): A mount function. | ||
|
||
#### Examples | ||
|
||
```js | ||
import { createMount } from 'material-ui/test-utils'; | ||
|
||
describe('<MyComponent />', () => { | ||
let mount; | ||
|
||
before(() => { | ||
mount = createMount(); | ||
}); | ||
|
||
after(() => { | ||
mount.cleanUp(); | ||
}); | ||
|
||
it('should work', () => { | ||
const wrapper = mount(<MyComponent />); | ||
}); | ||
}); | ||
``` | ||
|
||
### `createRender([options]) => render` | ||
|
||
Generate a render to string function with the needed context. | ||
Please refer to the [API documentation of enzyme](http://airbnb.io/enzyme/docs/api/render.html) for further details of the `render` function. | ||
|
||
#### Arguments | ||
|
||
1. `options` (*Object* [optional]) | ||
- `options.render` (*Function* [optional]): The render function to enhance, it's using **enzyme by default**. | ||
|
||
#### Returns | ||
|
||
`render` (*Function*): A render to string function. | ||
|
||
#### Examples | ||
|
||
```js | ||
import { createRender } from 'material-ui/test-utils'; | ||
|
||
describe('<MyComponent />', () => { | ||
let render; | ||
|
||
before(() => { | ||
render = createRender(); | ||
}); | ||
|
||
after(() => { | ||
render.cleanUp(); | ||
}); | ||
|
||
it('should work', () => { | ||
const wrapper = render(<MyComponent />); | ||
}); | ||
}); | ||
``` |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄 Well I guess you deserve the link juice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review 🥝