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

fix: antd-issue-51248 #541

Merged
merged 6 commits into from
Oct 16, 2024
Merged

Conversation

dongbanban
Copy link
Contributor

@dongbanban dongbanban commented Oct 15, 2024

fix ant-design/ant-design#51248

searchConfig.limit <= 0 时重新赋值为 false

Summary by CodeRabbit

  • 新功能
    • 更新了搜索配置处理逻辑,限制属性现在设置为false而不是被删除。
    • 在非生产环境中,如果限制不是正数或设置为false,将发出警告。
  • 测试
    • 新增了针对Cascader.Search组件的测试套件,确保组件在不同限制条件下的行为符合预期。

Copy link

coderabbitai bot commented Oct 15, 2024

Walkthrough

此次更改涉及 src/hooks/useSearchConfig.ts 文件中的 useSearchConfig 函数。主要修改在于对 searchConfig 对象中 limit 属性的处理。现在的条件检查 searchConfig.limit 是否小于或等于零且不等于 false,在这种情况下将 limit 属性设置为 false。此外,在非生产环境中,如果 limit 不是正数或被设置为 false,会发出警告。函数的整体结构和逻辑保持不变,返回类型和记忆化逻辑也未更改。

Changes

文件路径 更改摘要
src/hooks/useSearchConfig.ts 修改 limit 属性处理方式,设置为 false,并在非生产环境中发出警告
tests/search.limit.spec.tsx 新增 Cascader.Search 组件的测试套件,包含 doSearch 辅助函数和 limit 测试用例

Possibly related PRs

  • feat: test #531: 此 PR 增强了 Cascader.Search 组件的测试用例,与主 PR 中关于搜索功能的 limit 属性处理的修改相关。

Poem

在兔子洞中欢跃跳,
新的限制不再抛。
设置为假不再删,
警告声声在耳边。
搜索配置更灵巧,
兔子乐在新变化! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b717b24 and d524f04.

📒 Files selected for processing (2)
  • src/hooks/useSearchConfig.ts (1 hunks)
  • tests/search.limit.spec.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/hooks/useSearchConfig.ts
  • tests/search.limit.spec.tsx

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

@afc163
Copy link
Member

afc163 commented Oct 15, 2024

来个测试用例

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.83%. Comparing base (31a7de5) to head (d524f04).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #541      +/-   ##
==========================================
+ Coverage   99.67%   99.83%   +0.16%     
==========================================
  Files          23       23              
  Lines         611      611              
  Branches      185      176       -9     
==========================================
+ Hits          609      610       +1     
+ Misses          2        1       -1     

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

Copy link

vercel bot commented Oct 15, 2024

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

Name Status Preview Comments Updated (UTC)
cascader ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2024 2:25am

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: 0

🧹 Outside diff range and nitpick comments (1)
src/hooks/useSearchConfig.ts (1)

Line range hint 26-30: 代码变更符合预期,但可以进一步优化

这些更改很好地解决了 PR 中描述的问题。将 limit 设置为 false 而不是删除它是一个好方法,可以保持对象结构的一致性。添加警告也有助于开发人员在开发过程中捕获潜在问题。

为了提高代码的可读性和类型安全性,我建议将条件检查稍作修改:

-if ((searchConfig.limit as number) <= 0) {
+if (typeof searchConfig.limit === 'number' && searchConfig.limit <= 0) {
   searchConfig.limit = false;

   if (process.env.NODE_ENV !== 'production') {
     warning(false, "'limit' of showSearch should be positive number or false.");
   }
 }

这样可以避免使用类型断言,并更明确地表达意图。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 31a7de5 and 866a7e5.

📒 Files selected for processing (1)
  • src/hooks/useSearchConfig.ts (2 hunks)
🧰 Additional context used

@dongbanban
Copy link
Contributor Author

来个测试用例

npm run test ./tests/search.limit.spec.tsx

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

🧹 Outside diff range and nitpick comments (1)
tests/search.limit.spec.tsx (1)

14-29: 选项定义结构合理,但可以稍作改进。

选项的定义结构很好,创建了一个包含100个子选项的父选项'Asia'。这对于测试搜索功能和性能很有帮助。

建议:考虑使用Array.from和箭头函数来简化子选项的创建,使代码更简洁:

options[0].children = Array.from({ length: 100 }, (_, i) => ({
  id: i,
  label: `label${i}`,
  value: `value${i}`,
}));

这样可以减少代码行数并提高可读性。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 866a7e5 and 3995580.

📒 Files selected for processing (1)
  • tests/search.limit.spec.tsx (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
tests/search.limit.spec.tsx (2)

1-4: 导入语句看起来很好。

导入语句包含了测试所需的所有必要模块,包括React、Cascader组件和Enzyme测试工具。没有未使用的导入。


7-13: doSearch 辅助函数实现得很好。

这个函数很好地封装了模拟搜索输入的逻辑,使用了Enzyme的simulate方法来模拟输入变化。这将使测试用例更加清晰和可维护。

Comment on lines 31 to 45
it('limit', () => {
const wrapper = mount(
<Cascader
options={options}
open
showSearch={{
limit: false,
}}
/>,
);

doSearch(wrapper, 'as');
const itemList = wrapper.find('div.rc-cascader-menu-item-content');
expect(itemList).toHaveLength(itemList.length);
});
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

测试用例可以更具体和描述性。

当前的测试用例结构正确,但可以进行以下改进:

  1. 测试描述应更具体,例如:"当showSearch.limit设置为false时,应显示所有匹配项"。

  2. 断言可以更明确。目前的断言只是检查项目列表的长度与自身相等,这并不能真正验证功能。建议改为:

    expect(itemList).toHaveLength(101); // 100个子项 + 1个父项
  3. 可以添加一个反例,测试当limit设置为一个数值时的行为。

  4. 考虑测试搜索结果的内容,而不仅仅是数量。

这些改进将使测试更加健壮和有意义。

如果您需要,我可以帮助您重写这个测试用例。

options={options}
open
showSearch={{
limit: false,

Choose a reason for hiding this comment

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

Suggested change
limit: false,
limit: 0,

Choose a reason for hiding this comment

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

是不是用 0 比较好, pr修复的是<= 0的情况下,limit变成undefined然后又重新默认值50了,false本身是好的。

Copy link
Contributor Author

@dongbanban dongbanban Oct 16, 2024

Choose a reason for hiding this comment

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

image image

定位的问题出在limit = false时会走进 if的判断,导致 limit被 delete,所以测试用例里是用的limit:false, 或者直接再加一层判断,规避上述场景,这样原本的逻辑没变,false也可以当 no limit处理

Choose a reason for hiding this comment

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

如果误导你了不好意思,还是等大佬们建议。我评论的是测试用例用个 limit: 0,,因为之前limit: false的情况本来就是好的,是limit:0才出了问题, 感觉这里面的业务逻辑是 searchConfig.limit = 0; or searchConfig.limit = false; 可能都行。你改回了这个delete不是又导致了被默认赋值50的问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

测试用例我加上 limit:0/false/20 了,limit:false 是有问题的,实际使用依然会走进 if,大佬你可以试一下,官网 demo 里改成limit:false 依然是50 条

Choose a reason for hiding this comment

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

delete之后会变成undefined所以重新赋值了,所以是不是给个默认复制 0 可以呢
image

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

所以这里的逻辑 limit = false或者<=0都是展示全量数据,我最开始的修改进入判断后都是直接limit设为 false应该就可以的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image image image image image

这样就可以了吧

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: 0

🧹 Outside diff range and nitpick comments (2)
tests/search.limit.spec.tsx (2)

29-59: 建议改进前两个测试用例。

这两个测试用例的结构正确,但有以下几点可以改进:

  1. 测试描述应更具体,例如:

    • "当showSearch.limit设置为false时,应显示所有匹配项"
    • "当showSearch.limit设置为0时,应使用默认限制50"
  2. 第一个测试用例(limit: false)的断言不够有效。建议改为:

    expect(itemList).toHaveLength(101); // 100个子项 + 1个父项
  3. 考虑添加对搜索结果内容的检查,而不仅仅是数量。例如,验证第一个和最后一个项目的标签。

这些改进将使测试更加健壮和有意义。


61-76: 第三个测试用例实现正确,建议小幅改进。

这个测试用例正确地验证了自定义限制(20)的情况。为了进一步提高其可读性和明确性,建议:

  1. 使用更具描述性的测试名称,例如:

    it('当showSearch.limit设置为20时,应只显示20个匹配项', () => {
      // ... 现有的测试代码 ...
    });
  2. 考虑添加一个额外的断言来验证显示的项目是否正确,例如检查第一个和最后一个项目的标签。

这些小改动将使测试的目的更加清晰。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9c2b525 and 124d7c9.

📒 Files selected for processing (2)
  • src/hooks/useSearchConfig.ts (1 hunks)
  • tests/search.limit.spec.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useSearchConfig.ts
🧰 Additional context used
🔇 Additional comments (2)
tests/search.limit.spec.tsx (2)

1-13: 导入和辅助函数实现正确。

导入语句和doSearch辅助函数的实现看起来都很合适,没有发现任何问题。


14-28: 选项数组创建正确。

为测试目的创建的选项数组结构合理,包含一个父项"Asia"和100个子项。这提供了足够的数据来测试搜索限制功能。

Copy link

vercel bot commented Oct 16, 2024

Deployment failed with the following error:

The provided GitHub repository does not contain the requested branch or commit reference. Please ensure the repository is not empty.

@thinkasany
Copy link

thinkasany commented Oct 16, 2024

@afc163 , @zombieJ 佬, ci加上了,有空的时候帮忙看看。

@zombieJ
Copy link
Member

zombieJ commented Oct 16, 2024

CI 挂了,奇怪。看起来不像是和这个 PR 有关的


重跑后好了,似乎是 bun 对 deps 的解析有点问题。偶尔会把底层的依赖强行提上来。

@zombieJ zombieJ merged commit 4c13a89 into react-component:master Oct 16, 2024
11 checks passed
@thinkasany
Copy link

@dongbanban
🎉 Thank you for your contribution! If you have not yet joined our DingTalk community group, please feel free to join us (when joining, please provide the link to this PR).
🎉 感谢您的贡献!如果您还没有加入钉钉社区群,请扫描下方二维码加入我们(加群时请提供此 PR 链接)。

lihongxiangfrontend pushed a commit to lihongxiangfrontend/cascader that referenced this pull request Oct 25, 2024
* fix: antd-issue-51248

* feat: add test for 51248

* feat: update test for limit

* feat: update test for limit

* feat: update test for limit

* feat: update test for limit
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.

Cascader showSearch 的 limit 设为 false 后依然只展示前 50 条匹配结果
4 participants