|
| 1 | +import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"; |
| 2 | +import { Window } from "happy-dom"; |
| 3 | +import type React from "react"; |
| 4 | + |
| 5 | +const createGlobeMock = mock(() => ({ |
| 6 | + destroy: mock(() => {}), |
| 7 | + update: mock(() => {}), |
| 8 | +})); |
| 9 | + |
| 10 | +mock.module("cobe", () => ({ |
| 11 | + default: createGlobeMock, |
| 12 | +})); |
| 13 | + |
| 14 | +mock.module("next-themes", () => ({ |
| 15 | + useTheme: () => ({ |
| 16 | + resolvedTheme: "light", |
| 17 | + }), |
| 18 | +})); |
| 19 | + |
| 20 | +type RootHandle = { |
| 21 | + render(node: React.ReactNode): void; |
| 22 | + unmount(): void; |
| 23 | +}; |
| 24 | + |
| 25 | +let activeRoot: RootHandle | null = null; |
| 26 | +let mountNode: HTMLElement | null = null; |
| 27 | +let windowInstance: Window | null = null; |
| 28 | +let globeSize = { height: 0, width: 0 }; |
| 29 | +const resizeObserverDisconnectMock = mock(() => {}); |
| 30 | +const resizeObserverObserveMock = mock((_element: Element) => {}); |
| 31 | +const installedGlobalKeys = [ |
| 32 | + "window", |
| 33 | + "self", |
| 34 | + "document", |
| 35 | + "navigator", |
| 36 | + "Document", |
| 37 | + "DocumentFragment", |
| 38 | + "Element", |
| 39 | + "Event", |
| 40 | + "EventTarget", |
| 41 | + "HTMLElement", |
| 42 | + "HTMLCanvasElement", |
| 43 | + "MutationObserver", |
| 44 | + "Node", |
| 45 | + "SVGElement", |
| 46 | + "Text", |
| 47 | + "getComputedStyle", |
| 48 | + "ResizeObserver", |
| 49 | + "requestAnimationFrame", |
| 50 | + "cancelAnimationFrame", |
| 51 | + "IS_REACT_ACT_ENVIRONMENT", |
| 52 | +] as const; |
| 53 | + |
| 54 | +class MockResizeObserver { |
| 55 | + static callback: (() => void) | null = null; |
| 56 | + |
| 57 | + disconnect = resizeObserverDisconnectMock; |
| 58 | + observe = resizeObserverObserveMock; |
| 59 | + |
| 60 | + constructor(callback: () => void) { |
| 61 | + MockResizeObserver.callback = callback; |
| 62 | + } |
| 63 | + |
| 64 | + static trigger() { |
| 65 | + MockResizeObserver.callback?.(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function setGlobalValue(key: string, value: unknown) { |
| 70 | + Object.defineProperty(globalThis, key, { |
| 71 | + configurable: true, |
| 72 | + value, |
| 73 | + writable: true, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +function installDomGlobals(window: Window) { |
| 78 | + setGlobalValue("window", window); |
| 79 | + setGlobalValue("self", window); |
| 80 | + setGlobalValue("document", window.document); |
| 81 | + setGlobalValue("navigator", window.navigator); |
| 82 | + setGlobalValue("Document", window.Document); |
| 83 | + setGlobalValue("DocumentFragment", window.DocumentFragment); |
| 84 | + setGlobalValue("Element", window.Element); |
| 85 | + setGlobalValue("Event", window.Event); |
| 86 | + setGlobalValue("EventTarget", window.EventTarget); |
| 87 | + setGlobalValue("HTMLElement", window.HTMLElement); |
| 88 | + setGlobalValue("HTMLCanvasElement", window.HTMLCanvasElement); |
| 89 | + setGlobalValue("MutationObserver", window.MutationObserver); |
| 90 | + setGlobalValue("Node", window.Node); |
| 91 | + setGlobalValue("SVGElement", window.SVGElement); |
| 92 | + setGlobalValue("Text", window.Text); |
| 93 | + setGlobalValue("getComputedStyle", window.getComputedStyle.bind(window)); |
| 94 | + setGlobalValue("ResizeObserver", MockResizeObserver); |
| 95 | + setGlobalValue( |
| 96 | + "requestAnimationFrame", |
| 97 | + mock(() => 1) |
| 98 | + ); |
| 99 | + setGlobalValue( |
| 100 | + "cancelAnimationFrame", |
| 101 | + mock(() => {}) |
| 102 | + ); |
| 103 | + setGlobalValue("IS_REACT_ACT_ENVIRONMENT", true); |
| 104 | + |
| 105 | + Object.defineProperty(window.HTMLElement.prototype, "clientHeight", { |
| 106 | + configurable: true, |
| 107 | + get() { |
| 108 | + return this.getAttribute("data-slot") === "globe-root" |
| 109 | + ? globeSize.height |
| 110 | + : 0; |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + Object.defineProperty(window.HTMLElement.prototype, "clientWidth", { |
| 115 | + configurable: true, |
| 116 | + get() { |
| 117 | + return this.getAttribute("data-slot") === "globe-root" |
| 118 | + ? globeSize.width |
| 119 | + : 0; |
| 120 | + }, |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +describe("Globe", () => { |
| 125 | + beforeEach(() => { |
| 126 | + activeRoot = null; |
| 127 | + mountNode = null; |
| 128 | + windowInstance = new Window({ |
| 129 | + url: "https://example.com", |
| 130 | + }); |
| 131 | + globeSize = { height: 0, width: 0 }; |
| 132 | + createGlobeMock.mockReset(); |
| 133 | + createGlobeMock.mockImplementation(() => ({ |
| 134 | + destroy: mock(() => {}), |
| 135 | + update: mock(() => {}), |
| 136 | + })); |
| 137 | + MockResizeObserver.callback = null; |
| 138 | + resizeObserverDisconnectMock.mockReset(); |
| 139 | + resizeObserverObserveMock.mockReset(); |
| 140 | + installDomGlobals(windowInstance); |
| 141 | + }); |
| 142 | + |
| 143 | + afterEach(async () => { |
| 144 | + const { act } = await import("react"); |
| 145 | + |
| 146 | + if (activeRoot) { |
| 147 | + await act(async () => { |
| 148 | + activeRoot?.unmount(); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + mountNode?.remove(); |
| 153 | + activeRoot = null; |
| 154 | + mountNode = null; |
| 155 | + windowInstance = null; |
| 156 | + |
| 157 | + for (const key of installedGlobalKeys) { |
| 158 | + Reflect.deleteProperty(globalThis, key); |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + it("retries globe creation after an initial zero-size mount", async () => { |
| 163 | + const { act } = await import("react"); |
| 164 | + const { createRoot } = await import("react-dom/client"); |
| 165 | + const { Globe } = await import("./index"); |
| 166 | + |
| 167 | + mountNode = document.createElement("div"); |
| 168 | + document.body.appendChild(mountNode); |
| 169 | + activeRoot = createRoot(mountNode); |
| 170 | + |
| 171 | + await act(async () => { |
| 172 | + activeRoot?.render(<Globe allowDrag={false} autoRotate={false} />); |
| 173 | + }); |
| 174 | + |
| 175 | + expect(createGlobeMock).not.toHaveBeenCalled(); |
| 176 | + expect(document.body.innerHTML).toContain('data-slot="globe-root"'); |
| 177 | + expect(resizeObserverObserveMock).toHaveBeenCalledTimes(1); |
| 178 | + |
| 179 | + globeSize = { height: 240, width: 320 }; |
| 180 | + |
| 181 | + await act(async () => { |
| 182 | + MockResizeObserver.trigger(); |
| 183 | + }); |
| 184 | + |
| 185 | + expect(createGlobeMock).toHaveBeenCalledTimes(1); |
| 186 | + const createGlobeCalls = createGlobeMock.mock.calls as unknown as [ |
| 187 | + unknown, |
| 188 | + { height: number; width: number }, |
| 189 | + ][]; |
| 190 | + |
| 191 | + expect(createGlobeCalls[0]?.[1]).toMatchObject({ |
| 192 | + height: 240, |
| 193 | + width: 320, |
| 194 | + }); |
| 195 | + |
| 196 | + await act(async () => { |
| 197 | + MockResizeObserver.trigger(); |
| 198 | + }); |
| 199 | + |
| 200 | + expect(createGlobeMock).toHaveBeenCalledTimes(1); |
| 201 | + }); |
| 202 | +}); |
0 commit comments