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

Support URL instance as to parameter. #963

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/history/__tests__/TestSequences/PushURL.js
@@ -0,0 +1,26 @@
import expect from "expect";

import { execSteps } from "./utils.js";

export default (history, done) => {
let steps = [
({ location }) => {
expect(location).toMatchObject({
pathname: "/",
});

const nextURL = new URL("/home?the=query#the-hash", new URL('https://example.com'));
history.push(nextURL);
},
({ action, location }) => {
expect(action).toBe("PUSH");
expect(location).toMatchObject({
pathname: "/home",
search: "?the=query",
hash: "#the-hash",
});
},
];

execSteps(steps, history, done);
};
26 changes: 26 additions & 0 deletions packages/history/__tests__/TestSequences/ReplaceURL.js
@@ -0,0 +1,26 @@
import expect from "expect";

import { execSteps } from "./utils.js";

export default (history, done) => {
let steps = [
({ location }) => {
expect(location).toMatchObject({
pathname: "/",
});

const nextURL = new URL("/home?the=query#the-hash", new URL('https://example.com'));
history.replace(nextURL);
},
({ action, location }) => {
expect(action).toBe("REPLACE");
expect(location).toMatchObject({
pathname: "/home",
search: "?the=query",
hash: "#the-hash",
});
},
];

execSteps(steps, history, done);
};
14 changes: 14 additions & 0 deletions packages/history/__tests__/browser-test.js
Expand Up @@ -6,11 +6,13 @@ import Listen from "./TestSequences/Listen.js";
import PushNewLocation from "./TestSequences/PushNewLocation.js";
import PushSamePath from "./TestSequences/PushSamePath.js";
import PushState from "./TestSequences/PushState.js";
import PushURL from "./TestSequences/PushURL.js";
import PushMissingPathname from "./TestSequences/PushMissingPathname.js";
import PushRelativePathname from "./TestSequences/PushRelativePathname.js";
import ReplaceNewLocation from "./TestSequences/ReplaceNewLocation.js";
import ReplaceSamePath from "./TestSequences/ReplaceSamePath.js";
import ReplaceState from "./TestSequences/ReplaceState.js";
import ReplaceURL from "./TestSequences/ReplaceURL.js";
import EncodedReservedCharacters from "./TestSequences/EncodedReservedCharacters.js";
import GoBack from "./TestSequences/GoBack.js";
import GoForward from "./TestSequences/GoForward.js";
Expand Down Expand Up @@ -81,6 +83,12 @@ describe("a browser history", () => {
});
});

describe("push URL", () => {
it("calls change listeners with the new location", (done) => {
PushURL(history, done);
});
});

describe("push with no pathname", () => {
it("reuses the current location pathname", (done) => {
PushMissingPathname(history, done);
Expand Down Expand Up @@ -111,6 +119,12 @@ describe("a browser history", () => {
});
});

describe("replace URL", () => {
it("calls change listeners with the new location", (done) => {
ReplaceURL(history, done);
});
});

describe("location created with encoded/unencoded reserved characters", () => {
it("produces different location objects", (done) => {
EncodedReservedCharacters(history, done);
Expand Down
14 changes: 14 additions & 0 deletions packages/history/__tests__/hash-test.js
Expand Up @@ -6,11 +6,13 @@ import InitialLocationDefaultKey from "./TestSequences/InitialLocationDefaultKey
import PushNewLocation from "./TestSequences/PushNewLocation.js";
import PushSamePath from "./TestSequences/PushSamePath.js";
import PushState from "./TestSequences/PushState.js";
import PushURL from "./TestSequences/PushURL.js";
import PushMissingPathname from "./TestSequences/PushMissingPathname.js";
import PushRelativePathnameWarning from "./TestSequences/PushRelativePathnameWarning.js";
import ReplaceNewLocation from "./TestSequences/ReplaceNewLocation.js";
import ReplaceSamePath from "./TestSequences/ReplaceSamePath.js";
import ReplaceState from "./TestSequences/ReplaceState.js";
import ReplaceURL from "./TestSequences/ReplaceURL.js";
import EncodedReservedCharacters from "./TestSequences/EncodedReservedCharacters.js";
import GoBack from "./TestSequences/GoBack.js";
import GoForward from "./TestSequences/GoForward.js";
Expand Down Expand Up @@ -85,6 +87,12 @@ describe("a hash history", () => {
});
});

describe("push URL", () => {
it("calls change listeners with the new location", (done) => {
PushURL(history, done);
});
});

describe("push with no pathname", () => {
it("reuses the current location pathname", (done) => {
PushMissingPathname(history, done);
Expand Down Expand Up @@ -115,6 +123,12 @@ describe("a hash history", () => {
});
});

describe("replace URL", () => {
it("calls change listeners with the new location", (done) => {
ReplaceURL(history, done);
});
});

describe("location created with encoded/unencoded reserved characters", () => {
it("produces different location objects", (done) => {
EncodedReservedCharacters(history, done);
Expand Down
14 changes: 14 additions & 0 deletions packages/history/__tests__/memory-test.js
Expand Up @@ -6,11 +6,13 @@ import InitialLocationHasKey from "./TestSequences/InitialLocationHasKey.js";
import PushNewLocation from "./TestSequences/PushNewLocation.js";
import PushSamePath from "./TestSequences/PushSamePath.js";
import PushState from "./TestSequences/PushState.js";
import PushURL from "./TestSequences/PushURL.js";
import PushMissingPathname from "./TestSequences/PushMissingPathname.js";
import PushRelativePathnameWarning from "./TestSequences/PushRelativePathnameWarning.js";
import ReplaceNewLocation from "./TestSequences/ReplaceNewLocation.js";
import ReplaceSamePath from "./TestSequences/ReplaceSamePath.js";
import ReplaceState from "./TestSequences/ReplaceState.js";
import ReplaceURL from "./TestSequences/ReplaceURL.js";
import EncodedReservedCharacters from "./TestSequences/EncodedReservedCharacters.js";
import GoBack from "./TestSequences/GoBack.js";
import GoForward from "./TestSequences/GoForward.js";
Expand Down Expand Up @@ -84,6 +86,12 @@ describe("a memory history", () => {
});
});

describe("push URL", () => {
it("calls change listeners with the new location", (done) => {
PushURL(history, done);
});
});

describe("push with no pathname", () => {
it("reuses the current location pathname", (done) => {
PushMissingPathname(history, done);
Expand Down Expand Up @@ -114,6 +122,12 @@ describe("a memory history", () => {
});
});

describe("replace URL", () => {
it("calls change listeners with the new location", (done) => {
ReplaceURL(history, done);
});
});

describe("location created with encoded/unencoded reserved characters", () => {
it("produces different location objects", (done) => {
EncodedReservedCharacters(history, done);
Expand Down
77 changes: 37 additions & 40 deletions packages/history/index.ts
Expand Up @@ -443,18 +443,6 @@ export function createBrowserHistory(
return typeof to === "string" ? to : createPath(to);
}

// state defaults to `null` because `window.history.state` does
function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
pathname: location.pathname,
hash: "",
search: "",
...(typeof to === "string" ? parsePath(to) : to),
state,
key: createKey(),
});
}

function getHistoryStateAndUrl(
nextLocation: Location,
index: number
Expand Down Expand Up @@ -483,7 +471,7 @@ export function createBrowserHistory(

function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
push(to, state);
}
Expand All @@ -507,7 +495,7 @@ export function createBrowserHistory(

function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
replace(to, state);
}
Expand Down Expand Up @@ -693,17 +681,6 @@ export function createHashHistory(
return getBaseHref() + "#" + (typeof to === "string" ? to : createPath(to));
}

function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
pathname: location.pathname,
hash: "",
search: "",
...(typeof to === "string" ? parsePath(to) : to),
state,
key: createKey(),
});
}

function getHistoryStateAndUrl(
nextLocation: Location,
index: number
Expand Down Expand Up @@ -732,7 +709,7 @@ export function createHashHistory(

function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
push(to, state);
}
Expand Down Expand Up @@ -763,7 +740,7 @@ export function createHashHistory(

function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
replace(to, state);
}
Expand Down Expand Up @@ -891,17 +868,6 @@ export function createMemoryHistory(
return typeof to === "string" ? to : createPath(to);
}

function getNextLocation(to: To, state: any = null): Location {
return readOnly<Location>({
pathname: location.pathname,
search: "",
hash: "",
...(typeof to === "string" ? parsePath(to) : to),
state,
key: createKey(),
});
}

function allowTx(action: Action, location: Location, retry: () => void) {
return (
!blockers.length || (blockers.call({ action, location, retry }), false)
Expand All @@ -916,7 +882,7 @@ export function createMemoryHistory(

function push(to: To, state?: any) {
let nextAction = Action.Push;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
push(to, state);
}
Expand All @@ -937,7 +903,7 @@ export function createMemoryHistory(

function replace(to: To, state?: any) {
let nextAction = Action.Replace;
let nextLocation = getNextLocation(to, state);
let nextLocation = getNextLocation(location, to, state);
function retry() {
replace(to, state);
}
Expand Down Expand Up @@ -1089,3 +1055,34 @@ export function parsePath(path: string): Partial<Path> {

return parsedPath;
}

/**
* Constructs new location object.
*
* @param from The current location.
* @param to
* @param [state=null] Defaults to `null` because `window.history.state` does.
*/
function getNextLocation(from: Location, to: To, state: any = null): Location {
let normalizedTo;
if ( typeof to === "string" ) {
normalizedTo = parsePath(to);
} else if (to instanceof URL) {
normalizedTo = {
pathname: to.pathname,
search: to.search,
hash: to.hash
}
} else {
normalizedTo = to;
}

return readOnly<Location>({
pathname: from.pathname,
hash: "",
search: "",
...normalizedTo,
state,
key: createKey(),
});
}