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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade jest and jsdom #10453

Merged
merged 2 commits into from May 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/router/__tests__/router-test.ts
Expand Up @@ -302,7 +302,6 @@ type SetupOpts = {
future?: FutureConfig;
};

let globalWindow = window;
function setup({
routes,
basename,
Expand Down Expand Up @@ -429,15 +428,18 @@ function setup({
});
}

// jsdom is making more and more properties non-configurable, so we inject our own jest-friendly window 馃槄
let window = {
...globalWindow,
// jsdom is making more and more properties non-configurable, so we inject
// our own jest-friendly window.
let testWindow = {
jenseng marked this conversation as resolved.
Show resolved Hide resolved
...window,
location: {
...globalWindow.location,
...window.location,
assign: jest.fn(),
replace: jest.fn(),
},
} as unknown as Window; // spread makes TS sad, since `window.NaN` conflicts with the `[index: number]: Window` index signature
} as unknown as Window;
// ^ Spread makes TS sad - `window.NaN` conflicts with `[index: number]: Window`

let history = createMemoryHistory({ initialEntries, initialIndex });
jest.spyOn(history, "push");
jest.spyOn(history, "replace");
Expand All @@ -447,7 +449,7 @@ function setup({
routes: enhanceRoutes(routes),
hydrationData,
future,
window,
window: testWindow,
}).initialize();

function getRouteHelpers(
Expand Down Expand Up @@ -854,7 +856,7 @@ function setup({
}

return {
window,
window: testWindow,
history,
router: currentRouter,
navigate,
Expand Down
30 changes: 13 additions & 17 deletions packages/router/router.ts
Expand Up @@ -662,8 +662,6 @@ export const IDLE_BLOCKER: BlockerUnblocked = {

const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;

const globalWindow = typeof window !== "undefined" ? window : undefined;

const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
hasErrorBoundary: Boolean(route.hasErrorBoundary),
});
Expand All @@ -677,14 +675,16 @@ const defaultMapRouteProperties: MapRoutePropertiesFunction = (route) => ({
/**
* Create a router and listen to history POP navigations
*/
export function createRouter({
window = globalWindow,
...init
}: RouterInit): Router {
export function createRouter(init: RouterInit): Router {
const routerWindow = init.window
? init.window
: typeof window !== "undefined"
? window
: undefined;
const isBrowser =
typeof window !== "undefined" &&
typeof window.document !== "undefined" &&
typeof window.document.createElement !== "undefined";
typeof routerWindow !== "undefined" &&
typeof routerWindow.document !== "undefined" &&
typeof routerWindow.document.createElement !== "undefined";
const isServer = !isBrowser;

invariant(
Expand Down Expand Up @@ -2088,19 +2088,15 @@ export function createRouter({
"Expected a location on the redirect navigation"
);
// Check if this an absolute external redirect that goes to a new origin
if (
ABSOLUTE_URL_REGEX.test(redirect.location) &&
isBrowser &&
typeof window?.location !== "undefined"
) {
if (ABSOLUTE_URL_REGEX.test(redirect.location) && isBrowser) {
let url = init.history.createURL(redirect.location);
let isDifferentBasename = stripBasename(url.pathname, basename) == null;

if (window.location.origin !== url.origin || isDifferentBasename) {
if (routerWindow.location.origin !== url.origin || isDifferentBasename) {
if (replace) {
window.location.replace(redirect.location);
routerWindow.location.replace(redirect.location);
} else {
window.location.assign(redirect.location);
routerWindow.location.assign(redirect.location);
}
return;
}
Expand Down