From 2f7b1cc07da472d92214943e6f67f785eb771ec9 Mon Sep 17 00:00:00 2001 From: Sugui Date: Thu, 28 Mar 2024 11:41:03 +0100 Subject: [PATCH] Implemented test --- test/ImageService.test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/ImageService.test.ts diff --git a/test/ImageService.test.ts b/test/ImageService.test.ts new file mode 100644 index 0000000..276cabf --- /dev/null +++ b/test/ImageService.test.ts @@ -0,0 +1,34 @@ +import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; +import Image from "../src/models/Image" +import ImageService from "../src/services/ImageService"; +import GelbooruResponse from "../src/models/GelbooruServiceResponse"; + +describe("endpoint gets a non repeated image", () => { + it("should return an image that is not in the response of the /images endpoint of the bot API", async () => { + const REPEATED_URL = "image.com/1"; + const UNIQUE_URL = "image.com/2"; + + mock.module("../services/GelbooruApiService", () => { + let alreadyCalled = false; + return { + default: { + get: (): GelbooruResponse[] => { + if (alreadyCalled) { + return [{ url: UNIQUE_URL, tags: [] }]; + } else { + alreadyCalled = true; + return [{ url: REPEATED_URL, tags: [] }]; + } + } + } + } + }); + + const botApiImagesResponse = { images: [ + {_id: "0", url: REPEATED_URL, status: "consumed", tags: ["pokemon", "computer"]} + ]}; + + const image: Image = await ImageService.get(); + expect(image.url).not.toBe(REPEATED_URL); + }) +}) \ No newline at end of file