mirror of
https://github.com/logos-storage/logos-storage-js.git
synced 2026-01-16 04:13:07 +00:00
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
|
|
import assert from "assert";
|
||
|
|
import { describe, it } from "node:test";
|
||
|
|
import { Fetch } from '../fetch-safe/fetch-safe';
|
||
|
|
|
||
|
|
class MockResponse implements Response {
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
describe("fetch", () => {
|
||
|
|
it("returns an error when the http call failed", async (t) => {
|
||
|
|
global.fetch = t.mock.fn(() =>
|
||
|
|
Promise.resolve(new MockResponse(false, 500, "error")),
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await Fetch.safe("http://localhost:3000/some-url", { method: "GET" })
|
||
|
|
const error = {
|
||
|
|
type: "api",
|
||
|
|
message: "error",
|
||
|
|
status: 500
|
||
|
|
}
|
||
|
|
|
||
|
|
assert.deepStrictEqual(result, { error: true, data: error });
|
||
|
|
});
|
||
|
|
|
||
|
|
it("returns an error when the json parsing failed", async (t) => {
|
||
|
|
global.fetch = t.mock.fn(() =>
|
||
|
|
Promise.resolve(new MockResponse(true, 200, "")),
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await Fetch.safe("http://localhost:3000/some-url", { method: "GET" })
|
||
|
|
const error = {
|
||
|
|
type: "error",
|
||
|
|
message: "Unexpected end of JSON input"
|
||
|
|
}
|
||
|
|
|
||
|
|
assert.deepStrictEqual(result, { error: true, data: error });
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
it("returns the data when the fetch succeed", async (t) => {
|
||
|
|
global.fetch = t.mock.fn(() =>
|
||
|
|
Promise.resolve(new MockResponse(true, 200, JSON.stringify({ hello: "world" }))),
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await Fetch.safe("http://localhost:3000/some-url", { method: "GET" })
|
||
|
|
|
||
|
|
assert.deepStrictEqual(result, { error: false, data: { hello: "world" } });
|
||
|
|
});
|
||
|
|
});
|