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

[gatsby-link] Make it usable outside Gatsby #4892

Closed
rstacruz opened this issue Apr 8, 2018 · 7 comments
Closed

[gatsby-link] Make it usable outside Gatsby #4892

rstacruz opened this issue Apr 8, 2018 · 7 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more.

Comments

@rstacruz
Copy link
Contributor

rstacruz commented Apr 8, 2018

Description

If you try to use <Link /> outside of Gatsby (eg, when running tests), it fails because the component tree doesn't have the context injected by Gatsby:

    TypeError: Cannot read property 'history' of undefined

      at new GatsbyLink (node_modules/gatsby-link/index.js:107:34)
      at constructClassInstance (node_modules/react-dom/cjs/react-dom.development.js:6355:20)
      at updateClassComponent (node_modules/react-dom/cjs/react-dom.development.js:7839:9)

It'd be nice if <Link /> will fallback to a common <a href=...> in the absence of this context.

Example

If you have a component (eg, MyMenu.js) that uses gatsby-link:

import * as React from 'react'
import Link from 'gatsby-link'

export const MyMenu = () => (
  <nav>
    <Link to='/'>Home</Link>
  </nav>
)

And have a test file that renders with it (using Jest syntax here, but it doesn't matter what test harness you use):

import * as React from 'react'
import { render } from 'react-dom'
import MyMenu from '../MyMenu'

it('works', () => {
  const div = document.createElement('div')
  render(<MyMenu />, div)
})

You'll get ⚠️ TypeError: Cannot read property 'history' of undefined.

Environment

  • Gatsby version (npm list gatsby): gatsby@1.9.236
  • gatsby-cli version (gatsby --version): n/a
  • Node.js version: 8.10.0
  • Operating System: Linux
@KyleAMathews
Copy link
Contributor

Hmmm yeah. What do people normally do when testing components that rely on context?

@phs1919
Copy link

phs1919 commented Apr 15, 2018

I had the same issue when testing Gatsby Link. This comment was useful to solve it. Hope this helps you :)

@rstacruz
Copy link
Contributor Author

Testing things with context? It really depends. I typically do one of two things:

  1. Create a context-less version (eg, <MediaPlayerView>) that's used by a "connector" component (eg, <MediaPlayer>) which will convert context stuff into props. I can now just test the <MediaPlayerView> component.

  2. Create a mock context.

Anyway, I'd really love for this for react-storybook too, so I can make storybook pages out of my components that use gatsby-link!

@KyleAMathews
Copy link
Contributor

Perhaps it'd be best to just create a context-less version? Can react-storybook do aliases? E.g. in there just create a component that wraps <a>. gatsby-link just has a bunch of logic necessary to make Gatsby page loading work, etc.

@varya
Copy link

varya commented Jul 5, 2018

I have the same problem. Is it possible to provide a property which would render Link but not access context things? This way it would be visible how it looks. This is what testing cares about.

@gatsbot
Copy link

gatsbot bot commented Jan 12, 2019

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Jan 12, 2019
@gatsbot
Copy link

gatsbot bot commented Jan 24, 2019

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more.
Projects
None yet
Development

No branches or pull requests

5 participants