Skip to content

Commit 6f30446

Browse files
committed
jsdoc for mock scenarios
1 parent 3924281 commit 6f30446

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

docs/mocks.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,19 @@ export const mockedGetRegistryV01Servers = AutoAPIMock<GetRegistryV01ServersResp
224224
Then use them in tests:
225225

226226
```typescript
227+
import { MockScenarios } from "@mocks";
227228
import { mockedGetRegistryV01Servers } from "@mocks/fixtures/registry_v0_1_servers/get";
228229

229230
describe("getServers", () => {
230231
it("returns empty array when API returns no servers", async () => {
231-
mockedGetRegistryV01Servers.useScenario("empty-servers");
232+
mockedGetRegistryV01Servers.useScenario(MockScenarios.EmptyServers);
232233

233234
const servers = await getServers();
234235
expect(servers).toEqual([]);
235236
});
236237

237238
it("throws on 500 server error", async () => {
238-
mockedGetRegistryV01Servers.useScenario("server-error");
239+
mockedGetRegistryV01Servers.useScenario(MockScenarios.ServerError);
239240

240241
await expect(getServers()).rejects.toBeDefined();
241242
});
@@ -247,14 +248,14 @@ describe("getServers", () => {
247248
Use `activateMockScenario` to activate a scenario across all registered mocks at once. This is useful for setting up a consistent state across multiple endpoints, with the option to further customize individual mocks afterwards.
248249

249250
```typescript
250-
import { activateMockScenario } from "@mocks";
251+
import { activateMockScenario, MockScenarios } from "@mocks";
251252
import { mockedGetRegistryV01Servers } from "@mocks/fixtures/registry_v0_1_servers/get";
252253

253254
describe("error handling", () => {
254255
it("shows error page when all APIs fail", async () => {
255256
// Activate "server-error" on all mocks that define it
256257
// Mocks without this scenario will use their default response
257-
activateMockScenario("server-error");
258+
activateMockScenario(MockScenarios.ServerError);
258259

259260
// Test that the app handles the error state correctly
260261
render(<App />);
@@ -263,7 +264,7 @@ describe("error handling", () => {
263264

264265
it("handles partial failures gracefully", async () => {
265266
// Start with all APIs returning errors
266-
activateMockScenario("server-error");
267+
activateMockScenario(MockScenarios.ServerError);
267268

268269
// Then customize specific endpoints to succeed
269270
mockedGetRegistryV01Servers.override((data) => data);
@@ -275,4 +276,4 @@ describe("error handling", () => {
275276
});
276277
```
277278

278-
Scenario names are defined in `src/mocks/scenarioNames.ts` and provide autocomplete and documentation across all mocks. Global scenarios are automatically reset before each test via `resetAllAutoAPIMocks()` in the test setup.
279+
Scenario names are defined in `src/mocks/scenarioNames.ts` via the `MockScenarios` object, which provides autocomplete and JSDoc documentation. Global scenarios are automatically reset before each test via `resetAllAutoAPIMocks()` in the test setup.

src/app/catalog/[repoName]/[serverName]/[version]/actions.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
2-
import { activateMockScenario } from "@/mocks";
32
import { getServerDetails } from "./actions";
43

54
// Mock the auth to bypass authentication
@@ -48,7 +47,6 @@ describe("getServerDetails", () => {
4847

4948
describe("error handling", () => {
5049
it("returns 404 for non-existent server", async () => {
51-
activateMockScenario("empty-servers");
5250
const result = await getServerDetails("non-existent/server", "1.0.0");
5351

5452
expect(result.response.status).toBe(404);

src/mocks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {
55
resetAllAutoAPIMocks,
66
} from "./autoAPIMock";
77
export type { MockScenarioName } from "./scenarioNames";
8+
export { MockScenarios } from "./scenarioNames";

src/mocks/scenarioNames.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/**
22
* Global scenario names for API mocks.
33
*
4-
* Define scenario names here so they can be reused across different mocks
5-
* with consistent naming and documentation.
4+
* Use `MockScenarios.X` for autocomplete with documentation,
5+
* or use the string literals directly.
66
*/
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";
7+
export const MockScenarios = {
8+
/** Empty state - API returns no data */
9+
EmptyServers: "empty-servers",
10+
/** API returns 500 Internal Server Error */
11+
ServerError: "server-error",
12+
} as const;
1313

1414
/**
1515
* Union of all available mock scenario names.
16-
*
17-
* Add new scenario types above and include them in this union.
1816
*/
19-
export type MockScenarioName = EmptyServers | ServerError;
17+
export type MockScenarioName =
18+
(typeof MockScenarios)[keyof typeof MockScenarios];

0 commit comments

Comments
 (0)