Uecko_ERP/apps/server/src/common/domain/value-objects/unique-id.spec.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-02-04 18:25:10 +00:00
import { UniqueID } from "./unique-id";
2025-01-30 10:45:31 +00:00
2025-02-04 18:25:10 +00:00
// 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);
2025-01-30 10:45:31 +00:00
2025-02-03 13:12:36 +00:00
expect(result.isSuccess).toBe(true);
2025-02-04 18:25:10 +00:00
expect(result.data?.isDefined()).toBe(true);
2025-01-30 10:45:31 +00:00
});
2025-02-04 18:25:10 +00:00
test("should fail to create UniqueID with an invalid UUID", () => {
2025-01-30 10:45:31 +00:00
const result = UniqueID.create("invalid-uuid");
2025-02-04 18:25:10 +00:00
expect(result.isFailure).toBe(true);
});
test("should create an undefined UniqueID when id is undefined and generateOnEmpty is false", () => {
const result = UniqueID.create(undefined, false);
2025-02-03 13:12:36 +00:00
expect(result.isSuccess).toBe(true);
2025-02-04 18:25:10 +00:00
expect(result.data?.isDefined()).toBe(false);
});
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?.isDefined()).toBe(true);
});
test("should fail when id is null", () => {
const result = UniqueID.create(null as any);
expect(result.isFailure).toBe(true);
2025-01-30 10:45:31 +00:00
});
2025-02-04 18:25:10 +00:00
test("should create a UniqueID when id is an empty string and generateOnEmpty is true", () => {
const result = UniqueID.create(" ", true);
2025-01-30 10:45:31 +00:00
2025-02-03 13:12:36 +00:00
expect(result.isSuccess).toBe(true);
2025-02-04 18:25:10 +00:00
expect(result.data?.isDefined()).toBe(true);
2025-01-30 10:45:31 +00:00
});
2025-02-04 18:25:10 +00:00
test("should create an undefined UniqueID when id is an empty string and generateOnEmpty is false", () => {
const result = UniqueID.create(" ", false);
2025-01-30 10:45:31 +00:00
2025-02-03 13:12:36 +00:00
expect(result.isSuccess).toBe(true);
2025-02-04 18:25:10 +00:00
expect(result.data?.isDefined()).toBe(false);
2025-01-30 10:45:31 +00:00
});
});