added missing types tests

This commit is contained in:
Eddi3_As 2025-03-30 18:32:39 -04:00
parent 3f9d53eb5e
commit 0ae9791e5b
6 changed files with 245 additions and 1 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1 +1 @@
//a;ready being tested by ImageGallery.test.tsx
//TESTS ON ImageGallery.test.tsx