Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions app/api/projects/[id]/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

type Route = typeof import("./route");

let route: Route;

beforeEach(async () => {
delete (globalThis as Record<string, unknown>).__devGarageDb;
vi.resetModules();
route = await import("./route");
});

const ctx = (id: string) => ({ params: Promise.resolve({ id }) });

const patch = (id: string, body: unknown) =>
route.PATCH(
new Request(`http://test/api/projects/${id}`, {
method: "PATCH",
body: JSON.stringify(body),
}),
ctx(id),
);

describe("GET /api/projects/[id]", () => {
it("200 para projeto existente", async () => {
const res = await route.GET(new Request("http://test"), ctx("habit-cli"));
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toMatchObject({ id: "habit-cli" });
expect(Array.isArray(body.tasks)).toBe(true);
});

it("404 para projeto inexistente", async () => {
const res = await route.GET(new Request("http://test"), ctx("nope"));
expect(res.status).toBe(404);
});
});

describe("PATCH /api/projects/[id]", () => {
it("400 para JSON inválido", async () => {
const res = await route.PATCH(
new Request("http://test", { method: "PATCH", body: "{bad" }),
ctx("habit-cli"),
);
expect(res.status).toBe(400);
});

it("400 quando a validação falha", async () => {
const res = await patch("habit-cli", { name: "" });
expect(res.status).toBe(400);
});

it("404 para projeto inexistente", async () => {
const res = await patch("nope", { status: "shipped" });
expect(res.status).toBe(404);
});

it("200 no sucesso", async () => {
const res = await patch("habit-cli", { status: "shipped" });
expect(res.status).toBe(200);
await expect(res.json()).resolves.toMatchObject({ status: "shipped" });
});
});

describe("DELETE /api/projects/[id]", () => {
it("204 no sucesso", async () => {
const res = await route.DELETE(new Request("http://test"), ctx("habit-cli"));
expect(res.status).toBe(204);
});

it("404 para projeto inexistente", async () => {
const res = await route.DELETE(new Request("http://test"), ctx("nope"));
expect(res.status).toBe(404);
});
});
52 changes: 52 additions & 0 deletions app/api/projects/[id]/tasks/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

type Route = typeof import("./route");

let route: Route;

beforeEach(async () => {
delete (globalThis as Record<string, unknown>).__devGarageDb;
vi.resetModules();
route = await import("./route");
});

const ctx = (id: string) => ({ params: Promise.resolve({ id }) });

const post = (id: string, body: unknown) =>
route.POST(
new Request(`http://test/api/projects/${id}/tasks`, {
method: "POST",
body: JSON.stringify(body),
}),
ctx(id),
);

describe("POST /api/projects/[id]/tasks", () => {
it("400 para JSON inválido", async () => {
const res = await route.POST(
new Request("http://test", { method: "POST", body: "{bad" }),
ctx("habit-cli"),
);
expect(res.status).toBe(400);
});

it("400 quando a validação falha", async () => {
const res = await post("habit-cli", { title: "" });
expect(res.status).toBe(400);
});

it("404 para projeto inexistente", async () => {
const res = await post("nope", { title: "Tarefa" });
expect(res.status).toBe(404);
});

it("201 com a task criada no sucesso", async () => {
const res = await post("habit-cli", { title: "Nova tarefa" });
expect(res.status).toBe(201);
await expect(res.json()).resolves.toMatchObject({
projectId: "habit-cli",
title: "Nova tarefa",
done: false,
});
});
});
54 changes: 54 additions & 0 deletions app/api/projects/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

type Route = typeof import("./route");

let route: Route;

beforeEach(async () => {
delete (globalThis as Record<string, unknown>).__devGarageDb;
vi.resetModules();
route = await import("./route");
});

const post = (body: unknown) =>
route.POST(
new Request("http://test/api/projects", {
method: "POST",
body: JSON.stringify(body),
}),
);

describe("GET /api/projects", () => {
it("retorna 200 com a lista de projetos", async () => {
const res = await route.GET();
expect(res.status).toBe(200);
const body = await res.json();
expect(Array.isArray(body)).toBe(true);
expect(body.length).toBeGreaterThan(0);
});
});

describe("POST /api/projects", () => {
it("400 para JSON inválido", async () => {
const res = await route.POST(
new Request("http://test/api/projects", {
method: "POST",
body: "{ not json",
}),
);
expect(res.status).toBe(400);
});

it("400 quando a validação falha", async () => {
const res = await post({ name: "" });
expect(res.status).toBe(400);
});

it("201 com o projeto criado no sucesso", async () => {
const res = await post({ name: "Meu projeto" });
expect(res.status).toBe(201);
const body = await res.json();
expect(body).toMatchObject({ name: "Meu projeto", status: "idea" });
expect(body.id).toBeTruthy();
});
});
66 changes: 66 additions & 0 deletions app/api/tasks/[id]/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

type Route = typeof import("./route");

let route: Route;

beforeEach(async () => {
delete (globalThis as Record<string, unknown>).__devGarageDb;
vi.resetModules();
route = await import("./route");
});

const ctx = (id: string) => ({ params: Promise.resolve({ id }) });

const patch = (id: string, body: unknown) =>
route.PATCH(
new Request(`http://test/api/tasks/${id}`, {
method: "PATCH",
body: JSON.stringify(body),
}),
ctx(id),
);

describe("PATCH /api/tasks/[id]", () => {
it("400 para JSON inválido", async () => {
const res = await route.PATCH(
new Request("http://test", { method: "PATCH", body: "{bad" }),
ctx("habit-cli-t1"),
);
expect(res.status).toBe(400);
});

it("400 quando a validação falha", async () => {
const res = await patch("habit-cli-t1", {});
expect(res.status).toBe(400);
});

it("404 para task inexistente", async () => {
const res = await patch("nope", { done: true });
expect(res.status).toBe(404);
});

it("200 no sucesso", async () => {
const res = await patch("habit-cli-t4", { done: true });
expect(res.status).toBe(200);
await expect(res.json()).resolves.toMatchObject({
id: "habit-cli-t4",
done: true,
});
});
});

describe("DELETE /api/tasks/[id]", () => {
it("204 no sucesso", async () => {
const res = await route.DELETE(
new Request("http://test"),
ctx("habit-cli-t1"),
);
expect(res.status).toBe(204);
});

it("404 para task inexistente", async () => {
const res = await route.DELETE(new Request("http://test"), ctx("nope"));
expect(res.status).toBe(404);
});
});
Loading