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

Add a test case for onLocationChanged #6595

Merged
merged 1 commit into from Oct 16, 2018
Merged

Conversation

willdurand
Copy link
Member

Fixes mozilla/addons#12502


This test case makes sure our helper function is always in sync with the
upstream lib.

@codecov-io
Copy link

codecov-io commented Oct 11, 2018

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    mozilla/addons-frontend#6595   +/-   ##
=======================================
  Coverage   97.81%   97.81%           
=======================================
  Files         239      239           
  Lines        6512     6512           
  Branches     1239     1239           
=======================================
  Hits         6370     6370           
  Misses        127      127           
  Partials       15       15

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 a233ba2...6e402c9. Read the comment docs.

Copy link
Contributor

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

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

Thanks, I like the approach. I just have a minor change request.


sinon.assert.calledWith(
dispatchSpy,
sinon.match(onLocationChanged({ pathname })),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should not use sinon.match() because we do want to know if there are more or less props in the payload. Was there a dynamic value giving you trouble? If so, you could create a custom sinon matcher.

Copy link
Member Author

Choose a reason for hiding this comment

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

A "key" property gets generated automatically.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. Here's an example of a custom matcher:

/*
* A sinon matcher to check if the URL contains the declared params.
*
* Example:
*
* mockWindow.expects('fetch').withArgs(urlWithTheseParams({ page: 1 }))
*/
export const urlWithTheseParams = (params) => {
return sinon.match((urlString) => {
const { query } = url.parse(urlString, true);
for (const param in params) {
if (
query[param] === undefined ||
query[param] !== params[param].toString()
) {
return false;
}
}
return true;
}, `urlWithTheseParams(${JSON.stringify(params)})`);
};

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. It seems a lot of code to me, for a matcher that won't be reused.

The use of sinon.match here catches the following bugs:

  • action set to another constant in the upstream lib
  • location not set or with a different shape (i.e. pathname not present anymore)

That's why I thought that would be enough. Let me come back with something stricter yet still simple.

Copy link
Contributor

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

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

🦑


sinon.assert.calledWith(
dispatchSpy,
onLocationChanged({ pathname, key: sinon.match.string }),
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes, I was thinking of sinon.match.string but I thought it only worked for mock expectations. I think this will be helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's super interesting!

@willdurand willdurand merged commit b8f59af into master Oct 16, 2018
@willdurand willdurand deleted the test-case-onlocationchanged branch October 16, 2018 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants