Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/__tests__/useUnicornScene.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { StrictMode } from "react";
import { useUnicornScene } from "../shared/hooks";
import type { UnicornStudioScene } from "../shared/types";
import { MockResizeObserver } from "./setup";
Expand Down Expand Up @@ -85,6 +86,20 @@ describe("useUnicornScene", () => {
expect(result.current.error).toBeNull();
});

it("initializes once in Strict Mode", async () => {
const scene = createMockScene();
addSceneMock.mockResolvedValue(scene);

renderHook(() => useUnicornScene(defaultProps(elementRef)), {
wrapper: StrictMode,
});

await act(async () => {});

expect(addSceneMock).toHaveBeenCalledTimes(1);
expect(scene.destroy).not.toHaveBeenCalled();
});

it("does not initialize when isScriptLoaded is false", async () => {
const { result } = renderHook(() =>
useUnicornScene({ ...defaultProps(elementRef), isScriptLoaded: false }),
Expand Down Expand Up @@ -487,6 +502,8 @@ describe("useUnicornScene", () => {
},
});

await act(async () => {});

// Props change while addScene() is still in flight, so the scene config was
// already built with the previous values.
rerender({
Expand Down
4 changes: 4 additions & 0 deletions src/shared/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ export function useUnicornScene({
async function initializeScene() {
if (!elementRef.current || !isScriptLoaded || validationError) return;

// Let Strict Mode clean up its discarded effect before starting the SDK.
await Promise.resolve();
if (ignore) return;

// Prevent multiple concurrent initializations
if (isInitializingRef.current) return;

Expand Down