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

Allow gatsby-link to be used in other contexts like Storybook #1611

Merged
merged 1 commit into from
Aug 1, 2017

Conversation

benmccormick
Copy link
Contributor

This afternoon I was experimenting with Storybook to view my Gatsby components and test different states, as preparation for some redesign work on my blog.

Unfortunately loading in components that imported gatsby-link failed in that context since __PREFIX_PATH__ was undefined. There are workarounds to this, but there doesn't seem to be any reason that gatsby-link should require that variable to be defined to be used (since it doesn't require it to actually have a value, just a defined reference) so this PR adds a typeof check, which will allow it to be imported safely into environments like Storybook.

@benmccormick
Copy link
Contributor Author

I didn't see any docs or tests that would need to be updated for this change, but if I missed a good place to add a test, let me know.

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 25, 2017

Deploy preview ready!

Built with commit ae91ff6

https://deploy-preview-1611--gatsbyjs.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 25, 2017

Deploy preview ready!

Built with commit ae91ff6

https://deploy-preview-1611--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jul 25, 2017

Deploy preview ready!

Built with commit ae91ff6

https://deploy-preview-1611--using-drupal.netlify.com

@KyleAMathews
Copy link
Contributor

This definitely makes sense, thanks!

@DSchau added some tests for gatsby-link (along with a bunch of other plugins) in #1581

Waiting on our windows CI to finish before merging it. Would love a test for this added :-)

@@ -3,7 +3,7 @@ import { Link, NavLink } from "react-router-dom"
import PropTypes from "prop-types"

let pathPrefix = ``
if (__PREFIX_PATHS__) {
if (typeof __PREFIX_PATHS__ !== "undefined" && __PREFIX_PATHS__) {
Copy link
Contributor

@Alaev Alaev Jul 25, 2017

Choose a reason for hiding this comment

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

I think it would be better to use lodash for this check as it reads better and the code can be more consistent.

import { isUndefined } from 'lodash'
_.isUndefined(__PREFIX_PATHS__)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Alaev lodash isUndefined can catch if a variable is declared and set to undefined, but it will throw a reference error like any other function call if it is passed a variable name that has not been declared at all. The only things I know of that can handle completely undeclared variable names are typeof or a try-catch block.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree Lodash is generally nicer but as gatsby-link is used on every Gatsby site out there, we want to add as little code as possible so the "raw" check in this PR is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Contributor

Choose a reason for hiding this comment

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

or use it as this:

const isUndefined = require('lodash.isUndefined');
const _ = require('lodash');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that it would look nicer though :(

Copy link
Contributor

Choose a reason for hiding this comment

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

I see I will try to dig on this one for a bit tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

@benmccormick quick one, should there be a check for null?

Choose a reason for hiding this comment

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

undefined is correct.

The null value represents an object with a non-existent reference. undefined is a clearer primitive, indicating that no value has been assigned at all.

@benmccormick
Copy link
Contributor Author

benmccormick commented Jul 25, 2017 via email

@benmccormick
Copy link
Contributor Author

benmccormick commented Jul 26, 2017

@KyleAMathews just to clarify, are we having this wait on #1581 landing? So the list looks like this?

@KyleAMathews
Copy link
Contributor

Yup! And just merged #1581 so step 1 is done :-)

@benmccormick benmccormick force-pushed the gatsby-link branch 2 times, most recently from 52d4303 to 51047fd Compare July 28, 2017 02:31
@benmccormick
Copy link
Contributor Author

@KyleAMathews and now step 2 is done :) . Test added and verified that it previously failed and now passes

@@ -19,6 +19,12 @@ const getNavigateTo = () => {
}

describe(`<Link />`, () => {

it(`does not fail to initialize when __PREFIX_PATHS__ is not defined`, () => {
Copy link
Contributor

@DSchau DSchau Jul 28, 2017

Choose a reason for hiding this comment

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

Another way you could write this (I think!) is like this

expect(() => {
  const Link = require(`../`).default
  const link = new Link({})
}).not.toThrow();

So that you're actually writing a test and not relying on side effects or anything. Also probably want to drop the eslint comments from your commit!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DSchau yeah that probably is more clear. Jest fails tests on uncaught exceptions, so its ok either way, but I'll look at that.

As for the eslint comments... eslint was warning about them in my editor so I added them. ¯_(ツ)_/¯. Any reason I shouldn't care?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DSchau updated the test. It does give nicer feedback when the test fails in that format. Good suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

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

As for the eslint comments

Only because (as far as I remember), I haven't seen any in the existing code-base, and it's pretty typically advisable to kinda keep that stuff as-is unless for a good reason! However I'll defer to @KyleAMathews on that one!

@KyleAMathews
Copy link
Contributor

Awesome, thanks!

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.

6 participants