Skip to content

Add Test and add myself to list of contributors #60

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 4 commits into from
Apr 21, 2018

Conversation

ChrisWcs
Copy link
Contributor

A test using the new react context API was added.

I felt like it was a good example and I just wanted to contribute something to this great library.

They are all in one file react-context.js

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

This is my first Pull Request to an open source library. I hope that it might be helpful. Please let me know if there is something that needs to be changed and if it is not needed I totally understand.

@codecov
Copy link

codecov bot commented Apr 20, 2018

Codecov Report

Merging #60 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #60   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines          18     18           
=====================================
  Hits           18     18

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ded221a...c2de87b. Read the comment docs.

test('Component renders with the correct message, correctly changes message after clicking butotn', () => {
const {getByTestId} = render(<Container />)
expect(getByTestId('message').textContent).toBe('Hidden')
Simulate.click(getByTestId('button'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use here getByText method instead?

const {getByTestId} = render(<Container />)
expect(getByTestId('message').textContent).toBe('Hidden')
Simulate.click(getByTestId('button'))
expect(getByTestId('message').textContent).toBe('Secret Message')
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a custom jest matcher called toHaveTextContent, may be we can use that here..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay so I changed it to getByText but I am not sure how to use toHaveTextContent since it is a custom jest matcher. How can I pull that in? thank you!

kentcdodds
kentcdodds previously approved these changes Apr 21, 2018
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

This is super! If you could also add a link to these tests in the README (like some of the others) that'd be great :)

<StateContext.Provider
value={{
toggleStatus: this.state.toggleStatus,
handleToggle: this.handleToggle,
Copy link
Member

Choose a reason for hiding this comment

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

TL;DR: if you could rewrite this so that handleToggle is in state (you'll have to move it above the state initialization) then pass this.state into value, that'd be great! Thanks!

This is a pretty common mistake with the context API. It's actually not a problem in this specific scenario because it's simple, but I'd prefer to avoid exacerbating the common mistake by having an example here :)

Anyway, the problem here is that you generally shouldn't have a value assigned to a value that is initialized in the render method. Every time the value is changed, React will re-render all consumers, even if the object is the same.

To solve this problem, the suggested solution is to put everything into state (including event handlers). That is, if you want to expose everything that's in state to consumers. If you don't, then the convention is to set a property on state called context and pass this.state.context, which actually makes things more complicated when updating the context, but avoids unnecessary re-renders.

}
}

test('Component renders with the correct message, correctly changes message after clicking butotn', () => {
Copy link
Member

Choose a reason for hiding this comment

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

butotn -> button :)

@ChrisWcs
Copy link
Contributor Author

Okay, I think it is ready to go. Please let me know if there is anything else that needs to be fixed 😄

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Super! Thank you!

@kentcdodds kentcdodds merged commit ecf4bba into testing-library:master Apr 21, 2018
@kentcdodds
Copy link
Member

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

julienw pushed a commit to julienw/react-testing-library that referenced this pull request Dec 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants