Skip to content

Commit

Permalink
Add relative URL support to hash location links
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 25, 2018
1 parent 687b8fd commit 0962dfb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ describe("react location", () => {
document.body.removeChild(node);
});

it('should render relative hash location links', () => {
// Set before creating the hash router, JSDOM doesn't emit `hashchange`.
window.location.hash = '#!/foo/bar?test=true'

const location = new HashLocation();
const node = document.createElement("div");

document.body.appendChild(node);

render(
<Context.Provider value={location}>
<Link to="baz">Click here</Link>
</Context.Provider>,
node
);

const el = node.children[0] as HTMLAnchorElement;

expect(window.location.href).toEqual("http://localhost/#!/foo/bar?test=true");

el.click();

expect(window.location.href).toEqual("http://localhost/#!/foo/baz");

window.location.hash = ""; // Reset.
document.body.removeChild(node);
});

it("should render history location", () => {
const location = new HistoryLocation();
const node = document.createElement("div");
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export class HashLocation extends SimpleLocation {
}

format(location: string) {
return `#!${location || "/"}`;
const { pathname, search, hash } = new URL(location, this.url.href)

return `#!${pathname}${search}${hash}`;
}

unsubscribe() {
Expand Down

0 comments on commit 0962dfb

Please sign in to comment.