Intoduce vitest

This commit is contained in:
Takeshi Kimata
2024-05-16 10:18:22 +09:00
parent ce73084f07
commit b773cb3415
3 changed files with 57 additions and 19 deletions

View File

@@ -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);

View File

@@ -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);
});

View File

@@ -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 };