-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Mock Service Worker
to mock client API responses
All the jest configuration is from: https://mswjs.io/docs/migrations/1.x-to-2.x#frequent-issues
- Loading branch information
Showing
11 changed files
with
421 additions
and
142 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
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,7 @@ | ||
import { createOpenApiHttp } from "openapi-msw"; | ||
|
||
import { type GalaxyApiPaths } from "@/api"; | ||
|
||
const clientMock = createOpenApiHttp<GalaxyApiPaths>(); | ||
|
||
export { clientMock }; |
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,3 @@ | ||
import { setupServer } from "msw/node"; | ||
|
||
export const server = setupServer(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,94 @@ | ||
import { client, type HistoryDetailed, type HistorySummary, type MessageException } from "@/api"; | ||
import { clientMock } from "@/api/__mocks__"; | ||
|
||
const TEST_HISTORY_SUMMARY: HistorySummary = { | ||
model_class: "History", | ||
id: "test", | ||
name: "Test History", | ||
archived: false, | ||
deleted: false, | ||
purged: false, | ||
published: false, | ||
update_time: "2021-09-01T00:00:00", | ||
count: 0, | ||
annotation: "Test History Annotation", | ||
tags: [], | ||
url: "/api/histories/test", | ||
}; | ||
|
||
const TEST_HISTORY_DETAILED: HistoryDetailed = { | ||
...TEST_HISTORY_SUMMARY, | ||
create_time: "2021-09-01T00:00:00", | ||
contents_url: "/api/histories/test/contents", | ||
importable: false, | ||
slug: "testSlug", | ||
size: 0, | ||
user_id: "userID", | ||
username_and_slug: "username/slug", | ||
state: "ok", | ||
empty: true, | ||
hid_counter: 0, | ||
genome_build: null, | ||
state_ids: {}, | ||
state_details: {}, | ||
}; | ||
|
||
const EXPECTED_500_ERROR: MessageException = { err_code: 500, err_msg: "Internal Server Error" }; | ||
|
||
// Mock the API client | ||
// You can do whatever you want with the parameters and return values | ||
// All API schema types must be strictly respected and will be up to date with the OpenAPI schema | ||
clientMock.get("/api/histories/{history_id}", ({ params, query, response }) => { | ||
if (query.get("view") === "detailed") { | ||
return response(200).json(TEST_HISTORY_DETAILED); | ||
} | ||
if (params.history_id === "must-fail") { | ||
return response("5XX").json(EXPECTED_500_ERROR, { status: 500 }); | ||
} | ||
return response(200).json(TEST_HISTORY_SUMMARY); | ||
}); | ||
|
||
describe("clientMock", () => { | ||
it("mocks the API client", async () => { | ||
{ | ||
const { data, error } = await client.GET("/api/histories/{history_id}", { | ||
params: { | ||
path: { history_id: "test" }, | ||
query: { view: "summary" }, | ||
}, | ||
}); | ||
|
||
expect(error).toBeUndefined(); | ||
|
||
expect(data).toBeDefined(); | ||
expect(data).toEqual(TEST_HISTORY_SUMMARY); | ||
} | ||
|
||
{ | ||
const { data, error } = await client.GET("/api/histories/{history_id}", { | ||
params: { | ||
path: { history_id: "test" }, | ||
query: { view: "detailed" }, | ||
}, | ||
}); | ||
|
||
expect(error).toBeUndefined(); | ||
|
||
expect(data).toBeDefined(); | ||
expect(data).toEqual(TEST_HISTORY_DETAILED); | ||
} | ||
|
||
{ | ||
const { data, error } = await client.GET("/api/histories/{history_id}", { | ||
params: { | ||
path: { history_id: "must-fail" }, | ||
}, | ||
}); | ||
|
||
expect(error).toBeDefined(); | ||
expect(error).toEqual(EXPECTED_500_ERROR); | ||
|
||
expect(data).toBeUndefined(); | ||
} | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @note The block below contains polyfills for Node.js globals | ||
* required for Jest to function when running JSDOM tests. | ||
* These HAVE to be require's and HAVE to be in this exact | ||
* order, since "undici" depends on the "TextEncoder" global API. | ||
* | ||
* Consider migrating to a more modern test runner if | ||
* you don't want to deal with this. | ||
*/ | ||
|
||
// https://mswjs.io/docs/migrations/1.x-to-2.x#frequent-issues | ||
|
||
const { TextDecoder, TextEncoder } = require("node:util"); | ||
|
||
Object.defineProperties(globalThis, { | ||
TextDecoder: { value: TextDecoder }, | ||
TextEncoder: { value: TextEncoder }, | ||
}); | ||
|
||
const { Blob, File } = require("node:buffer"); | ||
const { fetch, Headers, FormData, Request, Response } = require("undici"); | ||
|
||
Object.defineProperties(globalThis, { | ||
fetch: { value: fetch, writable: true }, | ||
Blob: { value: Blob }, | ||
File: { value: File }, | ||
Headers: { value: Headers }, | ||
FormData: { value: FormData }, | ||
Request: { value: Request }, | ||
Response: { value: Response }, | ||
}); |
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
Oops, something went wrong.