Skip to content

Avoid rc package deep imports#61

Open
QDyanbing wants to merge 2 commits into
react-component:masterfrom
QDyanbing:avoid-deep-imports
Open

Avoid rc package deep imports#61
QDyanbing wants to merge 2 commits into
react-component:masterfrom
QDyanbing:avoid-deep-imports

Conversation

@QDyanbing
Copy link
Copy Markdown
Contributor

@QDyanbing QDyanbing commented May 25, 2026

背景

为配合 rc 包统一避免引用其他 rc 包的 es / lib 构建产物内部路径,本次将 overflow 中相关引用调整为从包根入口导入公开 API。

调整内容

  • 升级 @rc-component/father-plugin@rc-component/resize-observer@rc-component/util
  • 将 util、resize-observer 等深路径引用改为根入口引用
  • 迁移测试到 Testing Library,并同步调整 resize mock 触发方式
  • 更新相关测试配置

验证

  • npm run lint 通过,存在既有 warning,无 error
  • npm test -- --runInBand 通过,SSR 用例存在 React useLayoutEffect console warning

Summary by CodeRabbit

发布说明

  • Chores

    • 升级 React 支持至 18.0.0
    • 升级 @rc-component/util 至 1.11.1
    • 移除旧版测试依赖(Enzyme 及相关序列化器)
    • 更新开发工具链版本
  • Tests

    • 迁移测试套件至现代工具链(@testing-library/react 等)
    • 重构测试辅助与全局测试环境,改进 SSR 与响应式测试时序

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

@QDyanbing is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 63cf4c6f-35b1-41a2-8ab5-9353a3f2b891

📥 Commits

Reviewing files that changed from the base of the PR and between 103b729 and 08bf717.

📒 Files selected for processing (3)
  • src/Overflow.tsx
  • tests/github.spec.tsx
  • tests/wrapper.ts
✅ Files skipped from review due to trivial changes (1)
  • tests/github.spec.tsx

Walkthrough

该PR统一@rc-component/util的导入为包级命名导入、升级依赖(含 React 18),并把测试框架从 Enzyme 完整迁移到 @testing-library/react:重写 tests/setup.js 与 tests/wrapper.ts、更新 Jest 配置、将大量测试异步化并调整断言与导入。

Changes

导入路径统一与依赖升级

Layer / File(s) Summary
依赖版本更新
package.json
@rc-component/util 升级到 ^1.11.1,React/React-DOM 升级到 ^18,移除 Enzyme 系列依赖,更新 @testing-library/react 与相关类型。
源码导入路径统一为包级命名导入
examples/fill-width.tsx, src/Overflow.tsx, src/hooks/channelUpdate.ts, src/hooks/useEffectState.tsx
useLayoutEffectrafuseEvent 等从深层默认导入改为从 @rc-component/util 的命名导入。
源码类型断言与回退格式化
src/Overflow.tsx
调整 getKey 的索引断言为 keyof any,将 mergedRenderItem 在无 renderItem 时显式返回 React.ReactNode,并重排 ForwardOverflow 的双重类型断言的括号化格式。

测试框架迁移

Layer / File(s) Summary
Jest 配置更新
jest.config.js
移除 enzyme-to-json snapshot 序列化器配置,保留 setupFiles 指向新的 tests/setup.js
测试运行时 polyfills
tests/setup.js
移除 Enzyme 适配/原型扩展,新增全局 polyfill:requestAnimationFrame、空实现 ResizeObserver、注入 TextEncoder/TextDecoder、并将 MessageChannel 置为 undefined
Wrapper 实现重写
tests/wrapper.ts
将 Enzyme 的 mount 封装替换为基于 @testing-library/reactrender,新增 NodeCollectionsetElementSizenormalizeStyleflushResize,并实现 triggerResize/triggerItemResize/initSize 等测试工具。
SSR 与测试导入更新
tests/ssr.spec.tsx, tests/github.spec.tsx, tests/raw.spec.tsx
SSR 测试改为使用 react-dom/serverrenderToStaticMarkup,并把若干测试文件的导入路径与断言调整为包级入口与新的 wrapper API。
测试用例异步化与断言调整
tests/prefix.spec.tsx, tests/responsive.spec.tsx, tests/index.spec.tsx
将测试改为 async,在需要时 await wrapper.initSize/triggerItemResize,在 afterEach 中用 act 包裹 jest.runAllTimers(),并调整断言从 key/Enzyme 风格改为基于 DOM 文本与新查找接口。

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • react-component/overflow#56: 继续同样的 @rc-component 命名空间导入迁移,在多个文件中将深层导入替换为包级入口。

