-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathreact-router.test.tsx
More file actions
63 lines (52 loc) · 1.72 KB
/
Copy pathreact-router.test.tsx
File metadata and controls
63 lines (52 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @ts-expect-error
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
import { signal } from "@preact/signals-react";
import { createElement } from "react";
import * as ReactRouter from "react-router-dom";
import { act, checkHangingAct, createRoot, Root } from "../shared/utils";
const MemoryRouter = ReactRouter.MemoryRouter;
const Routes = ReactRouter.Routes
? ReactRouter.Routes
: (ReactRouter as any).Switch; // react-router-dom v5
// @ts-expect-error We are doing a check for react-router-dom v5 vs v6 here, so
// while TS thinks ReactRouter.Routes will always be here, it isn't in v5.
const Route = ReactRouter.Routes
? ReactRouter.Route
: // react-router-dom v5 requires the element prop to be passed as children.
({ element, ...props }: any) => (
<ReactRouter.Route {...props}>{element}</ReactRouter.Route>
);
describe("@preact/signals-react", () => {
let scratch: HTMLDivElement;
let root: Root;
async function render(element: Parameters<Root["render"]>[0]) {
await act(() => root.render(element));
}
beforeEach(async () => {
scratch = document.createElement("div");
document.body.appendChild(scratch);
root = await createRoot(scratch);
});
afterEach(async () => {
checkHangingAct();
await act(() => root.unmount());
scratch.remove();
});
describe("react-router-dom", () => {
it("Route component should render", async () => {
const name = signal("World")!;
function App() {
return (
<MemoryRouter>
<Routes>
<Route path="/page1" element={<div>Page 1</div>}></Route>
<Route path="*" element={<div>Hello {name}!</div>}></Route>
</Routes>
</MemoryRouter>
);
}
await render(<App />);
expect(scratch.innerHTML).to.equal("<div>Hello World!</div>");
});
});
});