Add ResizeObserver support to useUnicornScene hook - #42
Merged
diegopeixoto merged 5 commits intoMar 23, 2026
Conversation
- 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.
- 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.
Contributor
There was a problem hiding this comment.
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
ResizeObserverinsideuseUnicornSceneto callscene.resize()when the container size changes. - Add a minimal
MockResizeObservertest 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.
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.
|
diegopeixoto
deleted the
31-canvas-does-not-resize-when-container-size-changes
branch
March 23, 2026 22:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This pull request introduces comprehensive test coverage and implementation for handling container resizing in the
useUnicornScenehook, ensuring that the scene properly responds to size changes. It includes a mock forResizeObserverto 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:
MockResizeObserverimplementation insrc/__tests__/setup.tsto mock browserResizeObserverbehavior, enabling simulation of resize events in tests. (src/__tests__/setup.ts)useUnicornScenehook to observe container element resizes and callscene.resize()as needed, disconnecting the observer on unmount. (src/shared/hooks.ts)scene.resize(), handles the absence of aresizemethod gracefully, disconnects the observer on unmount, and does not create observers for null refs. (src/__tests__/useUnicornScene.test.ts)Test code refactoring and improvements:
renderHookcalls to use a consistent, more concise object form forinitialPropsand improved formatting for readability. (src/__tests__/useUnicornScene.test.ts) [1] [2] [3] [4] [5] [6]MockResizeObserverinstances after each test to avoid cross-test contamination. (src/__tests__/useUnicornScene.test.ts)MockResizeObserverin the test suite. (src/__tests__/useUnicornScene.test.ts,src/__tests__/useUnicornScene.test.ts)src/__tests__/useUnicornScene.test.ts,src/__tests__/next-component.test.ts) [1] [2] [3] [4]