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

Fix URL creation with memory history #9814

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix URL creation with memory history
  • Loading branch information
brophdawg11 committed Jan 4, 2023
commit c8971daf1193dd00db2684d2fd64c1bd5b9be53a
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/semver": "^7.3.8",
"@typescript-eslint/eslint-plugin": "^4.28.3",
"@typescript-eslint/parser": "^4.28.3",
"abort-controller": "^3.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"babel-plugin-dev-expression": "^0.2.2",
Expand Down
4 changes: 0 additions & 4 deletions packages/router/__tests__/browser-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment ./__tests__/custom-environment.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure why we had 2 ways of doing this but I moved the custom environment stuff into setup.ts

*/

/* eslint-disable jest/expect-expect */

import { JSDOM } from "jsdom";
Expand Down
19 changes: 0 additions & 19 deletions packages/router/__tests__/custom-environment.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/router/__tests__/hash-base-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment ./__tests__/custom-environment.js
*/

import { JSDOM } from "jsdom";

import type { HashHistory } from "@remix-run/router";
Expand Down
4 changes: 0 additions & 4 deletions packages/router/__tests__/hash-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment ./__tests__/custom-environment.js
*/

/* eslint-disable jest/expect-expect */

import { JSDOM } from "jsdom";
Expand Down
107 changes: 107 additions & 0 deletions packages/router/__tests__/router-memory-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* @jest-environment node
*/
Comment on lines +1 to +3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because tests by default run in jsdom, we didn't catch this regression since window was defined during test runs when using a memory history. Now we have this standalone file that runs directly in node to catch those


import { createMemoryHistory, createRouter } from "../index";

// This suite of tests specifically runs in the node jest environment to catch
// issues when window is not present
describe("a memory router", () => {
it("initializes properly", async () => {
let router = createRouter({
routes: [
{
path: "/",
},
],
history: createMemoryHistory(),
});
expect(router.state).toEqual({
historyAction: "POP",
loaderData: {},
actionData: null,
errors: null,
location: {
hash: "",
key: expect.any(String),
pathname: "/",
search: "",
state: null,
},
matches: [
{
params: {},
pathname: "/",
pathnameBase: "/",
route: {
id: "0",
path: "/",
},
},
],
initialized: true,
navigation: {
location: undefined,
state: "idle",
},
preventScrollReset: false,
restoreScrollPosition: null,
revalidation: "idle",
fetchers: new Map(),
});
router.dispose();
});

it("can create Requests without window", async () => {
let loaderSpy = jest.fn();
let router = createRouter({
routes: [
{
path: "/",
},
{
path: "/a",
loader: loaderSpy,
},
],
history: createMemoryHistory(),
});

router.navigate("/a");
expect(loaderSpy.mock.calls[0][0].request.url).toBe("unknown://unknown/a");
router.dispose();
});

it("can create URLs without window", async () => {
let shouldRevalidateSpy = jest.fn();

let router = createRouter({
routes: [
{
path: "/",
loader: () => "ROOT",
shouldRevalidate: shouldRevalidateSpy,
children: [
{
index: true,
},
{
path: "a",
},
],
},
],
history: createMemoryHistory(),
hydrationData: { loaderData: { "0": "ROOT" } },
});

router.navigate("/a");
expect(shouldRevalidateSpy.mock.calls[0][0].currentUrl.toString()).toBe(
"unknown://unknown/"
);
expect(shouldRevalidateSpy.mock.calls[0][0].nextUrl.toString()).toBe(
"unknown://unknown/a"
);
router.dispose();
});
});
16 changes: 16 additions & 0 deletions packages/router/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {
TextEncoder as NodeTextEncoder,
TextDecoder as NodeTextDecoder,
} from "util";
import { fetch, Request, Response } from "@remix-run/web-fetch";
import { AbortController as NodeAbortController } from "abort-controller";

if (!globalThis.fetch) {
// Built-in lib.dom.d.ts expects `fetch(Request | string, ...)` but the web
Expand All @@ -13,3 +18,14 @@ if (!globalThis.fetch) {
// @ts-expect-error
globalThis.Response = Response;
}

if (!globalThis.AbortController) {
// @ts-expect-error
globalThis.AbortController = NodeAbortController;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the new node test run

}

if (!globalThis.TextEncoder || !globalThis.TextDecoder) {
globalThis.TextEncoder = NodeTextEncoder;
// @ts-expect-error
globalThis.TextDecoder = NodeTextDecoder;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied over from the deleted custom environment

23 changes: 14 additions & 9 deletions packages/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,20 @@ export function parsePath(path: string): Partial<Path> {
}

export function createClientSideURL(location: Location | string): URL {
// window.location.origin is "null" (the literal string value) in Firefox
// under certain conditions, notably when serving from a local HTML file
// See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
let base =
typeof window !== "undefined" &&
typeof window.location !== "undefined" &&
window.location.origin !== "null"
? window.location.origin
: window.location.href;
// Used for in-memory history URLs
let base = "unknown://unknown";

if (typeof window !== "undefined") {
// window.location.origin is "null" (the literal string value) in Firefox
// under certain conditions, notably when serving from a local HTML file
// See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
base =
typeof window.location !== "undefined" &&
window.location.origin !== "null"
? window.location.origin
: window.location.href;
}

let href = typeof location === "string" ? location : createPath(location);
invariant(
base,
Expand Down