Uecko_ERP/packages/rdx-ddd/src/value-objects/__tests__/unique-id.test.ts
2025-05-09 12:45:32 +02:00

60 lines
1.8 KiB
TypeScript

import { UniqueID } from "../unique-id";
// Mock UUID generation to ensure predictable tests
jest.mock("uuid", () => ({ v4: () => "123e4567-e89b-12d3-a456-426614174000" }));
describe("UniqueID", () => {
test("should create a UniqueID with a valid UUID", () => {
const id = "123e4567-e89b-12d3-a456-426614174000";
const result = UniqueID.create(id);
expect(result.isSuccess).toBe(true);
expect(result.data.toString()).toBe(id);
});
test("should generate a UniqueID with a valid UUID", () => {
const result = UniqueID.generate();
expect(result.isSuccess).toBe(true);
expect(result.data.toString()).toBeTruthy();
});
test("should fail to create UniqueID with an invalid UUID", () => {
const result = UniqueID.create("invalid-uuid");
expect(result.isFailure).toBe(true);
});
test("should fail when id is undefined and generateOnEmpty is false", () => {
const result = UniqueID.create(undefined, false);
expect(result.isFailure).toBe(true);
});
test("should generate a new UUID when id is undefined and generateOnEmpty is true", () => {
const result = UniqueID.create(undefined, true);
expect(result.isSuccess).toBe(true);
expect(result.data?.toString()).toBeTruthy();
});
test("should fail when id is null", () => {
const result = UniqueID.create(null as any);
expect(result.isFailure).toBe(true);
});
test("should create a UniqueID when id is an empty string and generateOnEmpty is true", () => {
const result = UniqueID.create(" ", true);
expect(result.isSuccess).toBe(true);
expect(result.data?.toString()).toBeTruthy();
});
test("should fail when id is an empty string and generateOnEmpty is false", () => {
const result = UniqueID.create(" ", false);
expect(result.isFailure).toBe(true);
});
});