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

LCV container tests #809

Merged
merged 21 commits into from May 22, 2019
Merged

LCV container tests #809

merged 21 commits into from May 22, 2019

Conversation

srallen
Copy link
Contributor

@srallen srallen commented May 9, 2019

Package: lib-classifier

For #650

Describe your changes:
A first step toward the refactor by adding tests to the LCV container component. I plan on moving the store connection from the LCV to the LCV container and I want to make sure what is existing doesn't get broken in that process.

I have a single failing test that is a bit of a mystery to me at the moment. It calls the onReady prop on successful load passes if isolated with only, but fails if run altogether with the block. I figured out that I was missing a config for nock to work for multiple tests.

Review Checklist

General

  • Are the tests passing locally and on Travis?
  • Is the documentation up to date?
  • Is the changelog updated?

Apps

  • Does it work in all major browsers: Firefox, Chrome, Edge, Safari?
  • Does it work on mobile?
  • Can you rm -rf node_modules/ && npm install and app works as expected?

@srallen srallen added this to the LCV Refactor milestone May 9, 2019
@srallen srallen requested a review from a team May 9, 2019 19:16
@srallen srallen added this to To do in Classifier via automation May 9, 2019
@srallen srallen added the refactor Refactoring existing code label May 9, 2019
@srallen srallen changed the title [WIP] LCV container tests LCV container tests May 9, 2019
Copy link
Contributor

@eatyourgreens eatyourgreens left a comment

Choose a reason for hiding this comment

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

I have some questions about the changes to the LCV container component. I'm not sure about modifying the React lifecycle methods in order to get the tests to work.

If methods are modified to return a value then there should be a default return value, outside of any if blocks, for consistency.

@srallen
Copy link
Contributor Author

srallen commented May 10, 2019

Could we have waiting until merging #810 until I had a chance to respond?

@srallen
Copy link
Contributor Author

srallen commented May 10, 2019

We discussed potentially using the onReady prop to spy on instead.

@srallen
Copy link
Contributor Author

srallen commented May 10, 2019

A few minutes of experimentation I've found that spying on onReady isn't useful for testing error states since onReady won't be called. We could spy on onError, but we'll have the same problem of figuring out how to tell the test to wait for the async calls to resolve.

I experimented with disabling the lifecycle methods and found that it worked for testing on waiting for a single promise to resolve, but not a chain of them. For example, my test for it 'should error if the location request response fails' is failing doing this because the test expectations run before the await for the http request resolves.

I really think returning the promise from componentDidMount is cleanest and most reliable in this case. I've found another example doing a similar approach: https://medium.com/@manastunga/unit-testing-api-calls-in-react-enzyme-and-jest-133b87aaacb4

One last approach could be flushing the promises, which I haven't tried yet: https://medium.com/@lucksp_22012/jest-enzyme-react-testing-with-async-componentdidmount-7c4c99e77d2d

@eatyourgreens
Copy link
Contributor

async componentDidMount seems much less brittle than conditionally returning a Promise if a specific prop is present. You don't have to check whether componentDidMount().then exists before you use it.

Should onError happen in the store, rather than the component? #815 has made me wonder if there should be a common error handler for these components.

@eatyourgreens
Copy link
Contributor

In #815 I'm splitting the viewer tests into without a subject, with a valid subject and with an invalid subject, which might be a useful way to organise the tests here too.

@srallen
Copy link
Contributor Author

srallen commented May 13, 2019

Should onError happen in the store, rather than the component? #815 has made me wonder if there should be a common error handler for these components.

If ready is renamed to be loading and used the async states and then we also stored the error message in the store, then that will be more consistent with the other store APIs. I'd be for this refactor.

@srallen
Copy link
Contributor Author

srallen commented May 13, 2019

async componentDidMount seems much less brittle than conditionally returning a Promise if a specific prop is present. You don't have to check whether componentDidMount().then exists before you use it.

I'll give this approach a try. I can see your point about using a return for the other promise being inconsistent since it is within a conditional while making componentDidMount async will guarantee that a promise is returned.

@eatyourgreens
Copy link
Contributor

I'm going to update #815 to add loadingState to the subject viewer store.

@srallen
Copy link
Contributor Author

srallen commented May 14, 2019

This will be updated to use the improved error handling introduced in #815

@srallen
Copy link
Contributor Author

srallen commented May 15, 2019

This is ready for review again. It's been updated to use the error handling in the subject viewer store.

Copy link
Contributor

@eatyourgreens eatyourgreens left a comment

Choose a reason for hiding this comment

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

I have some questions about changes to the error handling in the component itself. There's a couple of places where I think the code is catching errors then failing silently, where previously errors were logged to the console. I'm not sure if that's intended.

In the tests, you could stub the onReady and onError callbacks to check expectations and call done() after they've run, which might make the code cleaner. That would guard against any cases where chained promises mean your initial promise resolves before the test has finished running.

@srallen
Copy link
Contributor Author

srallen commented May 21, 2019

@eatyourgreens I've tidied this up a bit. I think it's ready for review again.

Copy link
Contributor

@eatyourgreens eatyourgreens left a comment

Choose a reason for hiding this comment

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

Looks good to me. I think we've been talking at cross purposes about how try/catch works. I'm suggesting that requestData should always return a value for rawData. We've enforced consistent returns for functions in PFE, and I've got into the habit of looking for that in code.

If requestData sometimes returns a data object, and sometimes an error or undefined, then that would be inconsistent and require more code to decide how to handle the return value, as you noted. My preference is to just keep the return type consistent for functions.

@eatyourgreens
Copy link
Contributor

There was a stray .only() in the tests, so I've removed it using the PR suggestion/commit feature.

@srallen
Copy link
Contributor Author

srallen commented May 22, 2019

@eatyourgreens this is ready for review again

@eatyourgreens eatyourgreens merged commit a50456f into master May 22, 2019
Classifier automation moved this from To do to Done May 22, 2019
@eatyourgreens eatyourgreens deleted the lcv-tests branch May 22, 2019 15:25
@srallen srallen mentioned this pull request Sep 12, 2019
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring existing code
Projects
Classifier
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants