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

Unit test "page-detect" module #148

Merged
merged 7 commits into from Apr 15, 2016
Merged

Unit test "page-detect" module #148

merged 7 commits into from Apr 15, 2016

Conversation

DrewML
Copy link
Contributor

@DrewML DrewML commented Apr 3, 2016

@sindresorhus @paulmolluzzo @hkdobrev Still heavily working 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:

  1. Proper (or not) usage of ava
  2. Whether I should keep or scrap helpers like urisMatch. Benefit being less code duplication, downside being stack-traces with a helper on top, and some shared logic between tests
  3. window-mocking strategy. I was hoping there was an npm 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 use JSDom, 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 the page-detect module to have each public fn accept the arg for the current location.
  4. How do we want to handle the tests that hit the DOM (isRepoRoot, isCommit)?
  5. Should I use t.plan(ava feature)?

TODO:

  • Test some URIs that shouldn't match for each predicate method (isGist, isDashboard, etc)

const isGistCheck = location.hostname === 'gist.github.com';

const isGist = () => isGistCheck;
const isGist = () => location.hostname === 'gist.github.com';
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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 => {
Copy link
Member

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.

@sindresorhus
Copy link
Member

  1. Looks good! :)
  2. Keep it. The less duplication the better. We're actually working on introducing test macros to make this kind of thing simpler: [Idea]: test macros avajs/ava#695 (Thoughts welcome there)
  3. What you did looks fine.
  4. I don't see the point in overcomplicating it. I would just leave them out.
  5. Nah. Really no point in our case. See: https://github.com/sindresorhus/ava/blob/master/docs/recipes/when-to-use-plan.md

},
"devDependencies": {
"ava": "^0.13.0",
Copy link
Member

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.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

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:

  1. Don't use ES2015 features in any files under test
  2. Use babel-register in ava's require option, with the es2015 node 5 preset (or just the explicit plugins we need, as an alternative to the preset)
  3. ??

@sindresorhus
Copy link
Member

2

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

Updates pushed.


test('isDashboard', t => {
urisMatch(t, pageDetect.isDashboard, [
'https://github.com/',
Copy link
Contributor

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.

Copy link
Member

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.

@sindresorhus
Copy link
Member

One minor dumb nit, but URI => URL

@sindresorhus sindresorhus changed the title [WIP] Unit Test "page-detect" module Unit Test "page-detect" module Apr 13, 2016
@paulmolluzzo
Copy link
Contributor

@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 'isCommitList' test:

https://github.com/sindresorhus/refined-github/commits/0.13.0
https://github.com/sindresorhus/refined-github/commits/230c2
https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a68
https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a681174ddbeba6255ca040d63

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 🚀

@paulmolluzzo
Copy link
Contributor

Sorry, another realization: There's no coverage of isRepoRoot.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

@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.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

Also, re: the shortened URIs, I think it's worth testing (Good catch). I'll push an update.

@paulmolluzzo
Copy link
Contributor

And I don't see coverage of isRepoTree or isSingleFile, neither of those use the DOM though isSingleFile uses the ownername and reponame from getOwnerAndRepo that is also not tested.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

Ah yes, good call again - just merged isSingleFile in from PR #154 when addressing merge conflicts.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 13, 2016

Updates pushed :)


Object.keys(ownerAndRepo).forEach(url => {
location.href = url;
t.deepEqual(ownerAndRepo[url], pageDetect.getOwnerAndRepo());
Copy link
Contributor Author

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

@paulmolluzzo
Copy link
Contributor

What about isRepoTree? Could we get a test for that in here too? (No DOM test in that.)

Still passing and looking good otherwise.

@DrewML
Copy link
Contributor Author

DrewML commented Apr 14, 2016

Haha, how did I miss so many!? 😆 Updates pushed @paulmolluzzo

@paulmolluzzo
Copy link
Contributor

OK, I'm good with this. :shipit:

Any last thoughts @hkdobrev or @sindresorhus?

@sindresorhus sindresorhus changed the title Unit Test "page-detect" module Unit test "page-detect" module Apr 15, 2016
@sindresorhus sindresorhus merged commit 0bfd735 into master Apr 15, 2016
@sindresorhus sindresorhus deleted the ava branch April 15, 2016 10:32
@sindresorhus
Copy link
Member

Terrific work on this @DrewML. Feels good having some tests :)

d8ee7104-273c-11e5-9676-43241f2fc082

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants