Skip to content

Commit

Permalink
Merge pull request #29 from iputapp/test/api/notify-newcomer
Browse files Browse the repository at this point in the history
test(api-notify-newcomer): ライブラリ依存を改善
  • Loading branch information
wiyco authored Dec 16, 2024
2 parents 2e5f416 + 99afb1e commit c38ba15
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions test/api/notify/newcomer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { testApiHandler } from "next-test-api-route-handler";
import { NextRequest } from "next/server";
import { describe, expect, it } from "vitest";

import * as appHandler from "@/app/api/v1/notify/newcomer/route";
import { POST } from "@/app/api/v1/notify/newcomer/route";
import type { Newcomer } from "@/models";
import { createAPIResponse } from "@/server";

describe("POST /api/v1/notify/newcomer", () => {
const REQUEST_URL = "http://localhost:3000/api/v1/notify/newcomer";

it("正常なリクエストを処理できる", async () => {
const validBody: Newcomer = {
name: "foobar",
Expand All @@ -15,20 +17,15 @@ describe("POST /api/v1/notify/newcomer", () => {
csrfToken: "csrf-token",
};

await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({
method: "POST",
body: JSON.stringify(validBody),
});

expect(response.status).toBe(200);
expect(await response.json()).toMatchObject(
createAPIResponse(validBody)
);
},
const request = new NextRequest(REQUEST_URL, {
method: "POST",
body: JSON.stringify(validBody),
});

const response = await POST(request);

expect(response.status).toBe(200);
expect(await response.json()).toMatchObject(createAPIResponse(validBody));
});

it("不正なメールアドレスの場合エラーを返す", async () => {
Expand All @@ -40,19 +37,16 @@ describe("POST /api/v1/notify/newcomer", () => {
csrfToken: "csrf-token",
};

await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({
method: "POST",
body: JSON.stringify(invalidBody),
});

expect(response.status).toBe(400);
const json = await response.json();
expect(json.error).toBeDefined();
},
const request = new NextRequest(REQUEST_URL, {
method: "POST",
body: JSON.stringify(invalidBody),
});

const response = await POST(request);

expect(response.status).toBe(400);
const json = await response.json();
expect(json.error).toBeDefined();
});

it("学籍番号が必要な場合にエラーを返す", async () => {
Expand All @@ -64,18 +58,15 @@ describe("POST /api/v1/notify/newcomer", () => {
csrfToken: "csrf-token",
};

await testApiHandler({
appHandler,
test: async ({ fetch }) => {
const response = await fetch({
method: "POST",
body: JSON.stringify(invalidBody),
});

expect(response.status).toBe(400);
const json = await response.json();
expect(json.error).toBeDefined();
},
const request = new NextRequest(REQUEST_URL, {
method: "POST",
body: JSON.stringify(invalidBody),
});

const response = await POST(request);

expect(response.status).toBe(400);
const json = await response.json();
expect(json.error).toBeDefined();
});
});

0 comments on commit c38ba15

Please # to comment.