-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Move gatsby-link into gatsby #3864
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
97b652a
move Link component from gatsby-link to gatsby package
tsriram 8bbcace
add TypeScript definition for Link component
tsriram 5967ddd
delete babelrc from gatsby package
tsriram c9d91bd
revert moving gatsby-link source to gatsby
tsriram 926e165
browser.js to export link components
tsriram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"../../.babelrc.js", | ||
{ | ||
"browser": true | ||
} | ||
] | ||
] | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
env: | ||
browser: true | ||
globals: | ||
___loader: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as React from "react"; | ||
import { NavLinkProps } from "react-router-dom"; | ||
|
||
export interface GatsbyLinkProps extends NavLinkProps { | ||
onClick?: (event: any) => void | ||
className?: string | ||
style?: any; | ||
} | ||
|
||
export const navigateTo: (path: string) => void; | ||
|
||
export const withPrefix: (path: string) => string; | ||
|
||
export class Link extends React.Component<GatsbyLinkProps, any> { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import { MemoryRouter } from "react-router-dom" | ||
|
||
const getInstance = (props, pathPrefix = ``) => { | ||
Object.assign(global.window, { | ||
__PREFIX_PATHS__: pathPrefix ? true : false, | ||
__PATH_PREFIX__: pathPrefix, | ||
}) | ||
|
||
const context = { router: { history: {} } } | ||
|
||
const Link = require(`../link`).default | ||
return new Link(props, context) | ||
} | ||
|
||
const getNavigateTo = () => { | ||
Object.assign(global.window, { | ||
___navigateTo: jest.fn(), | ||
}) | ||
|
||
return require(`../link`).navigateTo | ||
} | ||
|
||
const getWithPrefix = (pathPrefix = ``) => { | ||
Object.assign(global.window, { | ||
__PREFIX_PATHS__: pathPrefix ? true : false, | ||
__PATH_PREFIX__: pathPrefix, | ||
}) | ||
return require(`../link`).withPrefix | ||
} | ||
|
||
describe(`<Link />`, () => { | ||
it(`does not fail to initialize when __PREFIX_PATHS__ is not defined`, () => { | ||
expect(() => { | ||
const context = { router: { history: {} } } | ||
const Link = require(`../link`).default | ||
const link = new Link({}, context) //eslint-disable-line no-unused-vars | ||
}).not.toThrow() | ||
}) | ||
|
||
describe(`path prefixing`, () => { | ||
it(`does not include path prefix by default`, () => { | ||
const to = `/path` | ||
const instance = getInstance({ | ||
to, | ||
}) | ||
|
||
expect(instance.state.to.pathname).toEqual(to) | ||
}) | ||
|
||
/* | ||
* Running _both_ of these tests causes the globals to be cached or something | ||
*/ | ||
it.skip(`will use __PATH_PREFIX__ if __PREFIX_PATHS__ defined`, () => { | ||
const to = `/path` | ||
const pathPrefix = `/blog` | ||
|
||
const instance = getInstance( | ||
{ | ||
to, | ||
}, | ||
pathPrefix | ||
) | ||
|
||
expect(instance.state.to).toEqual(`${pathPrefix}${to}`) | ||
}) | ||
}) | ||
|
||
describe(`the location to link to`, () => { | ||
global.___loader = { | ||
enqueue: jest.fn(), | ||
} | ||
|
||
it(`accepts to as a string`, () => { | ||
const location = `/courses?sort=name` | ||
|
||
const node = document.createElement(`div`) | ||
const Link = require(`../link`).default | ||
|
||
ReactDOM.render( | ||
<MemoryRouter> | ||
<Link to={location}>link</Link> | ||
</MemoryRouter>, | ||
node | ||
) | ||
|
||
const href = node.querySelector(`a`).getAttribute(`href`) | ||
|
||
expect(href).toEqual(location) | ||
}) | ||
|
||
it(`accepts a location "to" prop`, () => { | ||
const location = { | ||
pathname: `/courses`, | ||
search: `?sort=name`, | ||
hash: `#the-hash`, | ||
state: { fromDashboard: true }, | ||
} | ||
|
||
const node = document.createElement(`div`) | ||
const Link = require(`../link`).default | ||
|
||
ReactDOM.render( | ||
<MemoryRouter> | ||
<Link to={location}>link</Link> | ||
</MemoryRouter>, | ||
node | ||
) | ||
|
||
const href = node.querySelector(`a`).getAttribute(`href`) | ||
|
||
expect(href).toEqual(`/courses?sort=name#the-hash`) | ||
}) | ||
|
||
it(`resolves to with no pathname using current location`, () => { | ||
const location = { | ||
search: `?sort=name`, | ||
hash: `#the-hash`, | ||
} | ||
|
||
const node = document.createElement(`div`) | ||
const Link = require(`../link`).default | ||
|
||
ReactDOM.render( | ||
<MemoryRouter initialEntries={[`/somewhere`]}> | ||
<Link to={location}>link</Link> | ||
</MemoryRouter>, | ||
node | ||
) | ||
|
||
const href = node.querySelector(`a`).getAttribute(`href`) | ||
|
||
expect(href).toEqual(`/somewhere?sort=name#the-hash`) | ||
}) | ||
}) | ||
|
||
it(`navigateTo is called with correct args`, () => { | ||
getNavigateTo()(`/some-path`) | ||
|
||
expect(global.window.___navigateTo).toHaveBeenCalledWith(`/some-path`) | ||
}) | ||
}) | ||
|
||
describe(`withRouter`, () => { | ||
describe(`works with default prefix`, () => { | ||
it(`default prefix does not return "//"`, () => { | ||
const to = `/` | ||
const root = getWithPrefix()(to) | ||
expect(root).toEqual(to) | ||
}) | ||
|
||
/* | ||
* Same as above, settings a path perfix does not work because the | ||
* link module sets variables on first import | ||
*/ | ||
it.skip(`respects path prefix`, () => { | ||
const to = `/abc/` | ||
const pathPrefix = `/blog` | ||
const root = getWithPrefix(pathPrefix)(to) | ||
expect(root).toEqual(`${pathPrefix}${to}`) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is correct, you shouldn't need the babelrc here, the existing setup will handle it correctly when it's imported in the browser code.