|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + bindEmbedAutoResize, |
| 4 | + createEmbedResizeMessage, |
| 5 | + isEmbedMeasureRequest, |
| 6 | + measureEmbedHeight, |
| 7 | +} from "@/lib/embed-resize"; |
| 8 | +import { MEMOS_EMBED_MEASURE_MESSAGE_TYPE } from "memos-embed"; |
| 9 | + |
| 10 | +const createDomRect = (height: number): DOMRect => |
| 11 | + ({ |
| 12 | + x: 0, |
| 13 | + y: 0, |
| 14 | + width: 0, |
| 15 | + height, |
| 16 | + top: 0, |
| 17 | + right: 0, |
| 18 | + bottom: height, |
| 19 | + left: 0, |
| 20 | + toJSON: () => ({}), |
| 21 | + } as DOMRect); |
| 22 | + |
| 23 | +describe("embed resize helpers", () => { |
| 24 | + it("measures the tallest available embed height", () => { |
| 25 | + const container = document.createElement("div"); |
| 26 | + vi.spyOn(container, "getBoundingClientRect").mockReturnValue(createDomRect(180)); |
| 27 | + Object.defineProperty(document.documentElement, "scrollHeight", { |
| 28 | + value: 240, |
| 29 | + configurable: true, |
| 30 | + }); |
| 31 | + Object.defineProperty(document.body, "scrollHeight", { |
| 32 | + value: 200, |
| 33 | + configurable: true, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(measureEmbedHeight(container, document)).toBe(240); |
| 37 | + }); |
| 38 | + |
| 39 | + it("recognizes valid measure requests and creates resize payloads", () => { |
| 40 | + expect( |
| 41 | + isEmbedMeasureRequest( |
| 42 | + { type: MEMOS_EMBED_MEASURE_MESSAGE_TYPE, frameId: "frame-1" }, |
| 43 | + "frame-1", |
| 44 | + ), |
| 45 | + ).toBe(true); |
| 46 | + expect( |
| 47 | + isEmbedMeasureRequest( |
| 48 | + { type: MEMOS_EMBED_MEASURE_MESSAGE_TYPE, frameId: "frame-2" }, |
| 49 | + "frame-1", |
| 50 | + ), |
| 51 | + ).toBe(false); |
| 52 | + expect(createEmbedResizeMessage("frame-1", 320)).toEqual({ |
| 53 | + type: "memos-embed:resize", |
| 54 | + frameId: "frame-1", |
| 55 | + height: 320, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("binds resize observers and responds to measurement requests", () => { |
| 60 | + const parentWindow = { postMessage: vi.fn() }; |
| 61 | + const container = document.createElement("div"); |
| 62 | + vi.spyOn(container, "getBoundingClientRect").mockReturnValue(createDomRect(190)); |
| 63 | + Object.defineProperty(document.documentElement, "scrollHeight", { |
| 64 | + value: 210, |
| 65 | + configurable: true, |
| 66 | + }); |
| 67 | + Object.defineProperty(document.body, "scrollHeight", { |
| 68 | + value: 160, |
| 69 | + configurable: true, |
| 70 | + }); |
| 71 | + |
| 72 | + const observe = vi.fn(); |
| 73 | + const disconnect = vi.fn(); |
| 74 | + let observerCallback: (() => void) | undefined; |
| 75 | + class ResizeObserverMock { |
| 76 | + constructor(callback: () => void) { |
| 77 | + observerCallback = callback; |
| 78 | + } |
| 79 | + observe = observe; |
| 80 | + disconnect = disconnect; |
| 81 | + } |
| 82 | + |
| 83 | + const rafSpy = vi |
| 84 | + .spyOn(window, "requestAnimationFrame") |
| 85 | + .mockImplementation((callback: FrameRequestCallback) => { |
| 86 | + callback(0); |
| 87 | + return 1; |
| 88 | + }); |
| 89 | + const cancelSpy = vi |
| 90 | + .spyOn(window, "cancelAnimationFrame") |
| 91 | + .mockImplementation(() => {}); |
| 92 | + |
| 93 | + const cleanup = bindEmbedAutoResize({ |
| 94 | + frameId: "frame-1", |
| 95 | + container, |
| 96 | + currentWindow: window, |
| 97 | + currentDocument: document, |
| 98 | + parentWindow: parentWindow as never, |
| 99 | + ResizeObserverCtor: ResizeObserverMock as never, |
| 100 | + }); |
| 101 | + |
| 102 | + expect(observe).toHaveBeenCalledWith(container); |
| 103 | + expect(parentWindow.postMessage).toHaveBeenCalledWith( |
| 104 | + { type: "memos-embed:resize", frameId: "frame-1", height: 210 }, |
| 105 | + "*", |
| 106 | + ); |
| 107 | + |
| 108 | + const callCount = parentWindow.postMessage.mock.calls.length; |
| 109 | + window.dispatchEvent( |
| 110 | + new MessageEvent("message", { |
| 111 | + data: { type: MEMOS_EMBED_MEASURE_MESSAGE_TYPE, frameId: "other" }, |
| 112 | + }), |
| 113 | + ); |
| 114 | + expect(parentWindow.postMessage).toHaveBeenCalledTimes(callCount); |
| 115 | + |
| 116 | + window.dispatchEvent( |
| 117 | + new MessageEvent("message", { |
| 118 | + data: { type: MEMOS_EMBED_MEASURE_MESSAGE_TYPE, frameId: "frame-1" }, |
| 119 | + }), |
| 120 | + ); |
| 121 | + expect(parentWindow.postMessage).toHaveBeenCalledTimes(callCount + 1); |
| 122 | + |
| 123 | + observerCallback?.(); |
| 124 | + expect(parentWindow.postMessage).toHaveBeenCalledTimes(callCount + 2); |
| 125 | + |
| 126 | + cleanup(); |
| 127 | + expect(disconnect).toHaveBeenCalled(); |
| 128 | + expect(cancelSpy).toHaveBeenCalledWith(1); |
| 129 | + |
| 130 | + rafSpy.mockRestore(); |
| 131 | + cancelSpy.mockRestore(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments