Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 202
refactor: use props check instead of version check#593
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* eslint-disable no-eval */ | ||
| import React from 'react'; | ||
| import { getNodeRef } from '../src/ref'; | ||
| jest.mock('react', () => { | ||
| const react19 = jest.requireActual('react-19'); | ||
| return react19; | ||
| }); | ||
| jest.mock('react-dom', () => { | ||
| const reactDom19 = jest.requireActual('react-dom-19'); | ||
| return reactDom19; | ||
| }); | ||
| describe('ref: React 19', () => { | ||
| const errSpy = jest.spyOn(console, 'error'); | ||
| beforeEach(() => { | ||
| errSpy.mockReset(); | ||
| }); | ||
| it('getNodeRef', () => { | ||
| const ref = React.createRef<HTMLDivElement>(); | ||
| const node = <div ref={ref} />; | ||
| expect(getNodeRef(node)).toBe(ref); | ||
| expect(errSpy).not.toHaveBeenCalled(); | ||
| }); | ||
Comment on lines
+22
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议增加更多测试用例 当前测试只覆盖了基本场景,建议添加以下测试用例:
示例测试用例: it('should handle null/undefined',()=>{expect(getNodeRef(null)).toBe(null);expect(getNodeRef(undefined)).toBe(null);});it('should handle non-React elements',()=>{expect(getNodeRef('string')).toBe(null);expect(getNodeRef(123)).toBe(null);});it('should work with function ref',()=>{constfnRef=jest.fn();constnode=<divref={fnRef}/>;expect(getNodeRef(node)).toBe(fnRef);}); | ||
| }); | ||
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.
建议优化 ref 获取逻辑的实现
当前实现存在以下问题:
propertyIsEnumerable方法可能不安全null分支缺少测试覆盖建议进行如下优化:
export const getNodeRef: <T = any>( node: React.ReactNode, ) => React.Ref<T> | null = node => { if (node && isReactElement(node)) { const ele = node as any; - return ele.props.propertyIsEnumerable('ref') ? ele.props.ref : ele.ref;+ return Object.prototype.propertyIsEnumerable.call(ele.props, 'ref') + ? ele.props.ref + : ele.ref; } return null; };📝 Committable suggestion
🧰 Tools
🪛 Biome (1.9.4)
[error] 90-90: Do not access Object.prototype method 'propertyIsEnumerable' from target object.
(lint/suspicious/noPrototypeBuiltins)
🪛 GitHub Check: codecov/patch
[warning] 92-92: src/ref.ts#L92
Added line #L92 was not covered by tests