Suggested reviewers

  • afc163
  • zombieJ

Poem

🐰 从 Enzyme 跳跃到 Testing 库,依赖升到十八岁,
导入路径统一整整齐齐,测试用例异步把时序理清晰,
polyfill 护航 Resize,wrapper 新规矩,
小兔子鼓掌唱一曲,迁移顺利无误意。 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 拉取请求标题「Avoid rc package deep imports」准确概括了主要变更内容,即避免深层导入rc包内部路径,改为使用公共API入口。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/Overflow.tsx

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

tests/github.spec.tsx

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.

tests/wrapper.ts

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@socket-security
Copy link
Copy Markdown

socket-security Bot commented May 25, 2026

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​types/​react-dom@​18.3.71001007685100
Updated@​types/​react@​19.2.15 ⏵ 18.3.2910010079 +196100
Added@​testing-library/​react@​16.3.29910010087100

View full report

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request migrates the project's testing infrastructure from Enzyme to React Testing Library and upgrades React to version 18. Key changes include the removal of Enzyme-related dependencies, a complete rewrite of the test setup and utility wrappers to support RTL, and updating existing test suites to handle asynchronous operations and new assertion patterns. The Overflow component was also updated to provide index information to the renderItem callback and to use modernized imports from @rc-component/util. Review feedback highlights several areas for improvement in the new test wrapper, including the lack of support for React component selectors in the custom find method, potential issues with hardcoded class names when using custom prefixCls, and a performance optimization to avoid redundant DOM queries during initialization.

Comment thread tests/wrapper.ts
Comment thread tests/wrapper.ts
Comment thread tests/wrapper.ts Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
tests/wrapper.ts (1)

224-232: 💤 Low value

在循环中重复查询 DOM 可能导致意外行为。

initSize 在每次循环迭代中调用两次 queryOverflowItems() —— 一次获取长度,一次获取元素。如果 resize 触发重渲染导致 DOM 结构变化,可能会出现索引不匹配或跳过元素的情况。

建议缓存初始元素列表,或改用 while 循环在每次迭代时重新获取当前第一个未处理的元素:

♻️ 建议的改进
 async initSize(width: number, itemWidth: number) {
   await wrapper.triggerResize(width);

-  for (let index = 0; index < queryOverflowItems().length; index += 1) {
-    await triggerElementResize(queryOverflowItems()[index], itemWidth);
+  const items = queryOverflowItems();
+  for (const item of items) {
+    await triggerElementResize(item, itemWidth);
   }

   return wrapper;
 },
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/wrapper.ts` around lines 224 - 232, The loop in initSize repeatedly
calls queryOverflowItems() causing potential DOM-index mismatches after resize;
fix by capturing the initial list into a local variable (e.g., const items =
queryOverflowItems()) and iterate over that cached array when calling
triggerElementResize(items[index], itemWidth) (or alternatively replace the
for-loop with a while that re-queries and processes the first unhandled
element), ensuring you reference initSize, queryOverflowItems,
triggerElementResize and wrapper when making the change.
tests/github.spec.tsx (1)

105-109: ⚡ Quick win

建议使用更类型安全的查询方法。

当前使用 as any 绕过了类型检查。考虑以下更好的方案:

  • 使用 Testing Library 的查询方法(如 getByClassName)替代 querySelector
  • 或者使用非空断言:expect(container.querySelector('.rc-overflow-item-rest')!).toHaveStyle(...)
♻️ 建议的改进方案
-    (expect(
-      container.querySelector('.rc-overflow-item-rest'),
-    ) as any).toHaveStyle({
+    expect(
+      container.querySelector('.rc-overflow-item-rest')!,
+    ).toHaveStyle({
       opacity: 1,
     });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/github.spec.tsx` around lines 105 - 109, Replace the unsafe cast using
"as any" when checking the element with selector '.rc-overflow-item-rest' in
tests/github.spec.tsx: use a type-safe DOM access instead (e.g., call
querySelector and assert it is non-null via a non-null assertion, or use
container.getElementsByClassName('rc-overflow-item-rest') and pick the first
element) and then call toHaveStyle on that element; update the assertion so it
no longer relies on "as any" and ensures the element is present before asserting
its style.
tests/index.spec.tsx (1)

99-105: ⚖️ Poor tradeoff

测试覆盖度不足:itemKey 用例仅断言渲染文本,未验证 React key 行为
tests/index.spec.tsx(99-116)中 itemKey(string/function)两用例都只检查 wrapper.findItems().text(),而 React key 不会出现在 DOM;tests/wrapper.ts 也没有提供读取 React key 的能力。建议:

  • 补充“重复 key 导致的 console.error/console.warn”这类间接校验(当前测试里未见相关拦截/断言,tests/setup.js 也未配置)。
  • 或将用例名称改为反映实际验证内容(例如 “renders items correctly”)。
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/index.spec.tsx` around lines 99 - 105, The test titled 'string' only
asserts rendered text and doesn't verify React key behavior; update the test to
either (A) assert key-related side effects by spying on
console.error/console.warn (use jest.spyOn(console, 'error'|'warn') and
mockImplementation) and feed Overflow (renderItem/getData) a dataset with
duplicate itemKey values to trigger the warning, or (B) rename the test to a
descriptive title like "renders items correctly" to reflect that it only checks
DOM text; adjust the other itemKey (function) test similarly and ensure
tests/wrapper.ts or tests/setup.js includes any necessary console spies if you
choose option A.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/github.spec.tsx`:
- Around line 105-109: Replace the unsafe cast using "as any" when checking the
element with selector '.rc-overflow-item-rest' in tests/github.spec.tsx: use a
type-safe DOM access instead (e.g., call querySelector and assert it is non-null
via a non-null assertion, or use
container.getElementsByClassName('rc-overflow-item-rest') and pick the first
element) and then call toHaveStyle on that element; update the assertion so it
no longer relies on "as any" and ensures the element is present before asserting
its style.

In `@tests/index.spec.tsx`:
- Around line 99-105: The test titled 'string' only asserts rendered text and
doesn't verify React key behavior; update the test to either (A) assert
key-related side effects by spying on console.error/console.warn (use
jest.spyOn(console, 'error'|'warn') and mockImplementation) and feed Overflow
(renderItem/getData) a dataset with duplicate itemKey values to trigger the
warning, or (B) rename the test to a descriptive title like "renders items
correctly" to reflect that it only checks DOM text; adjust the other itemKey
(function) test similarly and ensure tests/wrapper.ts or tests/setup.js includes
any necessary console spies if you choose option A.

In `@tests/wrapper.ts`:
- Around line 224-232: The loop in initSize repeatedly calls
queryOverflowItems() causing potential DOM-index mismatches after resize; fix by
capturing the initial list into a local variable (e.g., const items =
queryOverflowItems()) and iterate over that cached array when calling
triggerElementResize(items[index], itemWidth) (or alternatively replace the
for-loop with a while that re-queries and processes the first unhandled
element), ensuring you reference initSize, queryOverflowItems,
triggerElementResize and wrapper when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 418c9460-567f-4aaf-be4a-66fd3b945b45

📥 Commits

Reviewing files that changed from the base of the PR and between 3636d9e and 103b729.

📒 Files selected for processing (14)
  • examples/fill-width.tsx
  • jest.config.js
  • package.json
  • src/Overflow.tsx
  • src/hooks/channelUpdate.ts
  • src/hooks/useEffectState.tsx
  • tests/github.spec.tsx
  • tests/index.spec.tsx
  • tests/prefix.spec.tsx
  • tests/raw.spec.tsx
  • tests/responsive.spec.tsx
  • tests/setup.js
  • tests/ssr.spec.tsx
  • tests/wrapper.ts
💤 Files with no reviewable changes (1)
  • jest.config.js

@QDyanbing
Copy link
Copy Markdown
Contributor Author

Refs ant-design/ant-design#58115

antd 侧统一跟踪 rc 包 es/lib 深路径引用问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant