Skip to content

Conversation

iqbal125
Copy link
Contributor

simple example to show to to test the useReducer hook component based on guiding principles.

simple example to show to to test the useReducer hook component based on guiding principles.
alexkrolick
alexkrolick previously approved these changes Jul 14, 2019
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.
@iqbal125
Copy link
Contributor Author

@alexkrolick @afontcu made revisions based on feedback let me know what you think.

iqbal125 and others added 3 commits July 24, 2019 11:24
Co-Authored-By: Adrià Fontcuberta <afontcu@gmail.com>
Co-Authored-By: Adrià Fontcuberta <afontcu@gmail.com>
@iqbal125
Copy link
Contributor Author

@afontcu made revisions based on feedback let me know what you think! :)

Copy link
Collaborator

@alexkrolick alexkrolick left a 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

iqbal125 and others added 7 commits July 27, 2019 10:53
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>
iqbal125 and others added 10 commits July 27, 2019 10:53
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>
@iqbal125
Copy link
Contributor Author

@alexkrolick Great Suggestions!

Copy link
Member

@afontcu afontcu left a 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.
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`.
import React, { useReducer } from 'react'

const initialState = {
isConfirmed: false,
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
isConfirmed: false,
isConfirmed: false,

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 😞)

? <p>Confirmed!</p>
: <p>Waiting for confirmation...</p>}
</div>
<button onClick={dispatch({type: 'SUCCESS'})}>
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
<button onClick={dispatch({type: 'SUCCESS'})}>
<button onClick={dispatch({type: 'SUCCESS'})}>
afterEach(cleanup)

it('shows success message on click', () => {
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 />)
it('shows success message on click', () => {
const { getByText } = render(<TestHookReducer />)

expect(getByText(/waiting/i).textContent).toBeInTheDocument()
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
expect(getByText(/waiting/i).textContent).toBeInTheDocument()
expect(getByText(/waiting/i).textContent).toBeInTheDocument()

expect(getByText(/waiting/i).textContent).toBeInTheDocument()

fireEvent.click(getByText("Confirm"))
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
fireEvent.click(getByText("Confirm"))
fireEvent.click(getByText("Confirm"))

fireEvent.click(getByText("Confirm"))

expect(getByText('Confirmed')).toBeInTheDocument()
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
expect(getByText('Confirmed')).toBeInTheDocument()
expect(getByText('Confirmed')).toBeInTheDocument()
...state,
isConfirmed: 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 :)

@@ -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.
@alexkrolick
Copy link
Collaborator

@afontcu I'm going to merge this and apply the formatting locally

@alexkrolick alexkrolick merged commit 07bcf1c into testing-library:master Jul 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants