Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2-towns committed Sep 13, 2024
1 parent 85841b2 commit 72ca34f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 65 deletions.
85 changes: 22 additions & 63 deletions src/fetch-safe/fetch-safe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,18 @@ import { afterEach, assert, describe, it, vi } from "vitest";
import { Fetch } from "../fetch-safe/fetch-safe";
import type { CodexError } from "../async";

Check failure on line 3 in src/fetch-safe/fetch-safe.test.ts

View workflow job for this annotation

GitHub Actions / publish

'CodexError' is declared but its value is never read.

Check failure on line 3 in src/fetch-safe/fetch-safe.test.ts

View workflow job for this annotation

GitHub Actions / publish

'CodexError' is declared but its value is never read.

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.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"))
);
const mockResponse = {
ok: false,
status: 500,
text: async () => "error",
} as Response;
globalThis.fetch = vi.fn().mockResolvedValue(mockResponse);

const result = await Fetch.safeJson("http://localhost:3000/some-url", {
method: "GET",
Expand All @@ -69,31 +28,31 @@ describe.only("fetch", () => {
});

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")
)
);
const mockResponse = {
ok: true,
status: 200,
json: async () => {
return JSON.parse("{error");
},
} as any;
globalThis.fetch = vi.fn().mockResolvedValue(mockResponse);

const result = await Fetch.safeJson("http://localhost:3000/some-url", {
method: "GET",
});

assert.ok(result.error);
assert.deepStrictEqual(
(result.data as CodexError).message,
"Unexpected end of JSON input"
);
});

it("returns the data when the fetch succeed", async () => {
const spy = vi.spyOn(global, "fetch");
spy.mockImplementationOnce(() =>
Promise.resolve(
new MockResponse(true, 200, JSON.stringify({ hello: "world" }))
)
);
const mockResponse = {
ok: true,
status: 200,
json: async () => ({
hello: "world",
}),
} as Response;
globalThis.fetch = vi.fn().mockResolvedValue(mockResponse);

const result = await Fetch.safeJson("http://localhost:3000/some-url", {
method: "GET",
Expand Down
3 changes: 1 addition & 2 deletions src/fetch-safe/fetch-safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export const Fetch = {
return {
error: true,
data: {
message:
"The connection with the Codex node seems to be broken. Please check your node is running.",
message: res.data.message,
code: 502,
},
};
Expand Down

0 comments on commit 72ca34f

Please sign in to comment.