Skip to content

Commit b48bc8b

Browse files
committed
Add additional test for <Form get> ?index param
1 parent 5095809 commit b48bc8b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/react-router-dom/__tests__/data-browser-router-test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,6 +2695,39 @@ function testDomRouter(
26952695
"/foo/bar?index&a=1#hash"
26962696
);
26972697
});
2698+
2699+
// eslint-disable-next-line jest/expect-expect
2700+
it('does not put ?index param in final URL for <Form method="get"', async () => {
2701+
let testWindow = getWindow("/form");
2702+
let router = createTestRouter(
2703+
createRoutesFromElements(
2704+
<Route path="/">
2705+
<Route path="form">
2706+
<Route
2707+
index={true}
2708+
element={
2709+
<Form>
2710+
<button type="submit" name="name" value="value">
2711+
Submit
2712+
</button>
2713+
</Form>
2714+
}
2715+
/>
2716+
</Route>
2717+
</Route>
2718+
),
2719+
{
2720+
window: testWindow,
2721+
}
2722+
);
2723+
render(<RouterProvider router={router} />);
2724+
2725+
assertLocation(testWindow, "/form", "");
2726+
2727+
fireEvent.click(screen.getByText("Submit"));
2728+
await new Promise((r) => setTimeout(r, 0));
2729+
assertLocation(testWindow, "/form", "?name=value");
2730+
});
26982731
});
26992732

27002733
describe("dynamic routes", () => {

packages/router/router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,9 +3167,9 @@ function normalizeNavigateOptions(
31673167
// Flatten submission onto URLSearchParams for GET submissions
31683168
let parsedPath = parsePath(path);
31693169
let searchParams = convertFormDataToSearchParams(opts.formData);
3170-
// Since fetcher GET submissions only run a single loader (as opposed to
3171-
// navigation GET submissions which run all loaders), we need to preserve
3172-
// any incoming ?index params
3170+
// On GET navigation submissions we can drop the ?index param from the
3171+
// resulting location since all loaders will run. But fetcher GET submissions
3172+
// only run a single loader so we need to preserve any incoming ?index params
31733173
if (isFetcher && parsedPath.search && hasNakedIndexQuery(parsedPath.search)) {
31743174
searchParams.append("index", "");
31753175
}

0 commit comments

Comments
 (0)