You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
260
-
261
-
First, we will install [React Testing Library](https://testing-library.com/docs/react-testing-library/intro). React Testing Library is a simple and complete React DOM testing utilities that encourage good testing practices. It uses react-dom's `render` function and `act` from react-dom/tests-utils.
259
+
First, we will install [React Testing Library](https://testing-library.com/docs/react-testing-library/intro). React Testing Library is a simple and complete React DOM testing utility that encourage good testing practices. It uses react-dom's `render` function and `act` from react-dom/tests-utils.
262
260
263
261
```sh
264
262
npm install --save-dev @testing-library/react
@@ -270,92 +268,18 @@ If you are using jest as recommended above, we also recommend installing [jest-d
270
268
npm install --save-dev @testing-library/jest-dom
271
269
```
272
270
273
-
To test the components, we `render` them into the DOM and pass stubbed callbacks as props, then we assert whether the callbacks were called when expected.
it('should not call addTodo if length of text is 0', () => {
316
-
constmockAddTodo=jest.fn()
317
-
render(<Header addTodo={mockAddTodo} />)
318
-
319
-
fireEvent.change(screen.getByPlaceholderText(/what needs to be done/i), {
320
-
target: { value:'' }
321
-
})
322
-
323
-
expect(mockAddTodo).toHaveBeenCalledTimes(0)
324
-
})
325
-
326
-
it('should call addTodo if length of text is greater than 0', () => {
327
-
constmockAddTodo=jest.fn()
328
-
render(<Header addTodo={mockAddTodo} />)
329
-
330
-
fireEvent.change(screen.getByPlaceholderText(/what needs to be done/i), {
331
-
target: { value:'Use Redux' }
332
-
})
333
-
334
-
expect(mockAddTodo).toHaveBeenCalledTimes(1)
335
-
})
336
-
```
337
-
338
-
### Connected Components
339
-
340
-
If you use a library like [React Redux](https://github.com/reduxjs/react-redux), you might be using [higher-order components](https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750) like [`connect()`](https://react-redux.js.org/api/connect). This lets you inject Redux state into a regular React component.
341
-
342
271
Consider the following `App` component:
343
272
344
273
```js
345
-
import { connect } from'react-redux'
346
-
347
-
constApp=props=> {
348
-
return<div>{props.user}</div>
349
-
}
274
+
import { useSelector } from'react-redux'
350
275
351
-
constmapStateToProps=state=> {
352
-
return state
276
+
exportdefaultfunctionApp() {
277
+
constuser=useSelector(state=>state.user)
278
+
return<div>{user}</div>
353
279
}
354
-
355
-
exportdefaultconnect(mapStateToProps)(App)
356
280
```
357
281
358
-
To test it, we can use the `wrapper` option in React Testing Library's`render` function and export our own `render` function as explained in React Testing Library's [setup docs](https://testing-library.com/docs/react-testing-library/setup).
282
+
To test the component, we `render` it into the DOM and pass stubbed callbacks as props, then we assert whether the callbacks were called when expected. We can use the `wrapper` option in the`render` function and export our own `render` function as explained in React Testing Library's [setup docs](https://testing-library.com/docs/react-testing-library/setup).
0 commit comments