Skip to content

Avoid rc package deep imports#164

Open
QDyanbing wants to merge 1 commit into
react-component:masterfrom
QDyanbing:avoid-deep-imports
Open

Avoid rc package deep imports#164
QDyanbing wants to merge 1 commit into
react-component:masterfrom
QDyanbing:avoid-deep-imports

Conversation

@QDyanbing
Copy link
Copy Markdown

@QDyanbing QDyanbing commented May 21, 2026

背景

antd 侧限制继续使用 rc 包的 lib / es 深路径导入,需要将 switch 中对 @rc-component/util 的内部路径依赖迁移到包根入口。

调整内容

  • 升级 @rc-component/father-plugin,使用插件统一拦截 rc 包 lib / es 深路径导入。
  • 升级 @rc-component/util,从 @rc-component/util 根入口导入 KeyCodeuseControlledState
  • 将测试从 Enzyme 迁移到 Testing Library,避免继续升级 Enzyme adapter。
  • 测试环境升级到 React 18,仅影响 dev/test,不调整 runtime peer 约束。

验证

  • npm test -- --runInBand
  • npm run lint
  • npm run compile

Summary by CodeRabbit

发行说明

  • Chores
    • 更新了核心依赖版本,包括工具库和开发工具链的升级。
    • 升级开发依赖至最新版本,改进了开发体验和构建性能。
    • 优化了模块导入方式以适配更新的依赖版本。

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

概览

本 PR 升级了 Switch 组件的依赖版本(特别是 @rc-component/util 至 1.11.1 和 React 至 18.0.0),并将完整的测试套件从 Enzyme 迁移至 React Testing Library,同时调整了源代码的导入方式以适配新的包结构。

变更内容

Enzyme 至 React Testing Library 完整迁移

Layer / File(s) 概述
依赖版本升级与测试框架替换
package.json
升级 @rc-component/util 至 1.11.1、React/React-dom 至 18.0.0,移除 Enzyme 和 cheerio,引入 @testing-library/react
源代码导入方式适配
src/index.tsx
将 useControlledState 和 KeyCode 从 @rc-component/util 的子路径改为从包入口进行命名导入;调整 SwitchProps 接口声明格式。
测试框架工具链更新
tests/index.spec.js (2-12 行)
替换导入为 @testing-library/react 的 render 和 fireEvent;新增 keyDownWithWhich 辅助函数。
基础交互与回调测试重写
tests/index.spec.js (22-63, 123-130 行)
将断言从 DOM class 检查改为 aria-checked 属性验证;点击和按键事件采用 fireEvent 驱动。
禁用状态、焦点管理与加载图标测试重写
tests/index.spec.js (65-121 行)
移除 Enzyme 的 attachTo,改用 render + ref 调用 focus/blur;焦点事件验证采用 fireEvent.focus/blur。
鼠标事件与样式验证测试重写
tests/index.spec.js (132-156 行)
鼠标事件和样式检验改为 RTL 的 fireEvent 和 container.querySelector 方式。

相关 PR

  • react-component/switch#158:同样调整了 src/index.tsx 中 @rc-component/util 和 KeyCode 的导入方式,与本 PR 的命名空间迁移直接关联。
  • react-component/switch#161:两个 PR 都涉及 src/index.tsx 中 @rc-component/util 钩子的使用调整,本 PR 在工具升级后调整导入方式。

建议审查人

  • zombieJ

审查工作量

🎯 3 (中等难度) | ⏱️ ~25 分钟


🐰 旧酶已褪去,RTL 测试闪耀,
易用的 aria,无障碍路开,
React 十八升,组件更灿烂,
一个开关按,从此断言变清晰!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 包进行深层路径导入。该标题与文件级别的改动完全相关:package.json 中升级了依赖以支持此功能,src/index.tsx 中实现了从包入口而非内部路径的导入方式改更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

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.

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 updates the project's dependencies, including an upgrade to React 18 and a migration of the test suite from Enzyme to React Testing Library. Additionally, it refactors imports from @rc-component/util to use named exports. The review feedback recommends improving the test implementation by explicitly providing the which property in keyboard events to align with component logic and utilizing standard React Testing Library queries like getByText for better testing practices.

