Skip to content
Merged
Changes from 1 commit
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
119 changes: 119 additions & 0 deletions docs/example-react-hooks-useReducer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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: changes stateprop1 from false to true which changes some text.
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest mentioning that the component changes the DOM, since this is the main benefit of using Testing Library tools - focusing on DOM changes and events, rather than component instances.

Suggested change
The component: changes stateprop1 from false to true which changes some text.
The component: changes some text depending on `stateprop1` value.

```
import React, { useReducer } from 'react';
import * as ACTIONS from '../store/actions'
import * as Reducer from '../store/reducer'


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

const dispatchActionSuccess = () => {
dispatch(ACTIONS.SUCCESS)
}

const dispatchActionFailure = () => {
dispatch(ACTIONS.FAILURE)
}


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


export default TestHookReducer;

```


our reducer: changes stateprop1 to true based on the "SUCCESS" action.

```
import * as ACTIONS from './actions'

export const initialState = {
stateprop1: false,
}

export const Reducer1 = (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)
}
}
```

our actions hold strings of their type.

```

export const SUCCESS = {
type: 'SUCCESS'
}

export const FAILURE = {
type: 'FAILURE'
}

```
Finally our tests: we first test our reducer to make sure it is returning the correct state based on a action.
Then we test to see if we get the correct output in our JSX based on the reducer state.

```
import React from 'react';
import ReactDOM from 'react-dom';
import TestHookReducer from '../test_hook_reducer.js';
import {render, fireEvent, cleanup} from '@testing-library/react';
import * as Reducer from '../../store/reducer';
import * as ACTIONS from '../../store/actions';


afterEach(cleanup)

describe('test the reducer and actions', () => {
it('should return the initial state', () => {
expect(Reducer.initialState).toEqual({ stateprop1: false })
})

it('should change stateprop1 from false to true', () => {
expect(Reducer.Reducer1(Reducer.initialState, ACTIONS.SUCCESS ))
.toEqual({ stateprop1: true })
})
})

it('Reducer changes stateprop1 from false to true', () => {
const { container, 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")
})

```