Skip to content

Commit 5f64128

Browse files
committed
add shared type for mock scenarios
1 parent 4c1d01b commit 5f64128

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/mocks/autoAPIMock.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { HttpResponseResolver, JsonBodyType } from "msw";
22
import { HttpResponse } from "msw";
3+
import type { MockScenarioName } from "./scenarioNames";
34

45
type ResponseResolverInfo = Parameters<HttpResponseResolver>[0];
56

@@ -13,8 +14,11 @@ export interface AutoAPIMockInstance<T> {
1314
generatedHandler: HttpResponseResolver;
1415
override: (fn: OverrideFn<T>) => AutoAPIMockInstance<T>;
1516
overrideHandler: (fn: OverrideHandlerFn<T>) => AutoAPIMockInstance<T>;
16-
scenario: (name: string, fn: ScenarioFn<T>) => AutoAPIMockInstance<T>;
17-
useScenario: (name: string) => AutoAPIMockInstance<T>;
17+
scenario: (
18+
name: MockScenarioName,
19+
fn: ScenarioFn<T>,
20+
) => AutoAPIMockInstance<T>;
21+
useScenario: (name: MockScenarioName) => AutoAPIMockInstance<T>;
1822
reset: () => AutoAPIMockInstance<T>;
1923
defaultValue: T;
2024
}
@@ -24,7 +28,7 @@ const registry: Set<AutoAPIMockInstance<unknown>> = new Set();
2428

2529
export function AutoAPIMock<T>(defaultValue: T): AutoAPIMockInstance<T> {
2630
let overrideHandlerFn: OverrideHandlerFn<T> | null = null;
27-
const scenarios = new Map<string, ScenarioFn<T>>();
31+
const scenarios = new Map<MockScenarioName, ScenarioFn<T>>();
2832

2933
const instance: AutoAPIMockInstance<T> = {
3034
defaultValue,
@@ -47,12 +51,12 @@ export function AutoAPIMock<T>(defaultValue: T): AutoAPIMockInstance<T> {
4751
return instance;
4852
},
4953

50-
scenario(name: string, fn: ScenarioFn<T>) {
54+
scenario(name: MockScenarioName, fn: ScenarioFn<T>) {
5155
scenarios.set(name, fn);
5256
return instance;
5357
},
5458

55-
useScenario(name: string) {
59+
useScenario(name: MockScenarioName) {
5660
const scenarioFn = scenarios.get(name);
5761
if (!scenarioFn) {
5862
throw new Error(

src/mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export type { AutoAPIMockInstance } from "./autoAPIMock";
22
export { AutoAPIMock, resetAllAutoAPIMocks } from "./autoAPIMock";
3+
export type { MockScenarioName } from "./scenarioNames";

src/mocks/scenarioNames.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Global scenario names for API mocks.
3+
*
4+
* Define scenario names here so they can be reused across different mocks
5+
* with consistent naming and documentation.
6+
*/
7+
8+
/** Empty state - API returns no data */
9+
export type EmptyServers = "empty-servers";
10+
11+
/** API returns 500 Internal Server Error */
12+
export type ServerError = "server-error";
13+
14+
/**
15+
* Union of all available mock scenario names.
16+
*
17+
* Add new scenario types above and include them in this union.
18+
*/
19+
export type MockScenarioName = EmptyServers | ServerError;

0 commit comments

Comments
 (0)