Skip to content

Commit 9c84bba

Browse files
HendrikRoehmKent C. Dodds
authored andcommitted
Enhance react examples (testing-library#318)
Use extend-expect to improve the example. Additionally, `@testing-library-react` was imported in the wrong file.
1 parent cd60b15 commit 9c84bba

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

docs/example-react-redux.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import React from 'react'
6666
import { createStore } from 'redux'
6767
import { Provider } from 'react-redux'
6868
import { render, fireEvent } from '@testing-library/react'
69+
import '@testing-library/jest-dom/extend-expect'
6970
import { initialState, reducer } from './reducer.js'
7071
import Counter from './counter.js'
7172

@@ -89,15 +90,15 @@ function renderWithRedux(
8990
test('can render with redux with defaults', () => {
9091
const { getByTestId, getByText } = renderWithRedux(<Counter />)
9192
fireEvent.click(getByText('+'))
92-
expect(getByTestId('count-value').textContent).toBe('1')
93+
expect(getByTestId('count-value')).toHaveTextContent('1')
9394
})
9495

9596
test('can render with redux with custom initial state', () => {
9697
const { getByTestId, getByText } = renderWithRedux(<Counter />, {
9798
initialState: { count: 3 },
9899
})
99100
fireEvent.click(getByText('-'))
100-
expect(getByTestId('count-value').textContent).toBe('2')
101+
expect(getByTestId('count-value')).toHaveTextContent('2')
101102
})
102103

103104
test('can render with redux with custom store', () => {
@@ -107,8 +108,8 @@ test('can render with redux with custom store', () => {
107108
store,
108109
})
109110
fireEvent.click(getByText('+'))
110-
expect(getByTestId('count-value').textContent).toBe('1000')
111+
expect(getByTestId('count-value')).toHaveTextContent('1000')
111112
fireEvent.click(getByText('-'))
112-
expect(getByTestId('count-value').textContent).toBe('1000')
113+
expect(getByTestId('count-value')).toHaveTextContent('1000')
113114
})
114115
```

docs/example-react-router.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ title: React Router
88
import React from 'react'
99
import { withRouter } from 'react-router'
1010
import { Link, Route, Router, Switch } from 'react-router-dom'
11-
import { render, fireEvent } from '@testing-library/react'
1211

1312
const About = () => <div>You are on the about page</div>
1413
const Home = () => <div>You are home</div>
@@ -38,6 +37,8 @@ function App() {
3837
// app.test.js
3938
import { Router } from 'react-router-dom'
4039
import { createMemoryHistory } from 'history'
40+
import { render, fireEvent } from '@testing-library/react'
41+
import '@testing-library/jest-dom/extend-expect'
4142

4243
test('full app rendering/navigating', () => {
4344
const history = createMemoryHistory()
@@ -76,7 +77,7 @@ test('rendering a component that uses withRouter', () => {
7677
<LocationDisplay />
7778
</Router>
7879
)
79-
expect(getByTestId('location-display').textContent).toBe(route)
80+
expect(getByTestId('location-display')).toHaveTextContent(route)
8081
})
8182
```
8283

@@ -129,7 +130,7 @@ test('landing on a bad page', () => {
129130
test('rendering a component that uses withRouter', () => {
130131
const route = '/some-route'
131132
const { getByTestId } = renderWithRouter(<LocationDisplay />, { route })
132-
expect(getByTestId('location-display').textContent).toBe(route)
133+
expect(getByTestId('location-display')).toHaveTextContent(route)
133134
})
134135
```
135136

0 commit comments

Comments
 (0)