Skip to content

Commit 819640c

Browse files
authored
Allow HTTP method to be specified for mockEndpointDynamic (#2234)
1 parent f40a353 commit 819640c

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/Frontend/test/driver.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { type PathParams } from "msw";
1+
import { DefaultBodyType, StrictRequest, type PathParams } from "msw";
22

33
type GoTo = (path: string) => Promise<void>;
44
type DisposeApp = () => void;
5+
6+
export type Method = "get" | "post" | "patch" | "put" | "delete" | "options";
7+
58
export type MockEndpointOptions = {
69
// eslint-disable-next-line @typescript-eslint/no-explicit-any
710
body: Record<string, any> | string | number | boolean | null | undefined;
8-
method?: "get" | "post" | "patch" | "put" | "delete" | "options";
11+
method?: Method;
912
status?: number;
1013
headers?: { [key: string]: string };
1114
};
@@ -18,7 +21,7 @@ export type MockEndpointDynamicOptions = {
1821
};
1922

2023
type MockEndpoint = (path: string, options: MockEndpointOptions) => void;
21-
type MockEndpointDynamic = (endpoint: string, callBack: (url: URL, params: PathParams) => MockEndpointDynamicOptions) => void;
24+
type MockEndpointDynamic = (endpoint: string, method: Method, callBack: (url: URL, params: PathParams, request: StrictRequest<DefaultBodyType>) => Promise<MockEndpointDynamicOptions>) => void;
2225

2326
export type SetupFactoryOptions = {
2427
driver: Driver;

src/Frontend/test/mock-endpoint.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { http, HttpResponse, type PathParams } from "msw";
1+
import { DefaultBodyType, http, HttpResponse, StrictRequest, type PathParams } from "msw";
22
import type { SetupWorker } from "msw/browser";
33
import { SetupServer } from "msw/node";
4-
import { MockEndpointDynamicOptions, MockEndpointOptions } from "./driver";
4+
import { MockEndpointDynamicOptions, MockEndpointOptions, Method } from "./driver";
55

66
export const makeMockEndpoint =
77
({ mockServer }: { mockServer: SetupServer | SetupWorker }) =>
@@ -11,11 +11,11 @@ export const makeMockEndpoint =
1111

1212
export const makeMockEndpointDynamic =
1313
({ mockServer }: { mockServer: SetupServer | SetupWorker }) =>
14-
(endpoint: string, callBack: (url: URL, params: PathParams) => MockEndpointDynamicOptions) => {
14+
(endpoint: string, method: Method = "get", callBack: (url: URL, params: PathParams, request: StrictRequest<DefaultBodyType>) => Promise<MockEndpointDynamicOptions>) => {
1515
mockServer.use(
16-
http.get(endpoint, ({ request, params }) => {
16+
http[method](endpoint, async ({ request, params }) => {
1717
const url = new URL(request.url.toString());
18-
const { body, status = 200, headers = {} } = callBack(url, params);
18+
const { body, status = 200, headers = {} } = await callBack(url, params, request);
1919
return HttpResponse.json(body, { status: status, headers: headers });
2020
})
2121
);

src/Frontend/test/preconditions/recoverability.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,20 @@ export const hasFailedMessage =
156156
edit_of: "",
157157
};
158158

159-
driver.mockEndpointDynamic(`${serviceControlUrl}errors`, (url) => {
159+
driver.mockEndpointDynamic(`${serviceControlUrl}errors`, "get", (url) => {
160160
const status = url.searchParams.get("status");
161161
if (status === "unresolved") {
162-
return {
162+
return Promise.resolve({
163163
body: [failedMessage],
164164
headers: { "Total-Count": "1" },
165-
};
165+
});
166166
}
167167

168168
//For status=archived or status=retryissued
169-
return {
169+
return Promise.resolve({
170170
body: [],
171171
headers: { "Total-Count": "0" },
172-
};
172+
});
173173
});
174174

175175
driver.mockEndpoint(`${serviceControlUrl}messages/${withMessageId}/body`, {

0 commit comments

Comments
 (0)