-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scenario function add extanded
Co-authored-by: Minseok Choi <[email protected]>
- Loading branch information
Showing
7 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./http" | ||
export * from "./worker" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters