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

fix(plugin-catch-links): handle pathPrefix #9000

Merged
merged 10 commits into from
Oct 16, 2018

Conversation

ahinrichs
Copy link
Contributor

Remove pathPrefix before calling navigate, closes #8907

* remove pathPrefix before calling navigate
@pieh
Copy link
Contributor

pieh commented Oct 10, 2018

Should we add some tests for pathPrefix scenarios?

@ahinrichs
Copy link
Contributor Author

I thought of the same. But I have little to no experience in writing effective tests. So someone else must jump in.

@JimLynchCodes
Copy link
Contributor

hey, I have experience writing tests, and also I need this PR to be merged so my site will work. lol

...but I have zero familiarity with the gatsby codebase and not sure exactly what it is that needs to be tested and where to put the tests.

@JimLynchCodes
Copy link
Contributor

JimLynchCodes commented Oct 10, 2018

this whole class is so abstract that I'm not sure what it is really doing, but I see that the PR really only changes two functions inside one file.

In the first chunk of changes you change pathIsNotHandledByApp to be a function that takes two parameters instead of one. Can it still be called with one? also, in the test for catch-links.js I don't see any mention of pathIsNotHandledByApp.

It probably would be nice to have something like this:

describe('pathIsNotHandledByApp works properly'. () => {

    it('works with some basic case'. () => {

       const fakeDestination = ""
       const fakepathStartRegEx = ""
       const expectedResult = ""  

        expect(pathIsNotHandledByApp(fakeDestination, pathStartRegEx).to.equal(expectedResult)


    })

})

@JimLynchCodes
Copy link
Contributor

Also this. Why are we passing "jest.fn()" to the method routeThroughBrowserOrApp? Can we have more cases to run through routeThroughBrowserOrApp?

@ahinrichs
Copy link
Contributor Author

ahinrichs commented Oct 10, 2018

Actually I can't think of any situation where this PR makes things worse. I'm just reusing the regex that was created inside pathIsNotHandledByApp. Therefore I create it in the callling function and pass it to pathIsNotHandledByApp as a parameter. The only semantic change is to replace /<pathPrefix>/ with / before calling hrefHandler if the catched link starts with it. So that navigate will get the unprefixed path as it expects. In builds without pathPrefix this is / to /. And in builds with pathPrefix things don't work at all ATM.

I wouldn't hold back the merge to wait for having tests. But it is a regression from #8289. And maybe tests would have helped to avoid this. Maybe merge and create an issue that this plugin needs more tests?

* better test would be to mock history and check for actual
  result there (/pathPrefix/somePath -> /pathPrefix/somePath)
@ahinrichs
Copy link
Contributor Author

Tried my very best™... This is a test that /pathPrefix/somePath href gets passed to hrefHandler as /somePath. This is what's currently right for gatsby-link.navigate. Better test would be to check that window.history recieves click on /pathPrefix/somePath as /pathPrefix/somePath.

@ahinrichs
Copy link
Contributor Author

Found what I wanted in gatsby-link tests

@ahinrichs ahinrichs changed the title [plugin-catch-links] fix redundant pathPrefix fix(plugin-catch-links): handle pathPrefix Oct 11, 2018
@JimLynchCodes
Copy link
Contributor

nice @ahinrichs. I like your idea for the test, but how can you know from the test when window.history recieves an event?

@ahinrichs
Copy link
Contributor Author

For the best test one really should somehow mock window.history. This test is what gatsby-link.navigate - which is what is used by this module - finally passes to @reach/router.navigate. So it should be good enough to cover changes like #8289.

@ahinrichs
Copy link
Contributor Author

After some thinking of it I think the test is more of an integration test. Anyone mind if I move it there?

@ahinrichs
Copy link
Contributor Author

ahinrichs commented Oct 11, 2018

This was just a try. The integration test worked here but failed in circle.ci. So I reverted.

Actually I would like it a bit more as an integration test. The test failed for import { navigate } from "gatsby-link". I checked the circleci setup and it just calles gatsby-dev --scan-once --copy-all. When I try this locally gatsby-link and gatsby/cache-dir/navigation.js are copied. Dunno... Anyway. I'm happy with the fix and the tests.

@ahinrichs
Copy link
Contributor Author

BTW, I always knew I will fall in love with writing tests the moment I start doing it :-)

