This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create base tests * start refining api_mock * update api_mock * lint * make the ApiMock as robust as it can be * update api_mock to mock xhr * polish api_mock * Get wrapper working. FIXME: button is not swapping wrapper modes
- Loading branch information
Showing
15 changed files
with
675 additions
and
183 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
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
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,112 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
|
||
import { afterAll, beforeAll, describe, expect, test } from "vitest"; | ||
import ApiMock from "./api_mock"; | ||
|
||
// Mock setup to simulate a browser XMLHttpRequest environment | ||
beforeAll(() => { | ||
const config = { delay: 0, mockData }; | ||
const apiMock = new ApiMock(config); | ||
globalThis.XMLHttpRequest = | ||
apiMock.createFakeXHR() as unknown as typeof XMLHttpRequest; | ||
}); | ||
|
||
afterAll(() => { | ||
// biome-ignore lint/performance/noDelete: <explanation> | ||
delete globalThis.XMLHttpRequest; | ||
}); | ||
|
||
// Define mock data with various response types and scenarios | ||
const mockData = { | ||
"GET /api/text": { | ||
ResponseBody: "Simple text response", | ||
ResponseStatus: 200, | ||
ResponseHeaders: { "Content-Type": "text/plain" }, | ||
ResponseType: "text", | ||
}, | ||
"POST /api/json": { | ||
ResponseBody: { message: "Data received" }, | ||
ResponseStatus: 201, | ||
ResponseHeaders: { "Content-Type": "application/json" }, | ||
ResponseType: "json", | ||
}, | ||
"PUT /api/blob": { | ||
ResponseBody: new Blob(["binary data"], { | ||
type: "application/octet-stream", | ||
}), | ||
ResponseStatus: 200, | ||
ResponseHeaders: { "Content-Type": "application/octet-stream" }, | ||
ResponseType: "blob", | ||
}, | ||
"DELETE /api/formData": { | ||
ResponseBody: new FormData(), | ||
ResponseStatus: 204, | ||
ResponseHeaders: { "Content-Type": "multipart/form-data" }, | ||
ResponseType: "formData", | ||
}, | ||
}; | ||
|
||
// Tests for different response types using XMLHttpRequest | ||
describe("ApiMock tests for different HTTP methods and response types", () => { | ||
test("should handle text response correctly", (done) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "/api/text"); | ||
xhr.onload = () => { | ||
expect(xhr.responseText).toBe("Simple text response"); | ||
expect(xhr.getResponseHeader("Content-Type")).toBe("text/plain"); | ||
done(); | ||
}; | ||
xhr.send(); | ||
}); | ||
|
||
test("should handle JSON response correctly", (done) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("POST", "/api/json"); | ||
xhr.onload = () => { | ||
expect(JSON.parse(xhr.responseText)).toEqual({ | ||
message: "Data received", | ||
}); | ||
expect(xhr.status).toBe(201); | ||
done(); | ||
}; | ||
xhr.send(); | ||
}); | ||
|
||
test("should handle Blob response correctly", (done) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.responseType = "blob"; | ||
xhr.open("PUT", "/api/blob"); | ||
xhr.onload = () => { | ||
expect(xhr.response.size).toBeGreaterThan(0); | ||
expect(xhr.getResponseHeader("Content-Type")).toBe( | ||
"application/octet-stream", | ||
); | ||
done(); | ||
}; | ||
xhr.send(); | ||
}); | ||
|
||
test("should handle FormData response correctly", (done) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("DELETE", "/api/formData"); | ||
xhr.onload = () => { | ||
expect(xhr.status).toBe(204); | ||
// FormData is not directly inspectable in the response, just check for correct handling | ||
done(); | ||
}; | ||
xhr.send(); | ||
}); | ||
|
||
test("should handle aborted requests", (done) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "/api/text"); | ||
xhr.onerror = () => { | ||
expect(xhr.statusText).toBe("AbortError"); | ||
done(); | ||
}; | ||
xhr.send(); | ||
xhr.abort(); | ||
}); | ||
}); |
Oops, something went wrong.