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
Unit test "page-detect" module #148
Conversation
const isGistCheck = location.hostname === 'gist.github.com'; | ||
|
||
const isGist = () => isGistCheck; | ||
const isGist = () => location.hostname === 'gist.github.com'; |
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.
@DrewML I did this like that, because isGist()
is called for every isRepo()
call which is called for every other check. But location.hostname
can't change without invalidating the JS context, so it's safe to check it only once.
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.
Happy to change it if we can't test it otherwise though.
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.
Yeah, I changed it for the purposes of making testing a tad bit easier. We technically can test without changing it, but it requires deleting a reference from node's require.cache
and re-requiring it each time we need to make a change to location.hostname
, which doesn't seem like a great thing to be doing in tests if it can be avoided.
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.
@hkdobrev Doesn't really matter. The overhead of one function is negligible.
const {pageDetect} = window; | ||
|
||
const urisMatch = (t, fn, testURIs = []) => { | ||
testURIs.forEach(uri => { |
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.
Use a for...of
loop.
|
}, | ||
"devDependencies": { | ||
"ava": "^0.13.0", |
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.
"ava": "*",
I prefer using *
in my own things to always be on latest so I can catch error before my users do.
Huh - I've been using Babel for so long, I didn't realize that Node v5 still didn't support destructing assignment :( @sindresorhus What do you want to do for this scenario? I think our options are:
|
2 |
Updates pushed. |
|
||
test('isDashboard', t => { | ||
urisMatch(t, pageDetect.isDashboard, [ | ||
'https://github.com/', |
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.
https://github.com/dashboard
should also match.
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.
and https://github.com/dashboard
and https://github.com
.
One minor dumb nit, but |
@DrewML This is excellent, well done! Adds a lot of confidence to the URL regexes we rely on. Just a general question about the pattern of match/don't match: Many URLs also work with the full/partial commit hash (commits and tags come to mind). Do we care to create "versions" of these in the match/don't match to make sure we are testing against the lowest and highest amount? For example, these all "count", are identical in content, and the tests pass if I add them to the
Do we care to include this stuff or is that just being overkill? Either way, this is excellent and passing for me. (Node v4.4.0 and npm 3.8.0) LGTM 🚀 |
Sorry, another realization: There's no coverage of |
@paulmolluzzo See 4 here and in the first comment. We're leaving out tests which rely on the DOM (for now), but we could always revisit. |
Also, re: the shortened URIs, I think it's worth testing (Good catch). I'll push an update. |
And I don't see coverage of |
Ah yes, good call again - just merged |
Updates pushed :) |
|
||
Object.keys(ownerAndRepo).forEach(url => { | ||
location.href = url; | ||
t.deepEqual(ownerAndRepo[url], pageDetect.getOwnerAndRepo()); |
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.
Note: I updated ava to *
in package.json
in a recent commit - if t.deepEqual
fails, run npm install to update to the latest version
What about Still passing and looking good otherwise. |
Haha, how did I miss so many!? 😆 Updates pushed @paulmolluzzo |
OK, I'm good with this. Any last thoughts @hkdobrev or @sindresorhus? |
Terrific work on this @DrewML. Feels good having some tests :) |
@sindresorhus @paulmolluzzo @hkdobrev Still
heavilyworking on this, but I wanted to start getting feedback sooner rather than waiting until I'm done.Few things I'm looking for comments/discussion on:
ava
urisMatch
. Benefit being less code duplication, downside being stack-traces with a helper on top, and some shared logic between testsnpm
module that would take care of this, but I didn't find a suitable one. Let me know if you've seen one. I could probably useJSDom
, but it seems a bit heavy for this particular use-case (and I suspect it would actually make requests to the page you set as location.href). Another option would be to change thepage-detect
module to have each public fn accept the arg for the current location.isRepoRoot
,isCommit
)?t.plan
(ava feature)?TODO:
isGist
,isDashboard
, etc)