|
15 | 15 | // @vitest-environment happy-dom
|
16 | 16 |
|
17 | 17 | import { render } from "@testing-library/react";
|
18 |
| -import { Provider } from "jotai"; |
19 |
| -import { useHydrateAtoms } from "jotai/utils"; |
20 |
| -import { Suspense } from "react"; |
21 | 18 | import { describe, expect, it, vi, afterAll, beforeEach } from "vitest";
|
22 | 19 |
|
23 | 20 | import { currentUserIdAtom, GqlResult } from "../atoms";
|
24 |
| -import { appConfigAtom, locationAtom } from "../routing"; |
| 21 | +import { WithLocation } from "../test-utils/WithLocation"; |
25 | 22 |
|
26 | 23 | import Layout from "./Layout";
|
27 | 24 |
|
28 |
| -beforeEach(async () => { |
29 |
| - // For some reason, the locationAtom gets updated with `about:black` on render, |
30 |
| - // so we need to set a "real" location and wait for the next tick |
31 |
| - window.location.assign("https://example.com/"); |
32 |
| - // Wait the next tick for the location to update |
33 |
| - await new Promise((resolve) => setTimeout(resolve, 0)); |
34 |
| -}); |
35 |
| - |
36 |
| -const HydrateLocation: React.FC<React.PropsWithChildren<{ path: string }>> = ({ |
37 |
| - children, |
38 |
| - path, |
39 |
| -}) => { |
40 |
| - useHydrateAtoms([ |
41 |
| - [appConfigAtom, { root: "/", graphqlEndpoint: "/graphql" }], |
42 |
| - [locationAtom, { pathname: path }], |
43 |
| - ]); |
44 |
| - return <>{children}</>; |
45 |
| -}; |
46 |
| - |
47 |
| -const WithLocation: React.FC<React.PropsWithChildren<{ path: string }>> = ({ |
48 |
| - children, |
49 |
| - path, |
50 |
| -}) => { |
51 |
| - return ( |
52 |
| - <Provider> |
53 |
| - <Suspense> |
54 |
| - <HydrateLocation path={path}>{children}</HydrateLocation> |
55 |
| - </Suspense> |
56 |
| - </Provider> |
57 |
| - ); |
58 |
| -}; |
59 |
| - |
60 | 25 | describe("<Layout />", () => {
|
61 | 26 | beforeEach(() => {
|
62 | 27 | vi.spyOn(currentUserIdAtom, "read").mockResolvedValue(
|
63 | 28 | "abc123" as unknown as GqlResult<string | null>,
|
64 | 29 | );
|
65 | 30 | });
|
| 31 | + |
66 | 32 | afterAll(() => {
|
67 | 33 | vi.restoreAllMocks();
|
68 | 34 | });
|
| 35 | + |
69 | 36 | it("renders app navigation correctly", async () => {
|
70 | 37 | const component = render(
|
71 | 38 | <WithLocation path="/account">
|
|
0 commit comments