Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Nov 18, 2024
1 parent f9d6396 commit 278e9b8
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import { expect, test, beforeEach, afterEach } from "@jest/globals";
import { StatusCodes } from "http-status-codes";
import { fetch as undiciFetch, RequestInit } from "undici";
import { RequestInit } from "undici";
import { fetch } from "./fetch";
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from "undici";
import { MockAgent, setGlobalDispatcher } from "undici";

let mockAgent: MockAgent;
interface Todo {
Expand All @@ -21,24 +21,30 @@ beforeEach(() => {

const mockClient = mockAgent.get("https://jsonplaceholder.typicode.com");

mockClient.intercept({ path: "/todos/1", method: "GET" }).reply(StatusCodes.OK, {
userId: 1,
id: 1,
title: "Example title",
completed: false,
}, {
headers: {
"Content-Type": "application/json; charset=utf-8",
mockClient.intercept({ path: "/todos/1", method: "GET" }).reply(
StatusCodes.OK,
{
userId: 1,
id: 1,
title: "Example title",
completed: false,
},
{
headers: {
"Content-Type": "application/json; charset=utf-8",
},
},
});
);

mockClient
.intercept({ path: "/todos", method: "POST" })
.reply(StatusCodes.OK, { title: "test todo", userId: 1, completed: false }, {
mockClient.intercept({ path: "/todos", method: "POST" }).reply(
StatusCodes.OK,
{ title: "test todo", userId: 1, completed: false },
{
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
},
);

mockClient
.intercept({ path: "/todos/empty-null", method: "GET" })
Expand All @@ -58,12 +64,11 @@ afterEach(() => {
});

test("fetch: should return without type data", async () => {

const url = "https://jsonplaceholder.typicode.com/todos/1";
const requestOptions: RequestInit = {
method: "GET",
headers: {
"hi":"there",
hi: "there",
"content-type": "application/json; charset=UTF-8",
},
};
Expand Down Expand Up @@ -143,11 +148,15 @@ test("fetch: should handle failed requests without throwing an error", async ()
test("fetch wrapper respects MockAgent", async () => {
const mockClient = mockAgent.get("https://example.com");

mockClient.intercept({ path: "/test", method: "GET" }).reply(200, { success: true }, {
headers: {
"Content-Type": "application/json; charset=utf-8",
mockClient.intercept({ path: "/test", method: "GET" }).reply(
200,
{ success: true },
{
headers: {
"Content-Type": "application/json; charset=utf-8",
},
},
});
);

const response = await fetch<{ success: boolean }>("https://example.com/test");

Expand All @@ -161,8 +170,7 @@ test("fetch wrapper respects MockAgent", async () => {
// for (const [key, value] of response.headers) {
// console.log(`${key}: ${value}`);
// }
// return response.json()
// return response.json()
// })
// .then(data => console.log(data.title))
// }

0 comments on commit 278e9b8

Please sign in to comment.