-
Notifications
You must be signed in to change notification settings - Fork 7
test: mock ResizeObserver for jest environment #171
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
Conversation
Walkthrough此次更改在两个文件中进行:在 Changes
Sequence Diagram(s)sequenceDiagram
participant T as 测试用例
participant G as 全局 ResizeObserver 模拟
T->>G: 调用 new ResizeObserver(callback)
G-->>T: 返回包含 observe、unobserve 和 disconnect 的对象
Poem
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
src/setup.tsOops! Something went wrong! :( ESLint: 7.32.0 ESLint couldn't find the config "airbnb" to extend from. Please check that the name of the config is correct. The config "airbnb" was referenced from the config file in "/.eslintrc". If you still have problems, please stop by https://eslint.org/chat/help to chat with the team. ✨ Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/setup.ts (1)
24-30: 建议增强 mock 实现以提供更好的测试能力当前实现可以进一步改进以提供更完整的测试支持:
- 添加类型定义和文档注释
- 实现基本的错误处理
- 跟踪被观察的元素
建议参考以下实现:
+// Mock implementation of ResizeObserver for testing environments +// See: https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver global.ResizeObserver = jest.fn(() => { + const observedElements = new Set(); return { - observe() {}, - unobserve() {}, - disconnect() {}, + observe(element) { + if (!(element instanceof Element)) { + throw new TypeError('Failed to execute observe: parameter 1 is not of type Element.'); + } + observedElements.add(element); + }, + unobserve(element) { + observedElements.delete(element); + }, + disconnect() { + observedElements.clear(); + }, }; });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
.gitignore(1 hunks)src/setup.ts(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- .gitignore
🔇 Additional comments (1)
src/setup.ts (1)
24-30: 基本实现正确且符合要求!mock 实现包含了 ResizeObserver 所需的所有基本方法(observe、unobserve 和 disconnect),并使用了 jest.fn() 来创建可跟踪的模拟函数。
Summary by CodeRabbit
Chores
Tests