Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
🐛 Fixed: when a <Link> explicitly receive an absolute url from th…
Browse files Browse the repository at this point in the history
…e same domain, but not the same project base path, a normal ``<a>`` is rendered
  • Loading branch information
MoOx committed Nov 22, 2016
1 parent 719e842 commit a6e7911
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
33 changes: 32 additions & 1 deletion src/components/Link/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import expectJSX from "expect-jsx"
import dom from "../../../_utils/jsdom"
import Link from "../"

dom("http://localhost/test/")
process.env.PHENOMIC_USER_URL = "http://localhost/test/"
process.env.PHENOMIC_USER_PATHNAME = "/test/"
dom(process.env.PHENOMIC_USER_URL)

expect.extend(expectJSX)

Expand Down Expand Up @@ -67,6 +68,36 @@ test("should render normal <a> tag if to is not 'local'", () => {
.toEqual("http://test.com")
})

test("should render normal <a> tag if to is not in the same root path", () => {
const component = renderer(
createElement(
Link,
{
to: "http://localhost/root",
className: "foo",
children: <span />,
},
),
{
router: {
isActive: () => false,
},
}
)

expect(component.props.href)
.toEqual("http://localhost/root")

expect(component).toEqualJSX(
<a
href="http://localhost/root"
className="foo"
>
<span />
</a>
)
})

test("should allow passing props to <a> tag", () => {
const component = renderer(
createElement(
Expand Down
19 changes: 14 additions & 5 deletions src/components/Link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,29 @@ function Link(
return simpleLink
}

const link = document.createElement("a")
link.href = to
const toLink = document.createElement("a")
toLink.href = to

if (
origin(link) === origin(window.location)
// parent absolute url with the same domain
// should not be Link
to.indexOf("://") > -1 &&
to.indexOf(process.env.PHENOMIC_USER_URL) === -1
) {
return simpleLink
}

if (
origin(toLink) === origin(window.location)
// we might want to restrict Link to path including the pathname
// but this will require to preprend pathname to all Links from the
// collection, which sucks.
// If people wants to use Link for a same domain, but in the parent path,
// you will need to includes the entire url, / won't work at it will use
// the react-router basename defined by Phenomic.
// &&
// link.pathname.includes(process.env.PHENOMIC_USER_PATHNAME)
// link.pathname.indexOf(process.env.PHENOMIC_USER_PATHNAME) > -1
// toLink.pathname.includes(process.env.PHENOMIC_USER_PATHNAME)
// toLink.pathname.indexOf(process.env.PHENOMIC_USER_PATHNAME) > -1
) {
return (
<RouterLink
Expand Down

0 comments on commit a6e7911

Please sign in to comment.