Comment thread tests/index.spec.js

describe('rc-switch', () => {
function keyDownWithWhich(target, which) {
fireEvent.keyDown(target, { keyCode: which });
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

建议在 fireEvent.keyDown 中显式传入 which 属性。虽然 keyCode 已被弃用,但由于组件内部逻辑(如 src/index.tsx 第 74 行)仍在使用 e.which,在测试中模拟该属性可以确保与组件逻辑的一致性并提高测试的健壮性。

Suggested change
fireEvent.keyDown(target, { keyCode: which });
fireEvent.keyDown(target, { keyCode: which, which });

Comment thread tests/index.spec.js
Comment on lines +77 to +78
const { container } = render(<Switch loadingIcon={<span className="extra">loading</span>} />);
expect(container.querySelector('.extra').textContent).toBe('loading');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

建议使用 React Testing Library 推荐的查询方式(如 getByText)来替代 container.querySelector。这不仅更符合 RTL 的最佳实践(从用户视角出发),而且在元素未找到时能提供更清晰的错误信息。

Suggested change
const { container } = render(<Switch loadingIcon={<span className="extra">loading</span>} />);
expect(container.querySelector('.extra').textContent).toBe('loading');
const { getByText } = render(<Switch loadingIcon={<span className="extra">loading</span>} />);
expect(getByText('loading').classList.contains('extra')).toBe(true);

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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@tests/index.spec.js`:
- Around line 140-147: The test "disabled should click not to change" defines an
onClick mock via onClick and fires a click on the switch but only asserts
onChange wasn't called; update the test that uses createSwitch({ disabled: true,
onChange, onClick, checked: true }) to also assert that the onClick mock was not
called after fireEvent.click(getByRole('switch')), i.e., add
expect(onClick).not.toHaveBeenCalled() to the test to verify disabled state
prevents click handlers from running.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 404ef499-24e5-4767-8f10-d488f8a14576

📥 Commits

Reviewing files that changed from the base of the PR and between 552d320 and b2c4397.

📒 Files selected for processing (3)
  • package.json
  • src/index.tsx
  • tests/index.spec.js

Comment thread tests/index.spec.js
Comment on lines 140 to 147
it('disabled should click not to change', () => {
const onChange = jest.fn();
const onClick = jest.fn();
const wrapper = createSwitch({ disabled: true, onChange, onClick, checked: true });
const { getByRole } = createSwitch({ disabled: true, onChange, onClick, checked: true });

wrapper.simulate('click');
fireEvent.click(getByRole('switch'));
expect(onChange).not.toHaveBeenCalled();
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

考虑验证 onClick 回调也未被调用。

测试中定义了 onClick 回调(第 142 行),但只断言了 onChange 未被调用。为了更完整的测试覆盖,建议也验证禁用状态下 onClick 的行为。

💡 建议添加 onClick 断言
 fireEvent.click(getByRole('switch'));
 expect(onChange).not.toHaveBeenCalled();
+expect(onClick).not.toHaveBeenCalled();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('disabled should click not to change', () => {
const onChange = jest.fn();
const onClick = jest.fn();
const wrapper = createSwitch({ disabled: true, onChange, onClick, checked: true });
const { getByRole } = createSwitch({ disabled: true, onChange, onClick, checked: true });
wrapper.simulate('click');
fireEvent.click(getByRole('switch'));
expect(onChange).not.toHaveBeenCalled();
});
it('disabled should click not to change', () => {
const onChange = jest.fn();
const onClick = jest.fn();
const { getByRole } = createSwitch({ disabled: true, onChange, onClick, checked: true });
fireEvent.click(getByRole('switch'));
expect(onChange).not.toHaveBeenCalled();
expect(onClick).not.toHaveBeenCalled();
});
🤖 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.js` around lines 140 - 147, The test "disabled should click
not to change" defines an onClick mock via onClick and fires a click on the
switch but only asserts onChange wasn't called; update the test that uses
createSwitch({ disabled: true, onChange, onClick, checked: true }) to also
assert that the onClick mock was not called after
fireEvent.click(getByRole('switch')), i.e., add
expect(onClick).not.toHaveBeenCalled() to the test to verify disabled state
prevents click handlers from running.

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