|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { createElement, type FunctionComponent } from "react"; |
| 3 | +import { renderToString } from "react-dom/server"; |
| 4 | +import { useRenderData } from "../../../src/ui/hooks/useRenderData.js"; |
| 5 | + |
| 6 | +type UseRenderDataResult<T> = ReturnType<typeof useRenderData<T>>; |
| 7 | + |
| 8 | +interface TestData { |
| 9 | + items: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Simple hook testing utility that renders a component using the hook |
| 14 | + * and captures the result for assertions. |
| 15 | + */ |
| 16 | +function testHook<T = unknown>(): UseRenderDataResult<T> { |
| 17 | + let hookResult: UseRenderDataResult<T> | undefined; |
| 18 | + |
| 19 | + const TestComponent: FunctionComponent = () => { |
| 20 | + hookResult = useRenderData<T>(); |
| 21 | + return null; |
| 22 | + }; |
| 23 | + |
| 24 | + renderToString(createElement(TestComponent)); |
| 25 | + |
| 26 | + if (!hookResult) { |
| 27 | + throw new Error("Hook did not return a result"); |
| 28 | + } |
| 29 | + |
| 30 | + return hookResult; |
| 31 | +} |
| 32 | + |
| 33 | +describe("useRenderData", () => { |
| 34 | + let postMessageMock: ReturnType<typeof vi.fn>; |
| 35 | + let originalWindow: typeof globalThis.window; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + originalWindow = globalThis.window; |
| 39 | + postMessageMock = vi.fn(); |
| 40 | + |
| 41 | + globalThis.window = { |
| 42 | + parent: { |
| 43 | + postMessage: postMessageMock, |
| 44 | + }, |
| 45 | + addEventListener: vi.fn(), |
| 46 | + removeEventListener: vi.fn(), |
| 47 | + } as unknown as typeof globalThis.window; |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + globalThis.window = originalWindow; |
| 52 | + vi.restoreAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns initial state with isLoading true", () => { |
| 56 | + const result = testHook<TestData>(); |
| 57 | + |
| 58 | + expect(result.data).toBeNull(); |
| 59 | + expect(result.isLoading).toBe(true); |
| 60 | + expect(result.error).toBeNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("returns parentOrigin as null initially", () => { |
| 64 | + const result = testHook<TestData>(); |
| 65 | + |
| 66 | + expect(result.parentOrigin).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("includes parentOrigin in return type", () => { |
| 70 | + const result = testHook<TestData>(); |
| 71 | + |
| 72 | + // Verify the hook returns the expected shape with parentOrigin |
| 73 | + expect(result).toHaveProperty("data"); |
| 74 | + expect(result).toHaveProperty("isLoading"); |
| 75 | + expect(result).toHaveProperty("error"); |
| 76 | + expect(result).toHaveProperty("parentOrigin"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns a stable object shape for destructuring", () => { |
| 80 | + const { data, isLoading, error, parentOrigin } = testHook<TestData>(); |
| 81 | + |
| 82 | + expect(data).toBeNull(); |
| 83 | + expect(isLoading).toBe(true); |
| 84 | + expect(error).toBeNull(); |
| 85 | + expect(parentOrigin).toBeNull(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments