From b773cb3415c111bdacbe13a8929bb21dce3b4abe Mon Sep 17 00:00:00 2001 From: Takeshi Kimata <117462761+kimatata@users.noreply.github.com> Date: Thu, 16 May 2024 10:18:22 +0900 Subject: [PATCH] Intoduce vitest --- .../cases/[caseId]/CaseAttachmentsEditor.tsx | 21 ++--------- .../cases/[caseId]/attachmentControl.test.ts | 35 +++++++++++++++++++ .../[folderId]/cases/[caseId]/isImage.ts | 20 +++++++++++ 3 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachmentControl.test.ts create mode 100644 frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/isImage.ts diff --git a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseAttachmentsEditor.tsx b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseAttachmentsEditor.tsx index 9d3f4e9..e067a8d 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseAttachmentsEditor.tsx +++ b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseAttachmentsEditor.tsx @@ -1,6 +1,7 @@ import { Image, Button, Tooltip, Card, CardBody } from "@nextui-org/react"; import { AttachmentType, CaseMessages } from "@/types/case"; import { Trash, ArrowDownToLine } from "lucide-react"; +import { isImage } from "./isImage"; type Props = { attachments: AttachmentType[]; @@ -12,23 +13,6 @@ type Props = { messages: CaseMessages, }; -export function isImg(attachmentFile: AttachmentType) { - let path = attachmentFile.path; - let extension = path.substring(path.lastIndexOf(".") + 1).toLowerCase(); - if ( - extension === "png" || - extension === "jpg" || - extension === "jpeg" || - extension === "gif" || - extension === "bmp" || - extension === "svg" - ) { - return true; - } else { - return false; - } -} - export default function CaseAttachmentsEditor({ attachments = [], onAttachmentDownload, @@ -39,8 +23,7 @@ export default function CaseAttachmentsEditor({ let others = []; attachments.forEach((attachment) => { - const isImage = isImg(attachment) - if (isImage) { + if (isImage(attachment)) { images.push(attachment); } else { others.push(attachment); diff --git a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachmentControl.test.ts b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachmentControl.test.ts new file mode 100644 index 0000000..8251aba --- /dev/null +++ b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/attachmentControl.test.ts @@ -0,0 +1,35 @@ +import { expect, test } from "vitest"; +import { isImage } from "./isImage"; +import { AttachmentType } from "@/types/case"; + +test("isImage", () => { + type CaseAttachmentType = { + createdAt: Date; + updatedAt: Date; + CaseId: number; + AttachmentId: number; + }; + + const sampleCaseAttachment: CaseAttachmentType = { + createdAt: new Date(), + updatedAt: new Date(), + CaseId: 1, + AttachmentId: 1, + }; + + const sampleAttachment: AttachmentType = { + id: 1, + title: "", + detail: "", + path: "", + createdAt: new Date(), + updatedAt: new Date(), + caseAttachments: sampleCaseAttachment, + }; + + sampleAttachment.path = "public/uploads/abc.png"; + expect(isImage(sampleAttachment)).toBe(true); + + sampleAttachment.path = "public/uploads/abc.mp3"; + expect(isImage(sampleAttachment)).toBe(false); +}); diff --git a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/isImage.ts b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/isImage.ts new file mode 100644 index 0000000..5586059 --- /dev/null +++ b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/isImage.ts @@ -0,0 +1,20 @@ +import { AttachmentType } from "@/types/case"; + +function isImage(attachmentFile: AttachmentType) { + let path = attachmentFile.path; + let extension = path.substring(path.lastIndexOf(".") + 1).toLowerCase(); + if ( + extension === "png" || + extension === "jpg" || + extension === "jpeg" || + extension === "gif" || + extension === "bmp" || + extension === "svg" + ) { + return true; + } else { + return false; + } +} + +export { isImage };