Skip to content

Commit fb8d172

Browse files
author
HongwuQz
committed
refactor(test): simplify Mermaid tests with getActionButtonByLabel helper
1 parent 4f6bc78 commit fb8d172

File tree

2 files changed

+78
-16
lines changed

2 files changed

+78
-16
lines changed

PR_TEMPLATE.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
=====
2+
3+
<!--
4+
First of all, thank you for your contribution! 😄
5+
For requesting to pull a new feature or bugfix, please send it from a feature/bugfix branch based on the `master` branch.
6+
Before submitting your pull request, please make sure the checklist below is confirmed.
7+
Your pull requests will be merged after one of the collaborators approve.
8+
Thank you!
9+
-->
10+
11+
[中文版模板 / Chinese template](https://github.com/ant-design/x/blob/master/.github/PULL_REQUEST_TEMPLATE_CN.md?plain=1)
12+
13+
### 🤔 This is a ...
14+
15+
- [ ] 🆕 New feature
16+
- [ ] 🐞 Bug fix
17+
- [ ] 📝 Site / documentation improvement
18+
- [ ] 📽️ Demo improvement
19+
- [ ] 💄 Component style improvement
20+
- [ ] 🤖 TypeScript definition improvement
21+
- [ ] 📦 Bundle size optimization
22+
- [ ] ⚡️ Performance optimization
23+
- [ ] ⭐️ Feature enhancement
24+
- [ ] 🌐 Internationalization
25+
- [x] 🛠 Refactoring
26+
- [ ] 🎨 Code style optimization
27+
- [x] ✅ Test Case
28+
- [ ] 🔀 Branch merge
29+
- [ ] ⏩ Workflow
30+
- [ ] ⌨️ Accessibility improvement
31+
- [ ] ❓ Other (about what?)
32+
33+
### 🔗 Related Issues
34+
35+
<!-- If there's a related issue, please link it here. If not, you can remove this section. -->
36+
37+
### 💡 Background and Solution
38+
39+
**Background:** Refactor Mermaid component to use the unified `Actions` component for operation buttons, replacing custom button implementations to improve code consistency and maintainability.
40+
41+
**Solution:**
42+
43+
- Use `@ant-design/x/es/actions` component to render operation buttons (copy, zoom in, zoom out, reset, download)
44+
- Dynamically display different action items based on render mode (image/code)
45+
- Update test cases to adapt to Actions component's DOM structure (use `getByLabelText` to find icons, then locate clickable container via `closest`)
46+
- Add Chinese localization for Mermaid component
47+
48+
**Changes:**
49+
50+
- Replace custom button implementation with Actions component
51+
- Fix test case element queries (Actions renders `div` instead of `button`)
52+
- Update localization files with Mermaid-related Chinese translations
53+
54+
### 📝 Change Log
55+
56+
> - Read [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) like a cat tracks a laser pointer.
57+
> - Describe the impact of the changes on developers, not the solution approach.
58+
> - Reference: https://x.ant.design/changelog
59+
60+
| Language | Changelog |
61+
| --- | --- |
62+
| 🇺🇸 English | Refactor Mermaid component to use Actions component for operation buttons, improving code consistency and maintainability. |
63+
| 🇨🇳 Chinese | 重构 Mermaid 组件,使用 Actions 组件统一渲染操作按钮,提升代码一致性和可维护性。 |

packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
22
import React from 'react';
33
import Mermaid from '../index';
44

5+
const getActionButtonByLabel = (label: string): HTMLElement => {
6+
const icon = screen.getByLabelText(label);
7+
const button = icon.closest('.ant-actions-item');
8+
if (!button) throw new Error(`Action button with label '${label}' not found.`);
9+
return button as HTMLElement;
10+
};
11+
512
// Mock mermaid
613
jest.mock('mermaid', () => ({
714
initialize: jest.fn(),
@@ -135,8 +142,7 @@ describe('Mermaid Plugin', () => {
135142

136143
render(<Mermaid>{mermaidContent}</Mermaid>);
137144

138-
const copyIcon = screen.getByLabelText('copy');
139-
const copyButton = copyIcon.closest('.ant-actions-item') as HTMLElement;
145+
const copyButton = getActionButtonByLabel('copy');
140146
fireEvent.click(copyButton);
141147

142148
await waitFor(() => {
@@ -155,8 +161,7 @@ describe('Mermaid Plugin', () => {
155161

156162
render(<Mermaid>{mermaidContent}</Mermaid>);
157163

158-
const copyIcon = screen.getByLabelText('copy');
159-
const copyButton = copyIcon.closest('.ant-actions-item') as HTMLElement;
164+
const copyButton = getActionButtonByLabel('copy');
160165

161166
// 确保点击不会抛出错误
162167
expect(() => fireEvent.click(copyButton)).not.toThrow();
@@ -179,8 +184,7 @@ describe('Mermaid Plugin', () => {
179184

180185
render(<Mermaid>{mermaidContent}</Mermaid>);
181186

182-
const copyIcon = screen.getByLabelText('copy');
183-
const copyButton = copyIcon.closest('.ant-actions-item') as HTMLElement;
187+
const copyButton = getActionButtonByLabel('copy');
184188
fireEvent.click(copyButton);
185189

186190
await waitFor(() => {
@@ -210,10 +214,8 @@ describe('Mermaid Plugin', () => {
210214
it('should handle zoom in/out', () => {
211215
render(<Mermaid>{mermaidContent}</Mermaid>);
212216

213-
const zoomInIcon = screen.getByLabelText('zoom-in');
214-
const zoomOutIcon = screen.getByLabelText('zoom-out');
215-
const zoomInButton = zoomInIcon.closest('.ant-actions-item') as HTMLElement;
216-
const zoomOutButton = zoomOutIcon.closest('.ant-actions-item') as HTMLElement;
217+
const zoomInButton = getActionButtonByLabel('zoom-in');
218+
const zoomOutButton = getActionButtonByLabel('zoom-out');
217219

218220
fireEvent.click(zoomInButton);
219221
fireEvent.click(zoomOutButton);
@@ -222,8 +224,7 @@ describe('Mermaid Plugin', () => {
222224
it('should handle reset functionality', () => {
223225
render(<Mermaid>{mermaidContent}</Mermaid>);
224226

225-
const resetIcon = screen.getByLabelText('undo');
226-
const resetButton = resetIcon.closest('.ant-actions-item') as HTMLElement;
227+
const resetButton = getActionButtonByLabel('undo');
227228
fireEvent.click(resetButton);
228229
});
229230
});
@@ -506,8 +507,7 @@ describe('Mermaid Plugin', () => {
506507
container.querySelector = mockQuerySelector;
507508
}
508509

509-
const downloadIcon = screen.getByLabelText('download');
510-
const downloadButton = downloadIcon.closest('.ant-actions-item') as HTMLElement;
510+
const downloadButton = getActionButtonByLabel('download');
511511
fireEvent.click(downloadButton);
512512

513513
// Wait for async operations
@@ -549,8 +549,7 @@ describe('Mermaid Plugin', () => {
549549
container.querySelector = mockQuerySelector;
550550
}
551551

552-
const downloadIcon = screen.getByLabelText('download');
553-
const downloadButton = downloadIcon.closest('.ant-actions-item') as HTMLElement;
552+
const downloadButton = getActionButtonByLabel('download');
554553
fireEvent.click(downloadButton);
555554

556555
// Should not throw and should return early

0 commit comments

Comments
 (0)