diff --git a/packages/core/src/core/Viewpoints/src/viewpoint.ts b/packages/core/src/core/Viewpoints/src/viewpoint.ts index ddfd4aa83..d4f2689f0 100644 --- a/packages/core/src/core/Viewpoints/src/viewpoint.ts +++ b/packages/core/src/core/Viewpoints/src/viewpoint.ts @@ -43,7 +43,7 @@ export class Viewpoint { camera_up_vector: { x: 0, y: 1, z: 0 }, }; - customData: Record = {} + customData: Record = {}; /** * A list of components GUIDs to hide when defaultVisibility = true or to show when defaultVisibility = false @@ -346,6 +346,7 @@ export class Viewpoint { * @param transition - Indicates whether the camera movement should have a transition effect. * Default value is `true`. * + * @throws An error if the viewpoint has no world. * @throws An error if the world's camera does not have camera controls. * * @returns A Promise that resolves when the camera has been set. @@ -356,7 +357,11 @@ export class Viewpoint { clippingsVisibility?: boolean; applyVisibility?: boolean; }) { - if (!this.world) return; + if (!this.world) { + throw new Error( + "Viewpoint: no world to apply the viewpoint to. Set `world` on this viewpoint, or set `Viewpoints.world` before creating or loading viewpoints (for example with `BCFTopics.load`).", + ); + } const { camera } = this.world; if (!(camera instanceof OrthoPerspectiveCamera)) { diff --git a/packages/core/src/core/Viewpoints/viewpoint-go.test.ts b/packages/core/src/core/Viewpoints/viewpoint-go.test.ts new file mode 100644 index 000000000..d412d2d42 --- /dev/null +++ b/packages/core/src/core/Viewpoints/viewpoint-go.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from "vitest"; +import { loadSourceBundle } from "../../../tests/source-bundle"; + +// Needs the real `Components`: see `loadSourceBundle`. +const OBC = await loadSourceBundle(); + +describe("Viewpoint.go", () => { + it("rejects with a clear error when the viewpoint has no world", async () => { + const components = new OBC.Components(); + const viewpoints = components.get(OBC.Viewpoints); + expect(viewpoints.world).toBeNull(); + const viewpoint = viewpoints.create(); + await expect(viewpoint.go()).rejects.toThrow(/no world/); + }); +});