Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance authored and MichaelDeBoey committed Jun 2, 2022
1 parent 98b5718 commit 0c07312
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
112 changes: 112 additions & 0 deletions packages/remix-react/__tests__/scroll-restoration-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as React from "react";
import { MemoryRouter, Outlet } from "react-router-dom";
import { fireEvent, render, screen } from "@testing-library/react";

import {
Link,
Links,
LiveReload,
Meta,
NavLink,
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");
});
2 changes: 1 addition & 1 deletion packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import type { Transition, Fetcher, Submission } from "./transition";
////////////////////////////////////////////////////////////////////////////////
// RemixEntry

interface RemixEntryContextType {
export interface RemixEntryContextType {
manifest: AssetsManifest;
matches: BaseRouteMatch<ClientRoute>[];
routeData: { [routeId: string]: RouteData };
Expand Down

0 comments on commit 0c07312

Please sign in to comment.