Skip to content

Add ResizeObserver support to useUnicornScene hook - #42

Merged
diegopeixoto merged 5 commits into
mainfrom
31-canvas-does-not-resize-when-container-size-changes
Mar 23, 2026
Merged

Add ResizeObserver support to useUnicornScene hook#42
diegopeixoto merged 5 commits into
mainfrom
31-canvas-does-not-resize-when-container-size-changes

Conversation

@diegopeixoto

Copy link
Copy Markdown
Owner

This pull request introduces comprehensive test coverage and implementation for handling container resizing in the useUnicornScene hook, ensuring that the scene properly responds to size changes. It includes a mock for ResizeObserver to enable reliable testing in the JSDOM environment, and adds several new tests to verify the resize behavior. Additionally, it refactors test code for improved readability and consistency.

ResizeObserver support and testing:

  • Added a minimal MockResizeObserver implementation in src/__tests__/setup.ts to mock browser ResizeObserver behavior, enabling simulation of resize events in tests. (src/__tests__/setup.ts)
  • Updated the useUnicornScene hook to observe container element resizes and call scene.resize() as needed, disconnecting the observer on unmount. (src/shared/hooks.ts)
  • Added tests to verify that resizing the container triggers scene.resize(), handles the absence of a resize method gracefully, disconnects the observer on unmount, and does not create observers for null refs. (src/__tests__/useUnicornScene.test.ts)

Test code refactoring and improvements:

  • Refactored multiple renderHook calls to use a consistent, more concise object form for initialProps and improved formatting for readability. (src/__tests__/useUnicornScene.test.ts) [1] [2] [3] [4] [5] [6]
  • Ensured proper cleanup of MockResizeObserver instances after each test to avoid cross-test contamination. (src/__tests__/useUnicornScene.test.ts)
  • Added import and usage of MockResizeObserver in the test suite. (src/__tests__/useUnicornScene.test.ts, src/__tests__/useUnicornScene.test.ts)
  • Minor formatting improvements in test assertions and callbacks for clarity and consistency. (src/__tests__/useUnicornScene.test.ts, src/__tests__/next-component.test.ts) [1] [2] [3] [4]

- Implemented a ResizeObserver in the useUnicornScene hook to adapt the canvas on container resize.
- Added tests to verify the behavior of scene resizing and handling undefined resize methods.
- Refactored test code for improved readability and consistency in next-component.test.tsx and useUnicornScene.test.ts.
- Updated .gitignore to exclude playground directory.
@diegopeixoto
diegopeixoto requested a review from Copilot March 23, 2026 22:43
@diegopeixoto diegopeixoto added the bug Something isn't working label Mar 23, 2026
@diegopeixoto diegopeixoto linked an issue Mar 23, 2026 that may be closed by this pull request
- Updated the test to ensure that simulating a resize does not throw an error when the resize method is undefined.
- Improved error handling in the test for better clarity and reliability.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds container resize handling to the useUnicornScene hook by wiring up a ResizeObserver, and introduces a JSDOM-friendly ResizeObserver mock plus new tests to verify resize behavior.

Changes:

  • Add ResizeObserver inside useUnicornScene to call scene.resize() when the container size changes.
  • Add a minimal MockResizeObserver test setup for JSDOM and new resize-related tests.
  • Minor refactors/formatting updates in existing tests and .gitignore.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/shared/hooks.ts Adds a ResizeObserver effect that calls scene.resize() on container resizes.
src/__tests__/setup.ts Introduces a MockResizeObserver and stubs it globally for the test environment.
src/__tests__/useUnicornScene.test.ts Adds resize behavior tests and refactors several renderHook usages for consistency.
src/__tests__/next-component.test.tsx Formatting-only changes to improve readability of JSX/assertions.
.gitignore Ignores playground/ directory.

馃挕 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/shared/hooks.ts
Comment thread src/__tests__/useUnicornScene.test.ts
diegopeixoto and others added 3 commits March 23, 2026 19:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Updated the useUnicornScene hook to check for the existence of ResizeObserver before proceeding with its functionality.
- Added a test case to ensure graceful degradation when ResizeObserver is unavailable, confirming no errors occur during unmounting.
- Improved test assertions to verify that no ResizeObserver instances are created when conditions are not met.
Repository owner deleted a comment from coderabbitai Bot Mar 23, 2026
@sonarqubecloud

Copy link
Copy Markdown

@diegopeixoto
diegopeixoto merged commit ad2d266 into main Mar 23, 2026
4 checks passed
@diegopeixoto
diegopeixoto deleted the 31-canvas-does-not-resize-when-container-size-changes branch March 23, 2026 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Canvas does not resize when container size changes

2 participants