Skip to content

Commit

Permalink
feat: scenario function add extanded
Browse files Browse the repository at this point in the history
Co-authored-by: Minseok Choi <[email protected]>
  • Loading branch information
minsgy and minsgy committed Jun 16, 2024
1 parent 77edbc3 commit 569f9af
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 10 deletions.
36 changes: 35 additions & 1 deletion example/src/mocks/browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
// src/mocks/browser.js
import { setupWorker } from "msw/browser"
import { http, setupWorker } from "msw-devtools"
import { handlers } from "./handlers"
import { HttpResponse } from "msw"

export const worker = setupWorker(...handlers)

worker.scenario([
{
scenarioName: "test",
handlers: [
http.get("https://jsonplaceholder.typicode.com/todos/1", () => {
return HttpResponse.json({
firstName: "111",
lastName: "111",
})
}),
http.get("https://jsonplaceholder.typicode.com/todos/2", () => {
return HttpResponse.json({
firstName: "222",
lastName: "222",
})
}),
],
isEnabled: true,
},
{
scenarioName: "test2",
handlers: [
http.get("https://jsonplaceholder.typicode.com/todos/3", () => {
return HttpResponse.json({
firstName: "333",
lastName: "333",
})
}),
],
isEnabled: false,
},
])
1 change: 0 additions & 1 deletion src/hooks/useMswDevtoolsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const useMswDevtoolsState = ({
if (worker) {
const httpUsedRoutes = generatorRequestHandler(routes)
worker.resetHandlers(...httpUsedRoutes)
// first call is not needed
if (isMounted.current) {
onRouteUpdate?.(routes)
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/providers/useMswDevtoolsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import constate from "constate"
import { useMswDevtoolsState } from "../hooks/useMswDevtoolsState"
import { useEditorPanelState } from "@/hooks/useEditorPanelState"
import { MSWDevtoolsProps } from ".."
import { MSWDevtoolsProps, SetupWorker } from ".."

export type MswDevtoolsContextType = Omit<
MSWDevtoolsProps,
"children" | "position"
>
"children" | "position" | "worker"
> & {
worker: ReturnType<SetupWorker>
}

const [
MswDevToolsProvider,
Expand Down
10 changes: 7 additions & 3 deletions src/shared/lib/http.ts → src/shared/lib/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ const createProxyMethod = <
url,
responseHandlersWithPresets,
])

result.presets = (presets: HttpPreset[]) =>
Reflect.apply(target, thisArg, [
url,
responseHandlersWithPresets,
presets,
])

return result
},
}) as HttpRequestHandler<
Expand All @@ -59,7 +61,7 @@ type HttpPreset = {
response: any
}

type HttpRequestHandler<
export type HttpRequestHandler<
Params extends PathParams<keyof Params> = PathParams,
RequestBodyType extends DefaultBodyType = DefaultBodyType,
ResponseBodyType extends DefaultBodyType = undefined,
Expand All @@ -71,11 +73,13 @@ type HttpRequestHandler<
presets: (presets: HttpPreset[]) => HttpHandler
}

// 4. custom http object creation
const http = originHttp as {
export type Http = {
[method in keyof typeof originHttp]: HttpRequestHandler
}

// 4. custom http object creation
const http = originHttp as Http

// 5. method assignment
HTTP_METHODS.forEach((method: keyof typeof originHttp) => {
http[method] = createProxyMethod(method)
Expand Down
1 change: 1 addition & 0 deletions src/shared/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./http"
export * from "./worker"
46 changes: 46 additions & 0 deletions src/shared/lib/worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
setupWorker as originSetupWorker,
type SetupWorker as OriginSetupWorker,
} from "msw/browser"
import { RequestHandler } from "msw"
import type { Http } from "../http"

export type ScenarioRoutePreset = {
scenarioName: string
handlers: Array<ReturnType<Http[keyof Http]>>
isEnabled?: boolean
}

export type SetupWorker = (
...handlers: Array<RequestHandler>
) => OriginSetupWorker & {
/**
* Add a scenario to the worker
*
*/
scenario: (scenarioRoutePreset: ScenarioRoutePreset[]) => void
listScenarios: () => ScenarioRoutePreset[]
}

const setupWorker = new Proxy(originSetupWorker, {
apply: (target, thisArg, argArray) => {
const originWorker = Reflect.apply(target, thisArg, argArray)
const scenarios: ScenarioRoutePreset[] = []
originWorker.scenario = (scenario: ScenarioRoutePreset[]) => {
scenarios.push(...scenario)
return scenario
}
originWorker.listScenarios = () => {
return scenarios
}
return originWorker
},
}) as SetupWorker

export const createScenarioPreset = (
scenarioRoutePreset: ScenarioRoutePreset[]
) => {
return scenarioRoutePreset
}

export { setupWorker }
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SetupWorker } from "msw/lib/browser"
import { HttpMethods } from "msw"
import { SetupWorker } from "./shared/lib/worker"

export interface MSWDevtoolsProps {
/**
Expand All @@ -12,7 +12,7 @@ export interface MSWDevtoolsProps {
/**
* An instance of the MSW worker returned from `setupWorker`.
*/
worker: SetupWorker
worker?: ReturnType<SetupWorker>
/**
* This component takes children so that it can ensure the worker has started before rendering the tree. This guarantees that
* all requests will be intercepted.
Expand Down

0 comments on commit 569f9af

Please sign in to comment.