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

refactor(gatsby): remove parsePath from gatsby core, and move to gatsby-link #9957

Merged
merged 9 commits into from Feb 19, 2019
55 changes: 55 additions & 0 deletions packages/gatsby-link/src/__tests__/parse-path.js
@@ -0,0 +1,55 @@
import { parsePath } from "../parse-path"

test(`it defaults to root if undefined`, () => {
expect(parsePath()).toEqual({
pathname: `/`,
search: ``,
hash: ``,
})
})

test(`it uses passed path, if defined`, () => {
const path = `/admin`
expect(parsePath(path)).toEqual({
pathname: path,
search: ``,
hash: ``,
})
})

test(`it returns query string`, () => {
const search = `?some-thing=true&other-thing=false`
const pathname = `/admin`
const path = `${pathname}${search}`

expect(parsePath(path)).toEqual({
pathname,
search,
hash: ``,
})
})

test(`it returns hash`, () => {
const hash = `#some-id`
const pathname = `/admin`
const path = `${pathname}${hash}`

expect(parsePath(path)).toEqual({
pathname,
hash,
search: ``,
})
})

test(`it returns hash, search, and pathname if all defined`, () => {
const hash = `#some-id`
const pathname = `/admin`
const search = `?this-thing=true&other-thing=false`
const path = `${pathname}${search}${hash}`

expect(parsePath(path)).toEqual({
pathname,
search,
hash,
})
})
3 changes: 2 additions & 1 deletion packages/gatsby-link/src/index.js
Expand Up @@ -2,7 +2,8 @@
import PropTypes from "prop-types"
import React from "react"
import { Link } from "@reach/router"
import { parsePath } from "gatsby"

import { parsePath } from "./parse-path"

export function withPrefix(path) {
return normalizePath(`${__PATH_PREFIX__}/${path}`)
Expand Down
@@ -1,4 +1,4 @@
export default function parsePath(path) {
export function parsePath(path) {
var pathname = path || `/`
var search = ``
var hash = ``
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/gatsby-browser-entry.js
Expand Up @@ -6,9 +6,9 @@ import Link, {
push,
replace,
navigateTo,
parsePath,
} from "gatsby-link"
import PageRenderer from "./public-page-renderer"
import parsePath from "./parse-path"

const StaticQueryContext = React.createContext({})

Expand Down