generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 45
test/use resize observer func target closure #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+37
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { render, waitFor } from '@testing-library/react'; | ||
| import { useEvent } from '@rc-component/util'; | ||
| import React from 'react'; | ||
| import { useResizeObserver } from '../src'; | ||
| import { _el as elementListeners } from '../src/utils/observerUtil'; | ||
|
|
||
| describe('useResizeObserver', () => { | ||
| it('should observe latest element when target getter closes over stateful ref value', async () => { | ||
| function Demo() { | ||
| const [element, setElement] = React.useState<HTMLElement | null>(null); | ||
| // `useEvent` keeps the getter identity stable while the closed-over DOM node | ||
| // comes from state. If the hook only checks the function reference, it misses | ||
| // the later state update from `null` to the actual element and never observes it. | ||
| const getTarget = useEvent(() => element as HTMLElement); | ||
|
|
||
| useResizeObserver(true, getTarget); | ||
|
|
||
| return <div ref={setElement} data-testid="target" />; | ||
| } | ||
|
|
||
| const { getByTestId } = render(<Demo />); | ||
| const target = getByTestId('target'); | ||
|
|
||
| await waitFor(() => { | ||
| // Once the ref callback stores the DOM into state, the latest element should | ||
| // still be observed even though the getter function itself never changes. | ||
| expect(elementListeners.get(target)).toBeTruthy(); | ||
| }); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: react-component/resize-observer
Length of output: 479
🏁 Script executed:
Repository: react-component/resize-observer
Length of output: 132
🏁 Script executed:
fd "useResizeObserver" --type fRepository: react-component/resize-observer
Length of output: 132
🏁 Script executed:
Repository: react-component/resize-observer
Length of output: 117
🏁 Script executed:
cat -n tests/useResizeObserver.spec.tsx | head -40Repository: react-component/resize-observer
Length of output: 1558
🏁 Script executed:
Repository: react-component/resize-observer
Length of output: 3508
🏁 Script executed:
Repository: react-component/resize-observer
Length of output: 2831
类型签名与运行时实现不匹配,导致需要不安全的类型断言。
测试中 getter 函数初始返回
null(当element状态为 null),后来返回真实元素。但当前签名只允许() => HTMLElement,强制测试使用as HTMLElement绕过类型检查。实现已在第 72 行明确处理了 null 目标(
if (target && enabled)),包括第 74-75 行对函数目标 null 情况的特殊处理。既然运行时支持这个场景,公开类型应同步允许HTMLElement | null和() => HTMLElement | null。建议的修改
🤖 Prompt for AI Agents