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

Stabilize useRevalidator #10707

Merged
merged 4 commits into from
Jul 19, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/stable-use-revalidator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Ensure `useRevalidator` is referentially stable across re-renders if revalidations are not actively occuring
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import {
import {
act,
fireEvent,
prettyDOM,
render,
screen,
waitFor,
} from "@testing-library/react";
import { JSDOM } from "jsdom";
import createDeferred from "../../router/__tests__/utils/createDeferred";
import getHtml from "react-router/__tests__/utils/getHtml";

describe("Handles concurrent mode features during navigations", () => {
function getComponents() {
Expand Down Expand Up @@ -378,36 +379,3 @@ function getWindowImpl(initialUrl: string, isHash = false): Window {
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
return dom.window as unknown as Window;
}

function getHtml(container: HTMLElement) {
return prettyDOM(container, undefined, {
highlight: false,
});
}

async function tick() {
await new Promise((r) => setTimeout(r, 0));
}

function createDeferred() {
let resolve: (val?: any) => Promise<void>;
let reject: (error?: Error) => Promise<void>;
let promise = new Promise((res, rej) => {
resolve = async (val: any) => {
res(val);
await tick();
await promise;
};
reject = async (error?: Error) => {
rej(error);
await promise.catch(() => tick());
};
});
return {
promise,
//@ts-ignore
resolve,
//@ts-ignore
reject,
};
}
43 changes: 3 additions & 40 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
fireEvent,
waitFor,
screen,
prettyDOM,
} from "@testing-library/react";
import "@testing-library/jest-dom";
import type { ErrorResponse, Fetcher } from "@remix-run/router";
Expand Down Expand Up @@ -37,6 +36,9 @@ import {
createRoutesFromElements,
} from "react-router-dom";

import createDeferred from "../../router/__tests__/utils/createDeferred";
import getHtml from "../../react-router/__tests__/utils/getHtml";

testDomRouter("<DataBrowserRouter>", createBrowserRouter, (url) =>
getWindowImpl(url, false)
);
Expand Down Expand Up @@ -5925,48 +5927,9 @@ function testDomRouter(
});
}

function createDeferred() {
let resolve: (val?: any) => Promise<void>;
let reject: (error?: Error) => Promise<void>;
let promise = new Promise((res, rej) => {
resolve = async (val: any) => {
res(val);
try {
await promise;
} catch (e) {}
};
reject = async (error?: Error) => {
rej(error);
try {
await promise;
} catch (e) {}
};
});
return {
promise,
//@ts-ignore
resolve,
//@ts-ignore
reject,
};
}

function getWindowImpl(initialUrl: string, isHash = false): Window {
// Need to use our own custom DOM in order to get a working history
const dom = new JSDOM(`<!DOCTYPE html>`, { url: "http://localhost/" });
dom.window.history.replaceState(null, "", (isHash ? "#" : "") + initialUrl);
return dom.window as unknown as Window;
}

function getHtml(container: HTMLElement) {
return prettyDOM(container, undefined, {
highlight: false,
theme: {
comment: null,
content: null,
prop: null,
tag: null,
value: null,
},
});
}
17 changes: 3 additions & 14 deletions packages/react-router-dom/__tests__/scroll-restoration-test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JSDOM } from "jsdom";
import * as React from "react";
import { render, prettyDOM, fireEvent, screen } from "@testing-library/react";
import { render, fireEvent, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import {
Link,
Expand All @@ -10,6 +10,8 @@ import {
createBrowserRouter,
} from "react-router-dom";

import getHtml from "../../react-router/__tests__/utils/getHtml";

describe(`ScrollRestoration`, () => {
it("removes the basename from the location provided to getKey", () => {
let getKey = jest.fn(() => "mykey");
Expand Down Expand Up @@ -70,16 +72,3 @@ function getWindowImpl(initialUrl: string): Window {
dom.window.history.replaceState(null, "", initialUrl);
return dom.window as unknown as Window;
}

function getHtml(container: HTMLElement) {
return prettyDOM(container, undefined, {
highlight: false,
theme: {
comment: null,
content: null,
prop: null,
tag: null,
value: null,
},
});
}