Skip to content

Commit

Permalink
test: use body instead of status for success flag
Browse files Browse the repository at this point in the history
  • Loading branch information
orionmiz committed Nov 27, 2023
1 parent b16f529 commit fbcbfb4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 98 deletions.
62 changes: 28 additions & 34 deletions packages/plantae/src/axios/createAxiosInterceptors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ describe("createAxiosInterceptors", () => {
http.post(
base("/"),
async ({ request }) =>
new Response(null, {
status:
(await request.text()) === "modified" ? Status.OK : Status.BAD,
})
new Response(
(await request.text()) === "modified" ? Status.OK : Status.BAD
)
)
);

Expand Down Expand Up @@ -44,20 +43,19 @@ describe("createAxiosInterceptors", () => {

const res = await axios.post("/");

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify request headers", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status:
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD,
})
new Response(
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD
)
)
);

Expand All @@ -84,20 +82,19 @@ describe("createAxiosInterceptors", () => {

const res = await axios.get("/");

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify existing request header", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status:
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD,
})
new Response(
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD
)
)
);

Expand Down Expand Up @@ -128,11 +125,11 @@ describe("createAxiosInterceptors", () => {
},
});

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify request method", async () => {
server.use(http.post(base("/"), () => new Response()));
server.use(http.post(base("/"), () => new Response(Status.OK)));

const axios = Axios.create({
baseURL,
Expand All @@ -158,11 +155,11 @@ describe("createAxiosInterceptors", () => {

const res = await axios.get("/");

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify request url", async () => {
server.use(http.get(base("/modified"), () => new Response()));
server.use(http.get(base("/modified"), () => new Response(Status.OK)));

const axios = Axios.create({
baseURL,
Expand All @@ -186,7 +183,7 @@ describe("createAxiosInterceptors", () => {

const res = await axios.get("/");

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can add request signal", async () => {
Expand Down Expand Up @@ -235,9 +232,7 @@ describe("createAxiosInterceptors", () => {
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status: request.credentials === "omit" ? Status.OK : Status.BAD,
})
new Response(request.credentials === "omit" ? Status.OK : Status.BAD)
)
);

Expand Down Expand Up @@ -267,20 +262,19 @@ describe("createAxiosInterceptors", () => {
withCredentials: true,
});

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify request cache", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status:
new URL(request.url).searchParams.get("_") !== null
? Status.OK
: Status.BAD,
})
new Response(
new URL(request.url).searchParams.get("_") !== null
? Status.OK
: Status.BAD
)
)
);

Expand Down Expand Up @@ -308,7 +302,7 @@ describe("createAxiosInterceptors", () => {

const res = await axios.get("/");

expect(res.status).toBe(Status.OK);
expect(res.data).toBe(Status.OK);
});

it("can modify response body", async () => {
Expand Down
49 changes: 18 additions & 31 deletions packages/plantae/src/fetch/createFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ describe("createAxiosInterceptors", () => {
http.post(
base("/"),
async ({ request }) =>
new Response(null, {
status:
(await request.text()) === "modified" ? Status.OK : Status.BAD,
})
new Response((await request.text()) === "modified" ? Status.OK : Status.BAD)
)
);

Expand All @@ -38,20 +35,17 @@ describe("createAxiosInterceptors", () => {
method: "POST",
});

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify request headers", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status:
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD,
})
new Response(request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD)
)
);

Expand All @@ -71,20 +65,17 @@ describe("createAxiosInterceptors", () => {

const res = await fetch(base("/"));

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify existing request header", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status:
request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD,
})
new Response(request.headers.get("x-custom-header") === "modified"
? Status.OK
: Status.BAD)
)
);

Expand All @@ -108,11 +99,11 @@ describe("createAxiosInterceptors", () => {
},
});

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify request method", async () => {
server.use(http.post(base("/"), () => new Response()));
server.use(http.post(base("/"), () => new Response(Status.OK)));

const fetch = createFetch({
plugins: [
Expand All @@ -131,11 +122,11 @@ describe("createAxiosInterceptors", () => {

const res = await fetch(base("/"));

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify request url", async () => {
server.use(http.get(base("/modified"), () => new Response()));
server.use(http.get(base("/modified"), () => new Response(Status.OK)));

const fetch = createFetch({
plugins: [
Expand All @@ -152,7 +143,7 @@ describe("createAxiosInterceptors", () => {

const res = await fetch(base("/"));

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can add request signal", async () => {
Expand Down Expand Up @@ -193,9 +184,7 @@ describe("createAxiosInterceptors", () => {
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status: request.credentials === "omit" ? Status.OK : Status.BAD,
})
new Response(request.credentials === "omit" ? Status.OK : Status.BAD)
)
);

Expand All @@ -218,17 +207,15 @@ describe("createAxiosInterceptors", () => {
credentials: "include",
});

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify request cache", async () => {
server.use(
http.get(
base("/"),
async ({ request }) =>
new Response(null, {
status: request.cache === "no-cache" ? Status.OK : Status.BAD,
})
new Response(request.cache === "no-cache" ? Status.OK : Status.BAD)
)
);

Expand All @@ -251,7 +238,7 @@ describe("createAxiosInterceptors", () => {
cache: "force-cache",
});

expect(res.status).toBe(Status.OK);
expect(await res.text()).toBe(Status.OK);
});

it("can modify response body", async () => {
Expand Down
Loading

0 comments on commit fbcbfb4

Please sign in to comment.