Skip to content
Merged
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
587d9e1
create example-react-hooks-useReducer
Jun 30, 2019
6ffa9a3
Update example-react-hooks-useReducer.md
Jul 23, 2019
0efd497
Update example-react-hooks-useReducer.md
Jul 23, 2019
bc239ff
Update example-react-hooks-useReducer.md
Jul 23, 2019
7db2443
Update example-react-hooks-useReducer.md
Jul 23, 2019
0266c65
Update docs/example-react-hooks-useReducer.md
Jul 24, 2019
9150dad
Update docs/example-react-hooks-useReducer.md
Jul 24, 2019
909875d
Update example-react-hooks-useReducer.md
Jul 24, 2019
6fd0bd3
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
ebcab51
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
77674e8
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
61eff1a
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
0cbaf5e
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
353311e
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
8d3b36e
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
b2b4ba5
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
e8e0ec6
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
3c643c6
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
413f8a6
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
f1f8a78
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
a8c9fc0
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
d0dafc0
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
ca79e52
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
efa40b0
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
dd87760
Update docs/example-react-hooks-useReducer.md
Jul 27, 2019
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
78 changes: 78 additions & 0 deletions docs/example-react-hooks-useReducer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Basic example showing how to test useReducer hook.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Basic example showing how to test useReducer hook.
Basic example showing how to test `useReducer` hook.


The component we are testing which changes some text depending on `stateprop1` value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The component we are testing which changes some text depending on `stateprop1` value.
The component we are testing, which changes some text depending on `isConfirmed`.

```jsx
import React, { useReducer } from 'react'

const initialState = {
stateprop1: false,
}

const Reducer = (state = initialState, action) => {
switch(action.type) {
case "SUCCESS":
return {
...state,
stateprop1: true,
}
case "FAILURE":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the "FAILURE" state isn't being used, I'd remove it from the demo, so code is as focused on the topic as it can be :)

return {
...state,
stateprop1: false,
}
default:
console.log(state)
}
}

const TestHookReducer = () => {
const [reducerState, dispatch] = useReducer(Reducer, initialState)

const dispatchActionSuccess = () => {
dispatch({ type: 'SUCCESS' })
}

const dispatchActionFailure = () => {
dispatch({ type: 'FAILURE' })
}

return (
<div>
<div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the wrapper <div> feels unnecessary:

<p> {state.isConfirmed ? 'Confirmed!' : 'Waiting for confirmation...'} </p> 

(sry I can't provide multiline suggestions 😞)

{reducerState.stateprop1
? <p>stateprop1 is true</p>
: <p>stateprop1 is false</p>}
</div>
<button onClick={dispatchActionSuccess}>
Dispatch Success
</button>
</div>
)
}

export default TestHookReducer
```


Finally our tests: We are testing to see if we get the correct output in our JSX based on the reducer state.

```js
import React from 'react'
import ReactDOM from 'react-dom'
import TestHookReducer from '../test_hook_reducer.js'
import {render, fireEvent, cleanup} from '@testing-library/react'

afterEach(cleanup)

it('Reducer changes stateprop1 from false to true', () => {
const { getByText } = render(<TestHookReducer />)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { getByText } = render(<TestHookReducer />)
const { getByText } = render(<TestHookReducer />)

expect(getByText(/stateprop1 is/i).textContent).toBe("stateprop1 is false")

fireEvent.click(getByText("Dispatch Success"))

expect(getByText(/stateprop1 is/i).textContent).toBe("stateprop1 is true")
})
```