logos-storage-js/src/fetch-safe/fetch-safe.test.ts

105 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-09-13 19:45:59 +02:00
import { afterEach, assert, describe, it, vi } from "vitest";
2024-08-15 12:08:45 +02:00
import { Fetch } from "../fetch-safe/fetch-safe";
2024-09-13 19:45:59 +02:00
import type { CodexError } from "../async";
2024-08-15 12:08:15 +02:00
class MockResponse implements Response {
2024-08-15 12:08:45 +02:00
headers: Headers = new Headers();
ok: boolean;
redirected = false;
status: number;
statusText = "";
type = "basic" as "basic";
url = "";
body = null;
bodyUsed = false;
_text: string;
constructor(ok: boolean, status: number, text: string) {
this.ok = ok;
this.status = status;
this._text = text;
}
clone(): Response {
throw new Error("Method not implemented.");
}
arrayBuffer(): Promise<ArrayBuffer> {
throw new Error("Method not implemented.");
}
blob(): Promise<Blob> {
throw new Error("Method not implemented.");
}
formData(): Promise<FormData> {
throw new Error("Method not implemented.");
}
json(): Promise<any> {
return Promise.resolve(JSON.parse(this._text));
}
text(): Promise<string> {
return Promise.resolve(this._text);
}
2024-08-15 12:08:15 +02:00
}
2024-08-15 12:08:51 +02:00
describe.only("fetch", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("returns an error when the http call failed", async () => {
const spy = vi.spyOn(global, "fetch");
spy.mockImplementationOnce(() =>
Promise.resolve(new MockResponse(false, 500, "error"))
2024-08-15 12:08:45 +02:00
);
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:51 +02:00
const result = await Fetch.safeJson("http://localhost:3000/some-url", {
2024-08-15 12:08:45 +02:00
method: "GET",
2024-08-15 12:08:15 +02:00
});
2024-08-15 12:08:51 +02:00
2024-08-15 12:08:45 +02:00
const error = {
message: "error",
2024-08-15 12:08:51 +02:00
code: 500,
2024-08-15 12:08:45 +02:00
};
assert.deepStrictEqual(result, { error: true, data: error });
});
it.only("returns an error when the json parsing failed", async () => {
const spy = vi.spyOn(global, "fetch");
spy.mockImplementationOnce(() =>
Promise.resolve(
new MockResponse(false, 200, "Unexpected end of JSON input")
)
2024-08-15 12:08:45 +02:00
);
2024-08-15 12:08:51 +02:00
const result = await Fetch.safeJson("http://localhost:3000/some-url", {
2024-08-15 12:08:45 +02:00
method: "GET",
2024-08-15 12:08:15 +02:00
});
2024-08-15 12:08:45 +02:00
2024-08-15 12:08:51 +02:00
assert.ok(result.error);
2024-09-13 19:45:59 +02:00
assert.deepStrictEqual(
(result.data as CodexError).message,
"Unexpected end of JSON input"
);
2024-08-15 12:08:45 +02:00
});
it("returns the data when the fetch succeed", async () => {
const spy = vi.spyOn(global, "fetch");
spy.mockImplementationOnce(() =>
2024-08-15 12:08:45 +02:00
Promise.resolve(
new MockResponse(true, 200, JSON.stringify({ hello: "world" }))
)
2024-08-15 12:08:45 +02:00
);
2024-08-15 12:08:51 +02:00
const result = await Fetch.safeJson("http://localhost:3000/some-url", {
2024-08-15 12:08:45 +02:00
method: "GET",
});
assert.deepStrictEqual(result, { error: false, data: { hello: "world" } });
});
2024-08-15 12:08:15 +02:00
});