From 532de04592d1106c11626c143efc5cc83422bb1f Mon Sep 17 00:00:00 2001 From: Alie Date: Mon, 25 Dec 2023 12:33:40 +0100 Subject: [PATCH] added tests for POST, fixed tests for GET --- tests/app.test.ts | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tests/app.test.ts b/tests/app.test.ts index feb519f..a375b81 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,22 +1,51 @@ -import { describe, expect, it } from "bun:test"; +import { beforeAll, describe, expect, it } from "bun:test"; import request from "supertest"; import { app } from "../src"; -describe("GET /images works properly", () => { +describe("GET /images works properly", async () => { + const res = await request(app).get("/images"); + it("should be an array", async () => { - const res = await request(app).get("/images"); expect(Array.isArray(res.body)).toBeTrue(); + }); + + it("should return a 200", async () => { expect(res.statusCode).toBe(200); }); }); describe("POST /images works properly", () => { - it("should return 201 or 409", async () => { + it("should return 201 for new image", async () => { const res = await request(app).post("/images").send({ - url: "https://test.url.com/123", + url: "https://test.url.com/1", status: "available", tags: ["2girls", "touhou"] }); - expect(res.status).toSatisfy(status => [201, 409].includes(status)); + expect(res.status).toSatisfy(status => [201].includes(status)); + }); + + it("should return 409 for a repeated images", async () => { + await request(app).post("/images").send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"] + }); + + const res = await request(app).post("/images").send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"] + }); + + expect(res.status).toSatisfy(status => [409].includes(status)); + }); + + it("should return 400 for malformed requests", async () => { + const res = await request(app).post("/images").send({ + url: "https://test.url.com/3", + status: "wrong", + tags: ["2girls", "touhou"] + }); + expect(res.status).toSatisfy(status => [400].includes(status)); }); }); \ No newline at end of file