-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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 router re-initialization bug #11301
Open
steinybot
wants to merge
3
commits into
remix-run:dev
Choose a base branch
from
steinybot:bug/render-stale-data
base: dev
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,7 @@ | |
- souzasmatheus | ||
- srmagura | ||
- stasundr | ||
- steinybot | ||
- stmtk1 | ||
- swalker326 | ||
- tanayv | ||
|
137 changes: 137 additions & 0 deletions
137
packages/react-router/__tests__/gh-issue-11300-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import * as React from "react"; | ||
import type { | ||
Dispatch, | ||
ReactNode, | ||
SetStateAction} from "react"; | ||
import { | ||
createContext, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
import { | ||
createMemoryRouter, | ||
createRoutesFromElements, | ||
Route, | ||
RouterProvider, | ||
useLoaderData, | ||
} from "react-router"; | ||
import { | ||
act, | ||
fireEvent, | ||
getByRole, | ||
getByText, | ||
render, | ||
waitFor, | ||
} from "@testing-library/react"; | ||
import { createDeferred } from "../../router/__tests__/utils/utils"; | ||
|
||
type Environment = { | ||
id: number; | ||
}; | ||
|
||
type Query = { | ||
environment: Environment; | ||
}; | ||
|
||
type State<A> = [A, Dispatch<SetStateAction<A>>]; | ||
|
||
describe("GH Issue #11300", () => { | ||
it("works", async () => { | ||
const EnvironmentContext = createContext<State<Environment> | undefined>( | ||
undefined | ||
); | ||
|
||
function useEnvironment(): Environment { | ||
const environmentState = useContext(EnvironmentContext); | ||
if (environmentState === undefined) | ||
throw new Error("EnvironmentContext.Provider is missing"); | ||
return environmentState[0]; | ||
} | ||
|
||
let environmentId = 0; | ||
|
||
function createEnvironment(): Environment { | ||
return { | ||
id: ++environmentId, | ||
}; | ||
} | ||
|
||
function useCreateNewEnvironment(): () => void { | ||
const environmentState = useContext(EnvironmentContext); | ||
if (environmentState === undefined) | ||
throw new Error("EnvironmentContext.Provider is missing"); | ||
return () => environmentState[1](createEnvironment()); | ||
} | ||
|
||
function MutableEnvironment({ children }: { children: ReactNode }) { | ||
const environmentState = useState<Environment>(createEnvironment); | ||
return ( | ||
<EnvironmentContext.Provider value={environmentState}> | ||
{children} | ||
</EnvironmentContext.Provider> | ||
); | ||
} | ||
|
||
function loadQuery(environment: Environment): Query { | ||
return { environment }; | ||
} | ||
|
||
function useQuery(query: Query): string { | ||
const environment = useEnvironment(); | ||
if (query.environment.id !== environment.id) | ||
throw new Error( | ||
"Query environment does not match environment from the context" | ||
); | ||
return `Data... (environment.id = ${environment.id})`; | ||
} | ||
|
||
function MyPage() { | ||
const query = useLoaderData() as Query; | ||
const data = useQuery(query); | ||
const createNewEnvironment = useCreateNewEnvironment(); | ||
return ( | ||
<> | ||
<p>{data}</p> | ||
<button onClick={createNewEnvironment}>Create new environment</button> | ||
</> | ||
); | ||
} | ||
|
||
function MyRouter() { | ||
const environment = useEnvironment(); | ||
let router = createMemoryRouter( | ||
createRoutesFromElements( | ||
<Route | ||
path="/" | ||
loader={() => loadQuery(environment)} | ||
element={<MyPage />} | ||
/> | ||
) | ||
); | ||
return <RouterProvider router={router} />; | ||
} | ||
|
||
let { container } = render( | ||
<MutableEnvironment> | ||
<MyRouter /> | ||
</MutableEnvironment> | ||
); | ||
|
||
// Somehow this avoids: | ||
// Warning: An update to RouterProvider inside a test was not wrapped in act(...). | ||
// I suspect that it has something to do with the way completeNavigation runs | ||
// asynchronously and will yield to render. | ||
let fooDefer = createDeferred(); | ||
await act(() => fooDefer.resolve(123)); | ||
|
||
expect(getByText(container, "Data... (environment.id = 1)")).toBeDefined(); | ||
|
||
fireEvent.click(getByRole(container, "button")); | ||
|
||
await waitFor(() => { | ||
expect( | ||
getByText(container, "Data... (environment.id = 2)") | ||
).toBeDefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": ["*"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by comment - this looks like a typo?