|
1 | 1 | import * as React from "react";
|
2 | 2 | import * as TestRenderer from "react-test-renderer";
|
3 |
| -import { MemoryRouter, Routes, Route, useMatch } from "react-router"; |
| 3 | +import { MemoryRouter, PathMatch, Routes, Route, useMatch } from "react-router"; |
4 | 4 |
|
5 | 5 | function ShowMatch({ pattern }: { pattern: string }) {
|
6 | 6 | return <pre>{JSON.stringify(useMatch(pattern), null, 2)}</pre>;
|
@@ -93,4 +93,45 @@ describe("useMatch", () => {
|
93 | 93 | `);
|
94 | 94 | });
|
95 | 95 | });
|
| 96 | + |
| 97 | + describe("when re-rendered with the same URL", () => { |
| 98 | + it("returns the memoized match", () => { |
| 99 | + let path = "/home"; |
| 100 | + let match: PathMatch<string>; |
| 101 | + let firstMatch: PathMatch<string>; |
| 102 | + |
| 103 | + function HomePage() { |
| 104 | + match = useMatch(path); |
| 105 | + |
| 106 | + if (!firstMatch) { |
| 107 | + firstMatch = match; |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + let renderer: TestRenderer.ReactTestRenderer; |
| 114 | + TestRenderer.act(() => { |
| 115 | + renderer = TestRenderer.create( |
| 116 | + <MemoryRouter initialEntries={[path]}> |
| 117 | + <Routes> |
| 118 | + <Route path={path} element={<HomePage />} /> |
| 119 | + </Routes> |
| 120 | + </MemoryRouter> |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + TestRenderer.act(() => { |
| 125 | + renderer.update( |
| 126 | + <MemoryRouter initialEntries={[path]}> |
| 127 | + <Routes> |
| 128 | + <Route path={path} element={<HomePage />} /> |
| 129 | + </Routes> |
| 130 | + </MemoryRouter> |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(match).toBe(firstMatch); |
| 135 | + }); |
| 136 | + }); |
96 | 137 | });
|
0 commit comments