Skip to content
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

Wrap all tests in strictmode #116

Merged
merged 7 commits into from
Dec 31, 2018

Conversation

tstirrat15
Copy link
Contributor

Motivation

This should help with development against future versions of React and enforces best practices generally.

Changes

  • Add test utils file with renderStrict and serverRenderStrict functions
  • Implement those functions across all tests

Testing

Code review. See that tests pass.

@tstirrat15
Copy link
Contributor Author

Though one thing I'm curious about is that it seems that the arity of renderStrict is wrong, and yet tests still pass. Any ideas there?

I'm also not seeing a single warning on output, which seems strange.

@tstirrat15
Copy link
Contributor Author

Mmmk. I wasn't calling the callback, which was one problem, and another problem is that the peer dependency specified in package.json is an old version of react which doesn't have strictmode, so the utils file is doing exactly as expected and making strictmode a passthrough component. I'll verify that the behavior is correct with a recent version of react and then we can figure out how we want to run tests against those versions of react.

@tstirrat15
Copy link
Contributor Author

It seems I'll have to rewrite some of the tests as well. They aren't playing nicely with React 16. I'll try and get around to this on Monday.

@edorivai
Copy link
Collaborator

Thanks a lot! Looking forward to it.

ReactDOM.render(element, node, () => {
expect(node.firstChild.innerHTML || '').not.toMatch(/hello/);
renderStrict(element, node, () => {
expect(node.innerHTML || '').not.toMatch(/hello/);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

firstChild was null, which meant that the statement was erroring out. Is this a good enough equivalent, or should it be node.firstChild && node.firstChild.innerHtml || ''?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit confused. Do you mean this test was broken before your changes? Or did this break by adding renderStrict?
In any case, I think node.innerHTML should be fine.

ReactDOM.render(element, node, () => {
expect(node.firstChild.innerHTML || '').not.toMatch(/hello/);
renderStrict(element, node, () => {
expect(node.innerHTML || '').not.toMatch(/hello/);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same thing here.

@tstirrat15
Copy link
Contributor Author

@edorivai this should be ready for review now. I left a couple of comments on the substantive changes I made to the tests.

It seems like the tests should ideally be run against multiple versions of React, unless the plan is to move to React 16 as a part of v2.

@edorivai
Copy link
Collaborator

It seems like the tests should ideally be run against multiple versions of React, unless the plan is to move to React 16 as a part of v2.

That is actually a good point. Thing is, we plan to add a hooks API in 2.1, but this would be a separate bundle (like react-media/hooks). That hooks bundle would support >=16.7, but the main bundle could still support >=15 || ^0.14.7. Not sure what would be the correct version to require in the peerDependency in that case... 🤔

Copy link
Collaborator

@edorivai edorivai left a comment

Choose a reason for hiding this comment

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

I left one suggestion which you can apply, other than that everything looks good to me!

modules/__tests__/Media-test.js Outdated Show resolved Hide resolved
ReactDOM.render(element, node, () => {
expect(node.firstChild.innerHTML || '').not.toMatch(/hello/);
renderStrict(element, node, () => {
expect(node.innerHTML || '').not.toMatch(/hello/);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit confused. Do you mean this test was broken before your changes? Or did this break by adding renderStrict?
In any case, I think node.innerHTML should be fine.

Co-Authored-By: tstirrat15 <tstirrat@gmail.com>
@tstirrat15
Copy link
Contributor Author

It wasn't broken before my changes. I think renderStrict did something, though it could also be the react 15->16 update if there were changes in how the react tree is committed to the DOM.

I'm more familiar with the enzyme method of writing tests, where it'd be pretty easy to say .find('div').text().to.equal('hello'), and the find statement will traverse and search the tree for you. Is there a way to do something similar in JSDOM? My concern is that this test wouldn't actually fail...

@edorivai
Copy link
Collaborator

I'm not that familiar with JSDOM myself, but it should be relatively straightforward to verify whether the test would fail, right? Let me know if you need help with verifying that!

@tstirrat15
Copy link
Contributor Author

I verified it by removing firstChild from each of the tests and seeing that the suite still passes. I think this is good to go.

@edorivai
Copy link
Collaborator

Yep, looks good to me. Thanks a lot!

@edorivai edorivai merged commit bca619c into ReactTraining:master Dec 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants