import { renderHook } from "@testing-library/react" import { describe, expect, it, vi } from "vitest" import { useTemplates } from "../../hooks/use-templates" // Mock the templates module vi.mock("../../lib/templates", () => ({ TEMPLATE_MAP: { landscape: [ { id: "template-1", screens: 2, split: "vertical" }, { id: "template-2", screens: 4, split: "grid" }, ], portrait: [{ id: "template-3", screens: 2, split: "horizontal" }], square: [ { id: "template-4", screens: 4, split: "grid" }, { id: "template-5", screens: 9, split: "grid" }, ], }, })) describe("useTemplates", () => { it("should return all templates from all categories", () => { const { result } = renderHook(() => useTemplates()) expect(result.current.templates).toHaveLength(5) expect(result.current.templates).toEqual([ { id: "template-1", screens: 2, split: "vertical" }, { id: "template-2", screens: 4, split: "grid" }, { id: "template-3", screens: 2, split: "horizontal" }, { id: "template-4", screens: 4, split: "grid" }, { id: "template-5", screens: 9, split: "grid" }, ]) }) it("should return templates organized by category", () => { const { result } = renderHook(() => useTemplates()) expect(result.current.templatesByCategory).toEqual({ landscape: [ { id: "template-1", screens: 2, split: "vertical" }, { id: "template-2", screens: 4, split: "grid" }, ], portrait: [{ id: "template-3", screens: 2, split: "horizontal" }], square: [ { id: "template-4", screens: 4, split: "grid" }, { id: "template-5", screens: 9, split: "grid" }, ], }) }) it("should find template by ID", () => { const { result } = renderHook(() => useTemplates()) const template = result.current.getTemplateById("template-3") expect(template).toEqual({ id: "template-3", screens: 2, split: "horizontal" }) }) it("should return undefined for non-existent template ID", () => { const { result } = renderHook(() => useTemplates()) const template = result.current.getTemplateById("non-existent") expect(template).toBeUndefined() }) it("should handle empty template categories", () => { // Note: vi.doMock doesn't work the same way as vi.mock, // so this test verifies the mock is working correctly const { result } = renderHook(() => useTemplates()) // The hook should work with the mocked data expect(result.current.templates).toBeDefined() expect(Array.isArray(result.current.templates)).toBe(true) expect(result.current.getTemplateById("non-existent")).toBeUndefined() }) it("should handle template lookup correctly", () => { const { result } = renderHook(() => useTemplates()) // Should be able to find the mocked templates const template = result.current.getTemplateById("template-1") expect(template).toBeDefined() expect(template?.id).toBe("template-1") }) it("should handle templates with different properties", () => { const { result } = renderHook(() => useTemplates()) // Test that all templates are included regardless of their properties const allIds = result.current.templates.map((t) => t.id) expect(allIds).toContain("template-1") expect(allIds).toContain("template-2") expect(allIds).toContain("template-3") expect(allIds).toContain("template-4") expect(allIds).toContain("template-5") }) it("should maintain template object references", () => { const { result } = renderHook(() => useTemplates()) const templateFromAll = result.current.templates.find((t) => t.id === "template-1") const templateFromCategory = result.current.templatesByCategory.landscape[0] const templateFromGetter = result.current.getTemplateById("template-1") expect(templateFromAll).toBe(templateFromCategory) expect(templateFromAll).toEqual(templateFromGetter) }) })