hrefHandler.mockImplementation((path) => {
getNavigate()(path)
expect(global.___navigate).toHaveBeenCalledWith(
`${pathPrefix}/someSubPath`,
Copy link
Contributor

Choose a reason for hiding this comment

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

will this test actually fail if you revert changes to src/catch-links.js? gotta say it's kind of confusing one to read ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just the one of them that simulates a build with --prefix-paths. That's the current state: module works in default builds but not in builds active prefix. But I wanted to catch the case too if any change would break the non prefix processing. As I said, these two are more of an integration test. I just don't know why I can't import "gatsby-link" there.

And yes, it was confusing. I tend to leave otherones code untouched. And this was a straight combination of the existing tests and one of the gatsby-link tests. I did some refactor and added comments.

@ahinrichs
Copy link
Contributor Author

I created another PR #9051. With this circleci setup the test from commit 9e09c65 succeeds as an integration test (see #9050). Acutally we could still merge this PR in the actual state to get the plugin fixed and maybe--if I'm right with the circle ci setup--could change this PR tests the to integration tests in another step.

@chasemccoy
Copy link
Contributor

@ahinrichs @pieh any ETA on getting this merged? This bug is causing some big headaches for me.

@pieh
Copy link
Contributor

pieh commented Oct 16, 2018

@chasemccoy I will be testing this locally today (mostly because those tests are kinda confusing to me :P)

@ahinrichs
Copy link
Contributor Author

Just on last change to import { navigate } from "gatsby" instead of from "gatsby-link".

I'm pretty confident, that this PR first and most important fixes the issue and second the tests at least do no harm. So I think it could be merged.

The only thing--beside the new tests being a bit abstract--that is not ideal is the import from gatsby-link resp. gatsby. That way it isn't a pure unit test anymore. But that could be done in the next step.

@pieh If you feel uncomfortable with the tests, would it help if we just merge the change in the regular code?

@pieh
Copy link
Contributor

pieh commented Oct 16, 2018

@pieh If you feel uncomfortable with the tests, would it help if we just merge the change in the regular code?

No, we definitely want some kind of tests to catch potential regressions, I just need to dig a bit and see if it actually tests stuff ;)

@pieh
Copy link
Contributor

pieh commented Oct 16, 2018

So it tests correctly, little verbose but better to have this in (+fix obviously) and try to figure out nicer tests (or some refactoring to not repeat a lot of code there).

I just formatted code and skipped empty tests (we shouldn't report success on empty tests - this just gives false sense of coverage)

@chasemccoy
Copy link
Contributor

Woohoo! Thank y'all for pushing this through, this will save me a lot of headaches.

@pieh pieh merged commit 6fed3e5 into gatsbyjs:master Oct 16, 2018
@ahinrichs
Copy link
Contributor Author

Actually - as an test beginner - I followed what was there. I first had a version where the test itself was in a function. But I assumed the code repetition in the tests before was for a reason. So I rewrote them to be two independent code blocks.

In the end I discovered the e2e test setup and I think this is best placed there. But this will take some days.

@ahinrichs ahinrichs deleted the v2-gatsby-plugin-catch-links-issue-8907 branch October 16, 2018 20:04
@ahinrichs
Copy link
Contributor Author

The empty tests where to give the output some kind of "story telling" impression. They were already there. But skip doesn't make much sense to me. I would then prefer to remove them (and maybe rephrase some of the other texts to stand on their own).

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

Successfully merging this pull request may close these issues.

gatsby-plugin-catch-links fails if pathPrefix exists
4 participants