From 0ae9791e5bac126045d5e77e1809f2dd42db422d Mon Sep 17 00:00:00 2001 From: Eddi3_As Date: Sun, 30 Mar 2025 18:32:39 -0400 Subject: [PATCH] added missing types tests --- .../__tests__/Types/AdminTableType.test.tsx | 17 ++++ .../src/__tests__/Types/FolderType.test.tsx | 62 +++++++++++++++ client/src/__tests__/Types/ImageType.test.tsx | 79 +++++++++++++++++++ client/src/__tests__/Types/LabelMap.test.tsx | 35 ++++++++ client/src/__tests__/Types/UserType.test.tsx | 51 ++++++++++++ .../src/__tests__/pages/Admin/Images.test.tsx | 2 +- 6 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 client/src/__tests__/Types/AdminTableType.test.tsx create mode 100644 client/src/__tests__/Types/FolderType.test.tsx create mode 100644 client/src/__tests__/Types/ImageType.test.tsx create mode 100644 client/src/__tests__/Types/LabelMap.test.tsx create mode 100644 client/src/__tests__/Types/UserType.test.tsx diff --git a/client/src/__tests__/Types/AdminTableType.test.tsx b/client/src/__tests__/Types/AdminTableType.test.tsx new file mode 100644 index 0000000..2534f1b --- /dev/null +++ b/client/src/__tests__/Types/AdminTableType.test.tsx @@ -0,0 +1,17 @@ +import { AdminTableType } from "../../Types/AdminTableType"; + +it("AdminTableType allows valid data", () => { + const validData: AdminTableType = { + _id: "123", + email: "user@example.com", + created_at: new Date(), + updated_at: new Date(), + title: "Manager", + name: "John Doe", + roles: ["admin", "editor"], + }; + + expect(validData).toBeDefined(); + expect(validData._id).toBe("123"); + expect(validData.roles).toContain("admin"); +}); \ No newline at end of file diff --git a/client/src/__tests__/Types/FolderType.test.tsx b/client/src/__tests__/Types/FolderType.test.tsx new file mode 100644 index 0000000..b19a2d8 --- /dev/null +++ b/client/src/__tests__/Types/FolderType.test.tsx @@ -0,0 +1,62 @@ +import { FolderType } from "../../Types/FolderType"; + + +it('FolderType should allow correct structure with valid types', () => { + const validFolder: FolderType = { + _id: "1", + userId: "user123", + title: "My Folder", + created_at: "2025-03-30T22:08:47.839Z", + }; + expect(validFolder._id).toBe("1"); + expect(validFolder.userId).toBe("user123"); + expect(validFolder.title).toBe("My Folder"); + expect(validFolder.created_at).toBe("2025-03-30T22:08:47.839Z"); +}); + +it('FolderType should throw error if required fields are missing', () => { + const missingRequiredFields = (folder: any) => { + const requiredFields = ['_id', 'userId', 'title', 'created_at']; + for (const field of requiredFields) { + if (!folder[field]) { + throw new Error(`Missing required field: ${field}`); + } + } + }; + + // Test: Missing required field _id + expect(() => { + missingRequiredFields({ + userId: "user123", + title: "My Folder", + created_at: "2025-03-30T22:08:47.839Z", + }); + }).toThrow('Missing required field: _id'); + + // Test: Missing required field userId + expect(() => { + missingRequiredFields({ + _id: "1", + title: "My Folder", + created_at: "2025-03-30T22:08:47.839Z", + }); + }).toThrow('Missing required field: userId'); + + // Test: Missing required field title + expect(() => { + missingRequiredFields({ + _id: "1", + userId: "user123", + created_at: "2025-03-30T22:08:47.839Z", + }); + }).toThrow('Missing required field: title'); + + // Test: Missing required field created_at + expect(() => { + missingRequiredFields({ + _id: "1", + userId: "user123", + title: "My Folder", + }); + }).toThrow('Missing required field: created_at'); +}); \ No newline at end of file diff --git a/client/src/__tests__/Types/ImageType.test.tsx b/client/src/__tests__/Types/ImageType.test.tsx new file mode 100644 index 0000000..2d571c5 --- /dev/null +++ b/client/src/__tests__/Types/ImageType.test.tsx @@ -0,0 +1,79 @@ +import { ImageType, ImagesResponse, ImagesParams } from "../../Types/ImageType"; + +it("valid ImageType structure", () => { + const validImage: ImageType = { + id: "1", + file_content: "mockBase64Content", + file_name: "image.jpg", + mime_type: "image/jpeg", + }; + + expect(validImage).toHaveProperty("id", "1"); + expect(validImage).toHaveProperty("file_content", "mockBase64Content"); + expect(validImage).toHaveProperty("file_name", "image.jpg"); + expect(validImage).toHaveProperty("mime_type", "image/jpeg"); +}); + +it("invalid ImageType throws an error", () => { + const invalidImage: any = { + id: "1", + file_content: "mockBase64Content", + mime_type: "image/jpeg", + }; + + expect(() => { + expect(invalidImage).toHaveProperty("file_name"); + }).toThrow(); +}); + +it("valid ImagesResponse structure", () => { + const validResponse: ImagesResponse = { + images: [ + { + id: "1", + file_content: "mockBase64Content1", + file_name: "image1.jpg", + mime_type: "image/jpeg", + }, + { + id: "2", + file_content: "mockBase64Content2", + file_name: "image2.jpg", + mime_type: "image/jpeg", + }, + ], + total: 2, + }; + + expect(validResponse).toHaveProperty("images"); + expect(validResponse.images).toBeInstanceOf(Array); + expect(validResponse.images[0]).toHaveProperty("id"); + expect(validResponse.images[0]).toHaveProperty("file_content"); + expect(validResponse.images[0]).toHaveProperty("file_name"); + expect(validResponse.images[0]).toHaveProperty("mime_type"); + expect(validResponse).toHaveProperty("total", 2); +}); + +it("invalid ImagesResponse structure", () => { + const invalidResponse: any = { total: 2}; + expect(invalidResponse.images).toBeUndefined(); +}); + +it("valid ImagesParams structure", () => { + const validParams: ImagesParams = { + page: 1, + limit: 10, + uid: "user123", + }; + expect(validParams).toHaveProperty("page", 1); + expect(validParams).toHaveProperty("limit", 10); + expect(validParams).toHaveProperty("uid", "user123"); +}); + +it("invalid ImagesParams structure", () => { + const invalidParams: any = { page: 1}; + + expect(() => { + expect(invalidParams).toHaveProperty("limit"); + }).toThrow(); +}); \ No newline at end of file diff --git a/client/src/__tests__/Types/LabelMap.test.tsx b/client/src/__tests__/Types/LabelMap.test.tsx new file mode 100644 index 0000000..2dec463 --- /dev/null +++ b/client/src/__tests__/Types/LabelMap.test.tsx @@ -0,0 +1,35 @@ +import { LabelMap } from "../../Types/LabelMap"; + +it("LabelMap should only allow string keys and string values", () => { + // Valid LabelMap example with different keys + const validLabelMap: LabelMap = { + name: "Name", + email: "Email", + created_at: "Created At", + }; + + expect(validLabelMap).toBeDefined(); + expect(Object.keys(validLabelMap)).toEqual(["name", "email", "created_at"]); + expect(validLabelMap.name).toBe("Name"); + expect(validLabelMap.email).toBe("Email"); + expect(validLabelMap.created_at).toBe("Created At"); +}); + +it("LabelMap should allow only specified keys", () => { + const validLabelMap: LabelMap = { + name: "Name", + email: "Email", + created_at: "Created At", + }; + + const knownKeys = ["name", "email", "created_at"]; + const keys = Object.keys(validLabelMap); + knownKeys.forEach((key) => { + expect(keys).toContain(key); + expect(typeof validLabelMap[key]).toBe("string"); + }); + + Object.values(validLabelMap).forEach((value) => { + expect(typeof value).toBe("string"); + }); +}); \ No newline at end of file diff --git a/client/src/__tests__/Types/UserType.test.tsx b/client/src/__tests__/Types/UserType.test.tsx new file mode 100644 index 0000000..9fdaeec --- /dev/null +++ b/client/src/__tests__/Types/UserType.test.tsx @@ -0,0 +1,51 @@ +import { UserType, UsersResponse } from "../../Types/UserType"; + +it("valid UserType structure", () => { + const validUser: UserType = { + id: "1", + name: "John Doe", + email: "john.doe@example.com", + created_at: new Date().toISOString(), + roles: ["admin", "user"], + }; + expect(validUser).toHaveProperty("id", "1"); + expect(validUser).toHaveProperty("name", "John Doe"); + expect(validUser).toHaveProperty("email", "john.doe@example.com"); + expect(validUser).toHaveProperty("created_at"); + expect(validUser).toHaveProperty("roles"); + expect(validUser.roles).toBeInstanceOf(Array); + expect(validUser.roles).toContain("admin"); + expect(validUser.roles).toContain("user"); +}); + +it("valid UsersResponse structure", () => { + const validResponse: UsersResponse = { + users: [ + { + id: "1", + name: "John Doe", + email: "john.doe@example.com", + created_at: new Date().toISOString(), + roles: ["admin"], + }, + { + id: "2", + name: "Jane Smith", + email: "jane.smith@example.com", + created_at: new Date().toISOString(), + roles: ["user"], + }, + ], + }; + expect(validResponse).toHaveProperty("users"); + expect(validResponse.users).toBeInstanceOf(Array); + expect(validResponse.users[0]).toHaveProperty("id"); + expect(validResponse.users[0]).toHaveProperty("name"); + expect(validResponse.users[0]).toHaveProperty("email"); + expect(validResponse.users[0]).toHaveProperty("roles"); +}); + +it("invalid UsersResponse structure", () => { + const invalidResponse: any = { }; + expect(invalidResponse.users).toBeUndefined(); +}); diff --git a/client/src/__tests__/pages/Admin/Images.test.tsx b/client/src/__tests__/pages/Admin/Images.test.tsx index 3e44ff5..4416c0f 100644 --- a/client/src/__tests__/pages/Admin/Images.test.tsx +++ b/client/src/__tests__/pages/Admin/Images.test.tsx @@ -1 +1 @@ -//a;ready being tested by ImageGallery.test.tsx \ No newline at end of file +//TESTS ON ImageGallery.test.tsx \ No newline at end of file