-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow pass-through script props in
ScrollRestoration
(#2879)
- Loading branch information
Showing
4 changed files
with
114 additions
and
4 deletions.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@remix-run/react": patch | ||
--- | ||
|
||
Allow pass-through props to be passed to the script rendered by `ScrollRestoration` |
104 changes: 104 additions & 0 deletions
104
packages/remix-react/__tests__/scroll-restoration-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,104 @@ | ||
import * as React from "react"; | ||
import { MemoryRouter, Outlet } from "react-router-dom"; | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import { LiveReload, RemixEntryContext, Scripts } from "../components"; | ||
import type { RemixEntryContextType } from "../components"; | ||
import { ScrollRestoration } from "../scroll-restoration"; | ||
|
||
import "@testing-library/jest-dom/extend-expect"; | ||
|
||
function AppShell({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<React.Fragment> | ||
<Outlet /> | ||
{children} | ||
<Scripts /> | ||
<LiveReload /> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
describe("<ScrollRestoration />", () => { | ||
function withContext(stuff: JSX.Element) { | ||
let context: RemixEntryContextType = { | ||
routeModules: { idk: { default: () => null } }, | ||
manifest: { | ||
routes: { | ||
idk: { | ||
hasLoader: true, | ||
hasAction: false, | ||
hasCatchBoundary: false, | ||
hasErrorBoundary: false, | ||
id: "idk", | ||
module: "idk", | ||
}, | ||
}, | ||
entry: { imports: [], module: "" }, | ||
url: "", | ||
version: "", | ||
}, | ||
matches: [], | ||
clientRoutes: [ | ||
{ | ||
id: "idk", | ||
path: "idk", | ||
hasLoader: true, | ||
element: "", | ||
module: "", | ||
async action() { | ||
return {}; | ||
}, | ||
async loader() { | ||
return {}; | ||
}, | ||
}, | ||
], | ||
routeData: {}, | ||
appState: {} as any, | ||
transitionManager: { | ||
getState() { | ||
return { | ||
transition: {}, | ||
}; | ||
}, | ||
} as any, | ||
}; | ||
return ( | ||
<RemixEntryContext.Provider value={context}> | ||
<MemoryRouter>{stuff}</MemoryRouter> | ||
</RemixEntryContext.Provider> | ||
); | ||
} | ||
|
||
it("should render a <script> tag", () => { | ||
render( | ||
withContext( | ||
<AppShell> | ||
<ScrollRestoration data-testid="scroll-script" /> | ||
</AppShell> | ||
) | ||
); | ||
let script = screen.getByTestId("scroll-script"); | ||
expect(script instanceof HTMLScriptElement).toBe(true); | ||
}); | ||
|
||
it("should pass props to <script>", () => { | ||
render( | ||
withContext( | ||
<AppShell> | ||
<ScrollRestoration | ||
data-testid="scroll-script" | ||
nonce="hello" | ||
crossOrigin="anonymous" | ||
/> | ||
</AppShell> | ||
) | ||
); | ||
let script = screen.getByTestId("scroll-script"); | ||
expect(script).toHaveAttribute("nonce", "hello"); | ||
expect(script).toHaveAttribute("crossorigin", "anonymous"); | ||
}); | ||
|
||
it.todo("should restore scroll position"); | ||
}); |
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
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