- Notifications
You must be signed in to change notification settings - Fork 733
create example-react-hooks-useReducer #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
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
6ffa9a3
Update example-react-hooks-useReducer.md
0efd497
Update example-react-hooks-useReducer.md
bc239ff
Update example-react-hooks-useReducer.md
7db2443
Update example-react-hooks-useReducer.md
0266c65
Update docs/example-react-hooks-useReducer.md
9150dad
Update docs/example-react-hooks-useReducer.md
909875d
Update example-react-hooks-useReducer.md
6fd0bd3
Update docs/example-react-hooks-useReducer.md
ebcab51
Update docs/example-react-hooks-useReducer.md
77674e8
Update docs/example-react-hooks-useReducer.md
61eff1a
Update docs/example-react-hooks-useReducer.md
0cbaf5e
Update docs/example-react-hooks-useReducer.md
353311e
Update docs/example-react-hooks-useReducer.md
8d3b36e
Update docs/example-react-hooks-useReducer.md
b2b4ba5
Update docs/example-react-hooks-useReducer.md
e8e0ec6
Update docs/example-react-hooks-useReducer.md
3c643c6
Update docs/example-react-hooks-useReducer.md
413f8a6
Update docs/example-react-hooks-useReducer.md
f1f8a78
Update docs/example-react-hooks-useReducer.md
a8c9fc0
Update docs/example-react-hooks-useReducer.md
d0dafc0
Update docs/example-react-hooks-useReducer.md
ca79e52
Update docs/example-react-hooks-useReducer.md
efa40b0
Update docs/example-react-hooks-useReducer.md
dd87760
Update docs/example-react-hooks-useReducer.md
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
Update example-react-hooks-useReducer.md
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,23 +1,43 @@ | ||||||
Basic example showing how to test useReducer hook. | ||||||
| ||||||
| ||||||
The component: changes some text depending on `stateprop1` value. | ||||||
The component we are testing which changes some text depending on `stateprop1` value. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||
| ||||||
``` | ||||||
import React, { useReducer } from 'react' | ||||||
import * as ACTIONS from '../store/actions' | ||||||
import * as Reducer from '../store/reducer' | ||||||
```jsx | ||||||
import React, { useReducer } from 'react'; | ||||||
afontcu marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
| ||||||
| ||||||
const TestHookReducer = (props) => { | ||||||
const [reducerState, dispatch] = useReducer(Reducer.Reducer1, Reducer.initialState) | ||||||
const initialState = { | ||||||
stateprop1: false, | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
} | ||||||
| ||||||
const Reducer = (state = initialState, action) => { | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
switch(action.type) { | ||||||
case "SUCCESS": | ||||||
return { | ||||||
...state, | ||||||
stateprop1: true, | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
} | ||||||
case "FAILURE": | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
} | ||||||
default: | ||||||
console.log(state) | ||||||
} | ||||||
} | ||||||
| ||||||
| ||||||
const TestHookReducer = () => { | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
const [reducerState, dispatch] = useReducer(Reducer, initialState) | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
| ||||||
const dispatchActionSuccess = () => { | ||||||
dispatch(ACTIONS.SUCCESS) | ||||||
dispatch({ type: 'SUCCESS' }) | ||||||
} | ||||||
| ||||||
const dispatchActionFailure = () => { | ||||||
dispatch(ACTIONS.FAILURE) | ||||||
dispatch({ type: 'FAILURE' }) | ||||||
} | ||||||
| ||||||
| ||||||
| @@ -37,74 +57,21 @@ const TestHookReducer = (props) => { | |||||
| ||||||
| ||||||
export default TestHookReducer; | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
| ||||||
``` | ||||||
| ||||||
| ||||||
our reducer: changes stateprop1 to true based on the "SUCCESS" action. | ||||||
Finally our tests: We are testing to see if we get the correct output in our JSX based on the reducer state. | ||||||
| ||||||
``` | ||||||
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": | ||||||
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. | ||||||
| ||||||
``` | ||||||
```js | ||||||
import React from 'react' | ||||||
import ReactDOM from 'react-dom' | ||||||
import TestHookReducer from '../test_hook_reducer.js' | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
import {render, fireEvent, cleanup} from '@testing-library/react' | ||||||
import * as Reducer from '../../store/reducer' | ||||||
import * as ACTIONS from '../../store/actions' | ||||||
| ||||||
iqbal125 marked this conversation as resolved. Show resolved Hide resolved | ||||||
| ||||||
afterEach(cleanup) | ||||||
| ||||||
describe('test the reducer and actions', () => { | ||||||
it('should return the initial state', () => { | ||||||
expect(Reducer.initialState).toEqual({ stateprop1: false }) | ||||||
}) | ||||||
afterEach(cleanup) | ||||||
| ||||||
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', () => { | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
const { container, getByText } = render(<TestHookReducer />); | ||||||
iqbal125 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||||||
|
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.