Skip to content

Commit e021a29

Browse files
committed
Fix loader request method
1 parent 0729641 commit e021a29

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5052,12 +5052,21 @@ describe("a router", () => {
50525052
// Assert request internals, cannot do a deep comparison above since some
50535053
// internals aren't the same on separate creations
50545054
let request = nav.actions.tasks.stub.mock.calls[0][0].request;
5055-
expect(request.url).toBe("http://localhost/tasks");
50565055
expect(request.method).toBe("POST");
5056+
expect(request.url).toBe("http://localhost/tasks");
50575057
expect(request.headers.get("Content-Type")).toBe(
50585058
"application/x-www-form-urlencoded;charset=UTF-8"
50595059
);
50605060
expect((await request.formData()).get("query")).toBe("params");
5061+
5062+
await nav.actions.tasks.resolve("TASKS ACTION");
5063+
let rootLoaderRequest = nav.loaders.root.stub.mock.calls[0][0].request;
5064+
expect(rootLoaderRequest.method).toBe("GET");
5065+
expect(rootLoaderRequest.url).toBe("http://localhost/tasks");
5066+
5067+
let tasksLoaderRequest = nav.loaders.tasks.stub.mock.calls[0][0].request;
5068+
expect(tasksLoaderRequest.method).toBe("GET");
5069+
expect(tasksLoaderRequest.url).toBe("http://localhost/tasks");
50615070
});
50625071

50635072
it("sends proper arguments to actions (using query string)", async () => {
@@ -10343,9 +10352,12 @@ describe("a router", () => {
1034310352
}
1034410353

1034510354
function createSubmitRequest(path: string, opts?: RequestInit) {
10355+
let searchParams = new URLSearchParams();
10356+
searchParams.append("key", "value");
10357+
1034610358
return createRequest(path, {
1034710359
method: "post",
10348-
body: createFormData({ key: "value" }),
10360+
body: searchParams,
1034910361
...opts,
1035010362
});
1035110363
}
@@ -10815,6 +10827,75 @@ describe("a router", () => {
1081510827
});
1081610828
});
1081710829

10830+
it("should send proper arguments to loaders", async () => {
10831+
let rootLoaderStub = jest.fn(() => "ROOT");
10832+
let childLoaderStub = jest.fn(() => "CHILD");
10833+
let { query } = createStaticHandler([
10834+
{
10835+
id: "root",
10836+
path: "/",
10837+
loader: rootLoaderStub,
10838+
children: [
10839+
{
10840+
id: "child",
10841+
path: "child",
10842+
loader: childLoaderStub,
10843+
},
10844+
],
10845+
},
10846+
]);
10847+
await query(createRequest("/child"));
10848+
10849+
// @ts-expect-error
10850+
let rootLoaderRequest = rootLoaderStub.mock.calls[0][0]?.request;
10851+
// @ts-expect-error
10852+
let childLoaderRequest = childLoaderStub.mock.calls[0][0]?.request;
10853+
expect(rootLoaderRequest.method).toBe("GET");
10854+
expect(rootLoaderRequest.url).toBe("http://localhost/child");
10855+
expect(childLoaderRequest.method).toBe("GET");
10856+
expect(childLoaderRequest.url).toBe("http://localhost/child");
10857+
});
10858+
10859+
it("should send proper arguments to actions", async () => {
10860+
let actionStub = jest.fn(() => "ACTION");
10861+
let rootLoaderStub = jest.fn(() => "ROOT");
10862+
let childLoaderStub = jest.fn(() => "CHILD");
10863+
let { query } = createStaticHandler([
10864+
{
10865+
id: "root",
10866+
path: "/",
10867+
loader: rootLoaderStub,
10868+
children: [
10869+
{
10870+
id: "child",
10871+
path: "child",
10872+
action: actionStub,
10873+
loader: childLoaderStub,
10874+
},
10875+
],
10876+
},
10877+
]);
10878+
await query(createSubmitRequest("/child"));
10879+
10880+
// @ts-expect-error
10881+
let actionRequest = actionStub.mock.calls[0][0]?.request;
10882+
expect(actionRequest.method).toBe("POST");
10883+
expect(actionRequest.url).toBe("http://localhost/child");
10884+
expect(actionRequest.headers.get("Content-Type")).toBe(
10885+
"application/x-www-form-urlencoded;charset=UTF-8"
10886+
);
10887+
expect((await actionRequest.formData()).get("key")).toBe("value");
10888+
10889+
// @ts-expect-error
10890+
let rootLoaderRequest = rootLoaderStub.mock.calls[0][0]?.request;
10891+
// @ts-expect-error
10892+
let childLoaderRequest = childLoaderStub.mock.calls[0][0]?.request;
10893+
expect(rootLoaderRequest.method).toBe("GET");
10894+
expect(rootLoaderRequest.url).toBe("http://localhost/child");
10895+
expect(childLoaderRequest.method).toBe("GET");
10896+
expect(childLoaderRequest.url).toBe("http://localhost/child");
10897+
});
10898+
1081810899
describe("statusCode", () => {
1081910900
it("should expose a 200 status code by default", async () => {
1082010901
let { query } = createStaticHandler([

packages/router/router.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,9 @@ export function createRouter(init: RouterInit): Router {
952952
...opts.submission,
953953
};
954954
loadingNavigation = navigation;
955+
956+
// Create a GET request for the loaders
957+
request = createRequest(request.url, request.signal);
955958
}
956959

957960
// Call loaders
@@ -2198,7 +2201,9 @@ export function unstable_createStaticHandler(
21982201
};
21992202
}
22002203

2201-
let context = await loadRouteData(request, matches);
2204+
// Create a GET request for the loaders
2205+
let loaderRequest = createRequest(request.url, request.signal);
2206+
let context = await loadRouteData(loaderRequest, matches);
22022207

22032208
return {
22042209
...context,

0 commit comments

Comments
 (0)