-
Notifications
You must be signed in to change notification settings - Fork 395
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
Conversation
dd87e44
to
7aeec0c
Compare
Codecov Report
@@ 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.
|
There was a problem hiding this 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.
tests/unit/amo/test_helpers.js
Outdated
|
||
sinon.assert.calledWith( | ||
dispatchSpy, | ||
sinon.match(onLocationChanged({ pathname })), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
addons-frontend/tests/unit/helpers.js
Lines 845 to 867 in 6dcad19
/* | |
* 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)})`); | |
}; |
There was a problem hiding this comment.
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 liblocation
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.
7aeec0c
to
1020063
Compare
1020063
to
6e402c9
Compare
There was a problem hiding this 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 }), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Fixes mozilla/addons#12502
This test case makes sure our helper function is always in sync with the
upstream lib.