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

Adds unit tests to Catalog actions #875

Merged
merged 6 commits into from
Feb 11, 2019
Merged

Adds unit tests to Catalog actions #875

merged 6 commits into from
Feb 11, 2019

Conversation

supernova-at
Copy link
Contributor

@supernova-at supernova-at commented Feb 8, 2019

Description

This PR adds unit tests to the Catalog's actions.js and asyncActions.js.

Related Issue

Closes #873 .

Motivation and Context

This PR is a part of the larger effort to increase unit test coverage in PWA Studio.

How Has This Been Tested?

yarn test.

Screenshots (if appropriate):

Proposed Labels for Change Type/Package

TEST

venia-concept.

Checklist:

  • I have read the CONTRIBUTING document.
  • I have linked an issue to this PR.
  • I have indicated the change type and relevant package(s).
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • All CI checks are green (linting, build/deploy, etc).
  • At least one core contributor has approved this PR.

@supernova-at supernova-at added the test This pertains to testing (unit/functional/etc). label Feb 8, 2019
@vercel
Copy link

vercel bot commented Feb 8, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

location,
queryParameter: 'page'
})
Math.floor(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is completely unrelated to the rest of this PR but since I had to spend time looking up what the ~~ operator does, I thought I'd save all future readers the effort.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is faster. Maybe a comment instead :)

Copy link
Contributor

Choose a reason for hiding this comment

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

You looked it up and now you know what it does. No need to change it. 👍

Copy link
Contributor Author

@supernova-at supernova-at Feb 11, 2019

Choose a reason for hiding this comment

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

So I dug a little deeper and wrote up a little something.

Turns out that ~~ is not a straight replacement for Math.floor, specifically for the case of negative floating point numbers.

So on one hand since we're pulling our input from the query string, we can't be certain that it will be a positive number. But on the other hand, negative page numbers don't make sense (let alone negative floating point page numbers) so I don't think we care about them.

So I reverted this change and added a comment, as @sirugh suggested (see c6e1272).

I do want to voice my opinion though, that we should strongly consider readability when we write our code - especially in Venia, which is supposed to be a reference implementation.

Is ~~ faster? Sure. Is it more concise? Yeah. Do either of those things matter here? I'd argue that they do not.

If ~~ was a widely understood and commonly used paradigm in JavaScript I'd have less of a problem, but my gut tells me that the majority of the people who run across and try to understand this code are going to have to look up double bitwise NOT, and we can make this much more developer friendly.

@@ -18,7 +18,6 @@ export const getAllCategories = () =>
export const setCurrentPage = payload =>
async function thunk(dispatch) {
dispatch(actions.setCurrentPage.receive(payload));
window.scrollTo(0, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Scrolling the window is a concern of the UI, not the action handler. See the corresponding change in category.js.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having window.scrollTo() in this particular action was a bit arbitrary—it only covered this one use case—but I don't actually think there's anything wrong with it being part of an async action. Ultimately, async actions are a natural place for side effects to live, and scrolling the window is just a side effect.

Copy link
Contributor

@sirugh sirugh 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! Just one thing -- asyncActions.js#14 was not tested, which is the error case in that file. Can you add a test for that and then maybe revert the ~~ command and add a comment?

jimbo
jimbo previously requested changes Feb 8, 2019
Copy link
Contributor

@jimbo jimbo left a comment

Choose a reason for hiding this comment

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

Good test coverage. 👍 Also left a couple of other comments.

location,
queryParameter: 'page'
})
Math.floor(
Copy link
Contributor

Choose a reason for hiding this comment

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

You looked it up and now you know what it does. No need to change it. 👍

@vercel vercel bot temporarily deployed to staging February 11, 2019 16:15 Inactive
@vercel vercel bot temporarily deployed to staging February 11, 2019 17:39 Inactive
@vercel vercel bot temporarily deployed to staging February 11, 2019 17:51 Inactive
@supernova-at
Copy link
Contributor Author

Just one thing -- asyncActions.js#14 was not tested,

Good catch, thanks for bringing that up - I'm actually not sure how to force the error case. Here's the code:

        try {
            // TODO: implement rest or graphql call for categories
            // `/rest/V1/categories` requires auth for some reason
            const { default: payload } = await import('./mockData');

            dispatch(actions.getAllCategories.receive(payload));
        } catch (error) {
            dispatch(actions.getAllCategories.receive(error));
        }

First, I think it makes more sense to come back and write a test for this path once we get a real implementation in there, and second, I legit don't know how to mock / force an error to be thrown there 😝 .

@coveralls
Copy link

coveralls commented Feb 11, 2019

Coverage Status

Coverage increased (+0.2%) to 62.131% when pulling 2de4f28 on supernova/873 into 4a042e7 on release/2.0.

@sirugh
Copy link
Contributor

sirugh commented Feb 11, 2019

I'm good with that answer @supernova-at.

@vercel vercel bot temporarily deployed to staging February 11, 2019 20:12 Inactive
@supernova-at supernova-at dismissed jimbo’s stale review February 11, 2019 20:14

Feedback was addressed

@supernova-at supernova-at merged commit e4568a8 into release/2.0 Feb 11, 2019
@supernova-at supernova-at deleted the supernova/873 branch February 11, 2019 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test This pertains to testing (unit/functional/etc).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants