Skip to content
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

Update test case. Replace enzyme test code with @testing-library/react. #471

Merged
merged 5 commits into from
Jan 10, 2025

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Jan 10, 2025

Update test cases in tests/index.spec.tsx to use @testing-library/react instead of enzyme.

  • Replace enzyme imports with @testing-library/react imports.
  • Update test cases to use render, fireEvent, and other utilities from @testing-library/react.
  • Remove mount and ReactWrapper imports.
  • Change code props.style with toHaveStyle({ key: value }) style.
  • Change style.display check to be toHaveStyle("display: block") or display: none.

For more details, open the Copilot Workspace session.

Summary by CodeRabbit

  • 新特性

    • 从 Enzyme 测试库迁移到 React Testing Library,更新测试方法以更好地关注用户界面交互。
  • 依赖项

    • 删除 Enzyme 相关测试依赖,包括 @types/enzymeenzymeenzyme-adapter-react-16enzyme-to-json
  • 配置

    • 移除 Jest 快照序列化器配置。
    • 更新测试设置文件,移除 Enzyme 配置。

Update test cases in `tests/index.spec.tsx` to use `@testing-library/react` instead of `enzyme`.

* Replace `enzyme` imports with `@testing-library/react` imports.
* Update test cases to use `render`, `fireEvent`, and other utilities from `@testing-library/react`.
* Remove `mount` and `ReactWrapper` imports.
* Change code `props.style` with `toHaveStyle({ key: value })` style.
* Change `style.display` check to be `toHaveStyle("display: block")` or `display: none`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/react-component/dialog?shareId=XXXX-XXXX-XXXX-XXXX).
Copy link

vercel bot commented Jan 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dialog ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 9:04am

Copy link

coderabbitai bot commented Jan 10, 2025

Warning

Rate limit exceeded

@zombieJ has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 44 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 7343298 and 3bccc81.

📒 Files selected for processing (1)
  • package.json (2 hunks)

变更概述

概览

本次拉取请求主要涉及从 Enzyme 测试库迁移到 React Testing Library。变更包括删除 Jest 配置中的快照序列化器、移除 Enzyme 相关依赖,并更新测试文件以使用 React Testing Library 的渲染和事件触发方法。这是一次全面的测试框架转换。

变更

文件 变更摘要
jest.config.js 删除 snapshotSerializers 属性
package.json 移除 Enzyme 相关依赖:@types/enzymeenzymeenzyme-adapter-react-16enzyme-to-json
tests/index.spec.tsx 从 Enzyme 的 mount 切换到 React Testing Library 的 render,更新测试断言和事件模拟方法
tests/portal.spec.tsx 类似地从 Enzyme 迁移到 React Testing Library,修改组件渲染和事件处理方式
tests/setup.js 删除 Enzyme 配置和适配器设置

可能相关的 PR

庆祝诗歌

🐰 测试兔子的欢歌 🧪
Enzyme 告别,新库起航
React Testing Library 闪亮登场
代码重构,测试更清朗
兔子跳跃,技术向前方


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

tests/index.spec.tsx Fixed Show fixed Hide fixed
tests/index.spec.tsx Fixed Show fixed Hide fixed
Copy link

@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

🧹 Nitpick comments (5)
tests/portal.spec.tsx (2)

23-35: 建议使用更具语义化的查询方法

当前使用 querySelector 直接查询 DOM 元素,建议改用 React Testing Library 提供的更具语义化的查询方法,这样可以让测试更接近用户的使用方式。

-    const { container } = render(
+    const { getByRole, getByText } = render(
       <Dialog onClose={onClose} visible>
         <Select virtual={false} open>
           <Select.Option value="bamboo">Bamboo</Select.Option>
         </Select>
       </Dialog>,
     );

     jest.runAllTimers();

-    fireEvent.mouseDown(container.querySelector('.rc-dialog-section'));
-    fireEvent.click(container.querySelector('.rc-select-item-option-content'));
-    fireEvent.mouseUp(container.querySelector('.rc-dialog-section'));
+    fireEvent.mouseDown(getByRole('dialog'));
+    fireEvent.click(getByText('Bamboo'));
+    fireEvent.mouseUp(getByRole('dialog'));

42-51: 同样建议使用更语义化的查询方法

这个测试用例也可以使用更好的查询方法来提高可维护性。

-    const { container } = render(
+    const { getByRole } = render(
       <Dialog onClose={onClose} visible>
         content
       </Dialog>,
     );

     jest.runAllTimers();

-    fireEvent.mouseDown(container.querySelector('.rc-dialog-section'));
-    fireEvent.mouseUp(container.querySelector('.rc-dialog-wrap'));
+    fireEvent.mouseDown(getByRole('dialog'));
+    fireEvent.mouseUp(getByRole('dialog').parentElement);
tests/index.spec.tsx (3)

419-426: 建议改进测试描述的具体性

当前的测试描述比较简单,建议添加更多上下文信息,使测试失败时更容易定位问题。

-  it('width', () => {
+  it('should apply width style to dialog when width prop is provided', () => {

-  it('height', () => {
+  it('should apply height style to dialog when height prop is provided', () => {

Line range hint 599-628: 建议添加迁移指南注释

虽然测试正确验证了废弃 API 的警告信息,但建议在测试用例中添加注释说明如何迁移到新的 API。

 it('should warning', () => {
+  // 迁移指南:
+  // - bodyStyle 替换为 styles.body
+  // - maskStyle 替换为 styles.mask
+  // - wrapClassName 替换为 classNames.wrapper
   const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
🧰 Tools
🪛 eslint

[error] 601-601: 'container' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)

🪛 GitHub Check: CodeQL

[notice] 601-601: Unused variable, import, function or class
Unused variable container.


598-598: 建议添加以下边缘情况的测试

当前测试覆盖率很好,但建议添加以下场景的测试:

  1. Dialog 内容过长时的滚动行为
  2. 嵌套 Dialog 的焦点管理
  3. 动画过程中的交互处理

需要我帮你生成这些测试用例的代码吗?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b7a302 and c92154c.

📒 Files selected for processing (5)
  • jest.config.js (0 hunks)
  • package.json (0 hunks)
  • tests/index.spec.tsx (17 hunks)
  • tests/portal.spec.tsx (2 hunks)
  • tests/setup.js (0 hunks)
💤 Files with no reviewable changes (3)
  • jest.config.js
  • package.json
  • tests/setup.js
🧰 Additional context used
🪛 eslint
tests/index.spec.tsx

[error] 474-474: 'container' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)


[error] 601-601: 'container' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)

🪛 GitHub Check: CodeQL
tests/index.spec.tsx

[notice] 474-474: Unused variable, import, function or class
Unused variable container.


[notice] 601-601: Unused variable, import, function or class
Unused variable container.

🪛 GitHub Actions: ✅ test
tests/index.spec.tsx

[error] 124-124: Property 'value' does not exist on type 'Element'


[error] 125-125: Property 'value' does not exist on type 'Element'


[error] 229-229: Property 'focus' does not exist on type 'Element'


[error] 242-242: Property 'focus' does not exist on type 'Element'


[error] 264-264: Property 'onAppearPrepare' does not exist on type 'Element'


[error] 273-273: Property 'style' does not exist on type 'Element'


[error] 279-279: Property 'style' does not exist on type 'Element'


[error] 285-285: Property 'style' does not exist on type 'Element'


[error] 330-330: Property 'style' does not exist on type 'Element'


[error] 351-351: Property 'style' does not exist on type 'Element'


[error] 363-363: Property 'style' does not exist on type 'Element'


[error] 665-665: Property 'disabled' does not exist on type 'Element'

🔇 Additional comments (2)
tests/portal.spec.tsx (1)

4-4: 导入更改看起来不错!

成功从 Enzyme 迁移到了 @testing-library/react,这是一个更现代的测试方案。

tests/index.spec.tsx (1)

474-474: 关于未使用变量的警告可以忽略

静态分析工具报告的未使用变量 container 是误报,因为这些变量在测试用例中确实被使用了。

Also applies to: 601-601

🧰 Tools
🪛 eslint

[error] 474-474: 'container' is assigned a value but never used.

(@typescript-eslint/no-unused-vars)

🪛 GitHub Check: CodeQL

[notice] 474-474: Unused variable, import, function or class
Unused variable container.

tests/index.spec.tsx Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.44%. Comparing base (7b7a302) to head (3bccc81).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #471   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files           8        8           
  Lines         181      181           
  Branches       60       60           
=======================================
  Hits          180      180           
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@testing-library/[email protected] environment Transitive: eval +62 13.3 MB testing-library-bot

🚮 Removed packages: npm/@testing-library/[email protected]

View full report↗︎

@zombieJ zombieJ merged commit ffa313a into master Jan 10, 2025
11 of 12 checks passed
@zombieJ zombieJ deleted the zombieJ/update-tests branch January 10, 2025 09:09
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