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

Move gatsby-link into gatsby #3864

Merged
merged 5 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/gatsby/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"../../.babelrc.js",
{
"browser": true
}
]
]
}
Copy link
Contributor

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.

4 changes: 4 additions & 0 deletions packages/gatsby/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
env:
browser: true
globals:
___loader: false
14 changes: 14 additions & 0 deletions packages/gatsby/index.d.ts
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> { }
5 changes: 4 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"@types/history": "^4.6.2",
"@types/react-router-dom": "4.2.2",
"async": "^2.1.2",
"autoprefixer": "^7.1.2",
"babel-code-frame": "^6.22.0",
Expand Down Expand Up @@ -89,6 +91,7 @@
"postcss-import": "^11.0.0",
"postcss-loader": "^2.0.6",
"postcss-reporter": "^5.0.0",
"prop-types": "^15.5.8",
"raw-loader": "^0.5.1",
"react": "^16.2.0",
"react-dev-utils": "^4.2.1",
Expand Down Expand Up @@ -145,7 +148,7 @@
"website"
],
"license": "MIT",
"main": "index.js",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/gatsbyjs/gatsby.git"
Expand Down
164 changes: 164 additions & 0 deletions packages/gatsby/src/components/__tests__/link.js
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}`)
})
})
})
Loading