|
| 1 | +# Test Guidelines |
| 2 | +We're using [Jest](https://facebook.github.io/jest/) and [Enzyme](https://github.com/airbnb/enzyme) for our unit tests. |
| 3 | + |
| 4 | +First of all, what we want with our unit tests is to ensure that our components behave the way we want. We're testing each component as a single unit. You should test your components isolated from the rest of the application (no redux involved for example) |
| 5 | + |
| 6 | +## An Example |
| 7 | + |
| 8 | +Let's say we have a component like |
| 9 | + |
| 10 | +```js |
| 11 | +class TheComponent extends React.Component { |
| 12 | + foo = name => { |
| 13 | + return `the ${name}`; |
| 14 | + } |
| 15 | + render() { |
| 16 | + return( |
| 17 | + <View> |
| 18 | + { |
| 19 | + this.props.isOpen && |
| 20 | + <Bar>Foo</Bar> |
| 21 | + } |
| 22 | + <Text>{this.props.name}</Text> |
| 23 | + <Text>{this.foo()}</Text> |
| 24 | + </View> |
| 25 | + ); |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +We would expect a couple of scenarios that test: |
| 31 | +1. `the component should render the Bar component when isOpen is truthy` |
| 32 | +2. `the component should not render the Bar component when isOpen is falsy` |
| 33 | +3. `the method foo should return 'the NAME'` |
| 34 | + |
| 35 | +As you can see, we didn't include a scenario that says something like `the component should render the name prop`, that's because testing that scenario will be like testing if React works. So please, avoid that kind of tests. |
| 36 | + |
| 37 | +## Tests folder structure |
| 38 | + |
| 39 | +All the test files live under `/__tests__/`. In that folder you'll find everything you need to keep working on tests of create new ones. If you're adding a test for a component, you should find/create its file under `/__tests__/components/the-component.js` |
| 40 | + |
| 41 | +## Tests file conventions |
| 42 | + |
| 43 | +A test file should look like: |
| 44 | + |
| 45 | +```js |
| 46 | +import React from 'react'; |
| 47 | +import { shallow } from 'enzyme'; |
| 48 | +import { TheComponent, Bar } from 'components'; // this may change depending on WHAT you want to test. utils, actions, components, etc... |
| 49 | + |
| 50 | +// Default props of the component |
| 51 | +const defaultProps = { |
| 52 | + isOpen: false, |
| 53 | + name: 'foo' |
| 54 | +}; |
| 55 | + |
| 56 | +describe('<TheComponent />', () => { // Usually, is the name of the component. |
| 57 | + it('should render the Bar component when isOpen is truthy', () => { |
| 58 | + const wrapper = shallow( |
| 59 | + <TheComponent |
| 60 | + {...defaultProps} |
| 61 | + isOpen={false} |
| 62 | + /> |
| 63 | + ); |
| 64 | + |
| 65 | + expect(wrapper.find(Bar).length).toBe(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not render the Bar component when isOpen is falsy', () => { ... }); |
| 69 | + |
| 70 | + it('should return 'the NAME' when foo received "name" as a param', () => { ... }); |
| 71 | + |
| 72 | + // ...add more tests accordingly |
| 73 | + |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +## Considerations |
| 78 | +1. Try to always use `shallow` instead of `render`, it's faster and only renders your component 1 level deep (real unit tests) |
| 79 | +2. Use the `defaultProps` object so you don't have to write all the props for each test |
| 80 | +3. Try to not nest `describe` blocks unless it's absolutely necessary |
| 81 | +4. Don't test unecessary stuff like we mentioned above |
| 82 | +5. Every `it` must start with `should...` to keep things consistent |
| 83 | + |
| 84 | +If you still have questions, don't hesitate to ask. |
0 commit comments