import "@testing-library/jest-dom";
import { act, render, screen } from "@testing-library/react";
import React from "react";
import { useCopyJobPrerequisitesCache } from "./useCopyJobPrerequisitesCache";
describe("useCopyJobPrerequisitesCache", () => {
let hookResult: any;
const TestComponent = ({ onHookUpdate }: { onHookUpdate?: () => void }): JSX.Element => {
hookResult = useCopyJobPrerequisitesCache();
React.useEffect(() => {
if (onHookUpdate) {
onHookUpdate();
}
}, [onHookUpdate]);
return (
{hookResult.validationCache.size}
);
};
afterEach(() => {
if (hookResult) {
act(() => {
hookResult.setValidationCache(new Map());
});
}
});
it("should initialize with an empty validation cache", () => {
render();
expect(hookResult.validationCache).toBeInstanceOf(Map);
expect(hookResult.validationCache.size).toBe(0);
expect(screen.getByTestId("cache-size")).toHaveTextContent("0");
});
it("should provide a setValidationCache function", () => {
render();
expect(typeof hookResult.setValidationCache).toBe("function");
});
it("should update validation cache when setValidationCache is called", () => {
render();
const testCache = new Map();
testCache.set("test-key", true);
testCache.set("another-key", false);
act(() => {
hookResult.setValidationCache(testCache);
});
expect(hookResult.validationCache).toBe(testCache);
expect(hookResult.validationCache.size).toBe(2);
expect(hookResult.validationCache.get("test-key")).toBe(true);
expect(hookResult.validationCache.get("another-key")).toBe(false);
expect(screen.getByTestId("cache-size")).toHaveTextContent("2");
});
it("should replace the entire validation cache when setValidationCache is called", () => {
render();
const initialCache = new Map();
initialCache.set("initial-key", true);
act(() => {
hookResult.setValidationCache(initialCache);
});
expect(hookResult.validationCache.get("initial-key")).toBe(true);
expect(screen.getByTestId("cache-size")).toHaveTextContent("1");
const newCache = new Map();
newCache.set("new-key", false);
act(() => {
hookResult.setValidationCache(newCache);
});
expect(hookResult.validationCache.get("initial-key")).toBeUndefined();
expect(hookResult.validationCache.get("new-key")).toBe(false);
expect(hookResult.validationCache.size).toBe(1);
expect(screen.getByTestId("cache-size")).toHaveTextContent("1");
});
it("should handle empty Map updates", () => {
render();
const initialCache = new Map();
initialCache.set("test-key", true);
act(() => {
hookResult.setValidationCache(initialCache);
});
expect(hookResult.validationCache.size).toBe(1);
expect(screen.getByTestId("cache-size")).toHaveTextContent("1");
act(() => {
screen.getByTestId("clear-cache-button").click();
});
expect(hookResult.validationCache.size).toBe(0);
expect(screen.getByTestId("cache-size")).toHaveTextContent("0");
});
it("should maintain state across multiple hook instances (global store behavior)", () => {
let firstHookResult: any;
let secondHookResult: any;
const FirstComponent = (): JSX.Element => {
firstHookResult = useCopyJobPrerequisitesCache();
return First
;
};
const SecondComponent = (): JSX.Element => {
secondHookResult = useCopyJobPrerequisitesCache();
return Second
;
};
render(
,
);
const testCache = new Map();
testCache.set("shared-key", true);
act(() => {
firstHookResult.setValidationCache(testCache);
});
expect(secondHookResult.validationCache.get("shared-key")).toBe(true);
expect(secondHookResult.validationCache.size).toBe(1);
expect(firstHookResult.validationCache.get("shared-key")).toBe(true);
expect(firstHookResult.validationCache.size).toBe(1);
});
it("should allow updates from different hook instances", () => {
let firstHookResult: any;
let secondHookResult: any;
const FirstComponent = (): JSX.Element => {
firstHookResult = useCopyJobPrerequisitesCache();
return (
);
};
const SecondComponent = (): JSX.Element => {
secondHookResult = useCopyJobPrerequisitesCache();
return (
);
};
render(
,
);
act(() => {
screen.getByTestId("first-update").click();
});
expect(secondHookResult.validationCache.get("key-from-first")).toBe(true);
act(() => {
screen.getByTestId("second-update").click();
});
expect(firstHookResult.validationCache.get("key-from-second")).toBe(false);
expect(firstHookResult.validationCache.get("key-from-first")).toBeUndefined();
});
it("should handle complex validation scenarios", () => {
const ComplexTestComponent = (): JSX.Element => {
hookResult = useCopyJobPrerequisitesCache();
const handleComplexUpdate = () => {
const complexCache = new Map();
complexCache.set("database-validation", true);
complexCache.set("container-validation", true);
complexCache.set("network-validation", false);
complexCache.set("authentication-validation", true);
complexCache.set("permission-validation", false);
hookResult.setValidationCache(complexCache);
};
return (
);
};
render();
act(() => {
screen.getByTestId("complex-update").click();
});
expect(hookResult.validationCache.size).toBe(5);
expect(hookResult.validationCache.get("database-validation")).toBe(true);
expect(hookResult.validationCache.get("container-validation")).toBe(true);
expect(hookResult.validationCache.get("network-validation")).toBe(false);
expect(hookResult.validationCache.get("authentication-validation")).toBe(true);
expect(hookResult.validationCache.get("permission-validation")).toBe(false);
});
it("should handle edge case keys", () => {
const EdgeCaseTestComponent = (): JSX.Element => {
hookResult = useCopyJobPrerequisitesCache();
const handleEdgeCaseUpdate = () => {
const edgeCaseCache = new Map();
edgeCaseCache.set("", true);
edgeCaseCache.set(" ", false);
edgeCaseCache.set("special-chars!@#$%^&*()", true);
edgeCaseCache.set("very-long-key-".repeat(10), false);
edgeCaseCache.set("unicode-key-🔑", true);
hookResult.setValidationCache(edgeCaseCache);
};
return (
);
};
render();
act(() => {
screen.getByTestId("edge-case-update").click();
});
expect(hookResult.validationCache.size).toBe(5);
expect(hookResult.validationCache.get("")).toBe(true);
expect(hookResult.validationCache.get(" ")).toBe(false);
expect(hookResult.validationCache.get("special-chars!@#$%^&*()")).toBe(true);
expect(hookResult.validationCache.get("very-long-key-".repeat(10))).toBe(false);
expect(hookResult.validationCache.get("unicode-key-🔑")).toBe(true);
});
it("should handle setting the same cache reference without errors", () => {
let testCache: Map;
const SameReferenceTestComponent = (): JSX.Element => {
hookResult = useCopyJobPrerequisitesCache();
const handleFirstUpdate = () => {
testCache = new Map();
testCache.set("test-key", true);
hookResult.setValidationCache(testCache);
};
const handleSecondUpdate = () => {
hookResult.setValidationCache(testCache);
};
return (
{hookResult.validationCache.get("test-key")?.toString()}
);
};
render();
act(() => {
screen.getByTestId("first-update").click();
});
expect(hookResult.validationCache.get("test-key")).toBe(true);
expect(screen.getByTestId("cache-content")).toHaveTextContent("true");
act(() => {
screen.getByTestId("second-update").click();
});
expect(hookResult.validationCache).toBe(testCache);
expect(hookResult.validationCache.get("test-key")).toBe(true);
expect(screen.getByTestId("cache-content")).toHaveTextContent("true");
});
});