- 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
Conversation
simple example to show to to test the useReducer hook component based on guiding principles.
Basic example showing how to test useReducer hook. | ||
| ||
| ||
The component: changes stateprop1 from false to true which changes some text. |
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.
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.
The component: changes stateprop1 from false to true which changes some text. | |
The component: changes some text depending on `stateprop1` value. |
@alexkrolick @afontcu made revisions based on feedback let me know what you think. |
Co-Authored-By: Adrià Fontcuberta <afontcu@gmail.com>
Co-Authored-By: Adrià Fontcuberta <afontcu@gmail.com>
@afontcu made revisions based on feedback let me know what you think! :) |
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.
Added some suggestions that make the example component represent a more real-world use case
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
Co-Authored-By: Alex Krolick <alexkrolick@users.noreply.github.com>
@alexkrolick Great Suggestions! |
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.
Looking great! 🙌 Just added a bunch of whitespace related suggestions, and a couple of proposals. Let me know what you think! :)
Basic example showing how to test useReducer hook. | ||
| ||
| ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
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`. |
import React, { useReducer } from 'react' | ||
| ||
const initialState = { | ||
isConfirmed: false, |
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.
isConfirmed: false, | |
isConfirmed: false, |
| ||
return ( | ||
<div> | ||
<div> |
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.
the wrapper <div>
feels unnecessary:
<p> {state.isConfirmed ? 'Confirmed!' : 'Waiting for confirmation...'} </p>
(sry I can't provide multiline suggestions 😞)
? <p>Confirmed!</p> | ||
: <p>Waiting for confirmation...</p>} | ||
</div> | ||
<button onClick={dispatch({type: 'SUCCESS'})}> |
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.
<button onClick={dispatch({type: 'SUCCESS'})}> | |
<button onClick={dispatch({type: 'SUCCESS'})}> |
afterEach(cleanup) | ||
| ||
it('shows success message on click', () => { | ||
const { getByText } = render(<TestHookReducer />) |
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.
const { getByText } = render(<TestHookReducer />) | |
const { getByText } = render(<TestHookReducer />) |
it('shows success message on click', () => { | ||
const { getByText } = render(<TestHookReducer />) | ||
| ||
expect(getByText(/waiting/i).textContent).toBeInTheDocument() |
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.
expect(getByText(/waiting/i).textContent).toBeInTheDocument() | |
expect(getByText(/waiting/i).textContent).toBeInTheDocument() |
| ||
expect(getByText(/waiting/i).textContent).toBeInTheDocument() | ||
| ||
fireEvent.click(getByText("Confirm")) |
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.
fireEvent.click(getByText("Confirm")) | |
fireEvent.click(getByText("Confirm")) |
| ||
fireEvent.click(getByText("Confirm")) | ||
| ||
expect(getByText('Confirmed')).toBeInTheDocument() |
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.
expect(getByText('Confirmed')).toBeInTheDocument() | |
expect(getByText('Confirmed')).toBeInTheDocument() |
...state, | ||
isConfirmed: true, | ||
} | ||
case "FAILURE": |
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.
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 :)
@@ -0,0 +1,78 @@ | |||
Basic example showing how to test useReducer hook. |
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.
Basic example showing how to test useReducer hook. | |
Basic example showing how to test `useReducer` hook. |
@afontcu I'm going to merge this and apply the formatting locally |
simple example to show to to test the useReducer hook component based on guiding principles.