Skip to content

为 #1949 补充 loading 隔离单测和 demo - #1982

Open
wenzeyu8888-rgb wants to merge 1 commit into
ant-design:mainfrom
wenzeyu8888-rgb:fix/x-markdown-loading-isolation-1949
Open

为 #1949 补充 loading 隔离单测和 demo#1982
wenzeyu8888-rgb wants to merge 1 commit into
ant-design:mainfrom
wenzeyu8888-rgb:fix/x-markdown-loading-isolation-1949

Conversation

@wenzeyu8888-rgb

@wenzeyu8888-rgb wenzeyu8888-rgb commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Closes #1949

背景

XMarkdown 在流式渲染时,多个自定义组件的 streamStatus(loading/done)需要按组件实例隔离,而非按标签名共享。PR #1590 已修复核心逻辑,本 PR 补充验收所需的测试和 demo。

改动内容

  • 单测loading-isolation.test.tsx):13 个测试用例,覆盖三层验证:

    • Renderer 层:不同标签名、同标签名、交错排列、嵌套、自闭合等场景
    • 完整管道:Parser(marked) + DOMPurify + html-react-parser 全链路
    • XMarkdown 组件层:流式渲染中各组件独立 loading/done 转换
  • Demomulti-component-loading.tsx):展示多个自定义组件在流式渲染中各自独立 loading 的效果

Summary by CodeRabbit

  • Bug 修复

    • 修复流式渲染中多个自定义组件加载状态相互影响的问题。
    • 现在每个组件实例可独立显示 loading 或完成状态,状态会随对应标签闭合及流式结束正确更新。
  • 文档

    • 新增多组件独立加载状态的流式渲染示例,并同步更新中英文文档示例列表。

…#1949

Add unit tests verifying streamStatus is isolated per component instance:
- Renderer-level: different/same tag names, interleaved, nested, self-closing
- Full pipeline: Parser + DOMPurify + html-react-parser
- XMarkdown component: streaming loading/done transitions

Add demo showing multiple custom components with independent loading states.

Closes ant-design#1949
@dosubot dosubot Bot added the documentation Improvements or additions to documentation label Jul 17, 2026
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

新增自定义组件实例级 streamStatus 隔离测试,覆盖解析与流式渲染场景;新增多组件流式演示,并更新中英文文档示例列表。

Changes

加载状态隔离

Layer / File(s) Summary
加载状态隔离测试
packages/x-markdown/src/XMarkdown/__tests__/loading-isolation.test.tsx
新增 Renderer、Parser + Renderer 与 XMarkdown 组件级测试,验证不同及同名组件实例的 loading/done 状态按闭合情况独立变化。
流式演示与文档入口
packages/x/docs/x-markdown/demo/streaming/multi-component-loading.tsx, packages/x/docs/x-markdown/streaming.en-US.md, packages/x/docs/x-markdown/streaming.zh-CN.md
新增 DataCardInfoPanel 流式 loading 演示,并在中英文文档中加入示例入口。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Bubble
  participant XMarkdown
  participant DataCard
  participant InfoPanel
  Bubble->>XMarkdown: 按 index 提供流式内容
  XMarkdown->>DataCard: 传递独立 streamStatus
  XMarkdown->>InfoPanel: 传递独立 streamStatus
  DataCard-->>XMarkdown: 渲染 loading 或 done
  InfoPanel-->>XMarkdown: 渲染 loading 或 done
Loading

Suggested labels: enhancement, javascript

Suggested reviewers: div627

Poem

我是兔子蹦蹦跳,
loading 状态各自跑。
DataCard 先闭合,
InfoPanel 还在摇。
测试文档齐欢笑,
月光下把代码夸。

🚥 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 标题准确概括了本次为 #1949 补充 loading 隔离单测和示例 Demo 的主要变更。
Linked Issues check ✅ Passed 已新增多自定义组件 Demo 和单元测试,且测试覆盖各组件 loading 状态彼此独立,符合 #1949 验收要求。
Out of Scope Changes check ✅ Passed 改动集中在 loading 隔离相关测试、Demo 和文档示例,没有明显无关的额外变更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

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

Copy link
Copy Markdown
Contributor

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 introduces tests and a demo to verify and showcase loading isolation for multiple custom components during streaming (Issue #1949). Specifically, it adds a comprehensive test suite in loading-isolation.test.tsx, a demo application in multi-component-loading.tsx, and updates the corresponding documentation. The review feedback suggests improving the demo's theme detection by inspecting token colors instead of relying on the fragile theme.id property, and refactoring the test suite to use afterEach for restoring mocks to prevent test pollution if assertions fail.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +89 to +90
const { theme: antdTheme } = theme.useToken();
const className = antdTheme.id === 0 ? 'x-markdown-light' : 'x-markdown-dark';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

在 Ant Design v5 中,theme.useToken() 返回的 theme.id@ant-design/cssinjs 内部用于缓存主题的自增标识符。依赖 antdTheme.id === 0 来判断是否为亮色主题是非常脆弱且不可靠的。例如,如果页面中存在多个 ConfigProvider,或者进行了动态主题切换,亮色主题的 id 可能会变成其他数字,导致主题样式判断错误。

建议通过判断 Token 中的颜色值(例如 token.colorBgContainer 是否为暗色背景)来更可靠地判断当前是否为暗色模式。

Suggested change
const { theme: antdTheme } = theme.useToken();
const className = antdTheme.id === 0 ? 'x-markdown-light' : 'x-markdown-dark';
const { token } = theme.useToken();
const isDark = token.colorBgContainer === '#141414' || token.colorBgContainer === '#000';
const className = isDark ? 'x-markdown-dark' : 'x-markdown-light';

streamStatus: string;
}

describe('Loading isolation (Issue #1949)', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

在测试用例中,每个 it 块内部都使用了 jest.spyOn(React, 'createElement') 并在末尾调用了 spy.mockRestore()。然而,如果测试中的断言(expect)失败,代码将提前抛出错误,导致后面的 spy.mockRestore() 无法执行。这会造成测试污染,影响后续其他测试用例的执行。

建议在 describe 块中使用 afterEach 来统一恢复所有的 mock,这样即使断言失败也能保证清理工作正常执行。

Suggested change
describe('Loading isolation (Issue #1949)', () => {
describe('Loading isolation (Issue #1949)', () => {
afterEach(() => {
jest.restoreAllMocks();
});

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 `@packages/x/docs/x-markdown/demo/streaming/multi-component-loading.tsx`:
- Line 77: Update the InfoPanel text in the multi-component loading demo to say
it “loads independently” instead of “downloads independently,” preserving the
rest of the message unchanged.
🪄 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: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d59e0a24-8fda-4ccd-b7d2-ad276ddfc697

📥 Commits

Reviewing files that changed from the base of the PR and between 17db20b and b85af65.

📒 Files selected for processing (4)
  • packages/x-markdown/src/XMarkdown/__tests__/loading-isolation.test.tsx
  • packages/x/docs/x-markdown/demo/streaming/multi-component-loading.tsx
  • packages/x/docs/x-markdown/streaming.en-US.md
  • packages/x/docs/x-markdown/streaming.zh-CN.md


Some text between components.

<info-panel>Component B: This info panel downloads independently.</info-panel>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

将 “downloads independently” 改为 “loads independently”。

当前文案错误地表示 InfoPanel 会独立“下载”,与演示的 loading 状态含义不符。

-<info-panel>Component B: This info panel downloads independently.</info-panel>
+<info-panel>Component B: This info panel loads independently.</info-panel>
📝 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
<info-panel>Component B: This info panel downloads independently.</info-panel>
<info-panel>Component B: This info panel loads independently.</info-panel>
🤖 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 `@packages/x/docs/x-markdown/demo/streaming/multi-component-loading.tsx` at
line 77, Update the InfoPanel text in the multi-component loading demo to say it
“loads independently” instead of “downloads independently,” preserving the rest
of the message unchanged.

@wenzeyu8888-rgb

Copy link
Copy Markdown
Contributor Author

#1949

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

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Good First Issue] XMarkdown 多个自定义组件同时进入 loading

1 participant