Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): name MCP header delete actions (#40300)

yyh1828cc6a10c8
diff --git a/web/app/components/tools/mcp/__tests__/headers-input.spec.tsx b/web/app/components/tools/mcp/__tests__/headers-input.spec.tsx
index abf436329d3..64a4812603f 100644
--- a/web/app/components/tools/mcp/__tests__/headers-input.spec.tsx
+++ b/web/app/components/tools/mcp/__tests__/headers-input.spec.tsx
@@ -66,15 +66,19 @@ describe('HeadersInput', () => {
it('should render delete buttons for each item when not readonly', () => {
render(<HeadersInput {...defaultProps} headersItems={headersItems} />)
- // Should have delete buttons for each header
- const deleteButtons = document.querySelectorAll('[class*="text-text-destructive"]')
- expect(deleteButtons.length).toBe(headersItems.length)
+ expect(
+ screen.getByRole('button', { name: 'common.operation.delete Authorization 1' }),
+ ).toBeInTheDocument()
+ expect(
+ screen.getByRole('button', { name: 'common.operation.delete Content-Type 2' }),
+ ).toBeInTheDocument()
})
it('should not render delete buttons when readonly', () => {
render(<HeadersInput {...defaultProps} headersItems={headersItems} readonly={true} />)
- const deleteButtons = document.querySelectorAll('[class*="text-text-destructive"]')
- expect(deleteButtons.length).toBe(0)
+ expect(
+ screen.queryByRole('button', { name: /common\.operation\.delete/ }),
+ ).not.toBeInTheDocument()
})
it('should render add button at bottom when not readonly', () => {
@@ -129,13 +133,8 @@ describe('HeadersInput', () => {
const onChange = vi.fn()
render(<HeadersInput {...defaultProps} headersItems={headersItems} onChange={onChange} />)
- const deleteButton = document
- .querySelector('[class*="text-text-destructive"]')
- ?.closest('button')
- if (deleteButton) {
- fireEvent.click(deleteButton)
- expect(onChange).toHaveBeenCalledWith([])
- }
+ fireEvent.click(screen.getByRole('button', { name: 'common.operation.delete Header1 1' }))
+ expect(onChange).toHaveBeenCalledWith([])
})
it('should add new item when add button is clicked', () => {
@@ -170,9 +169,7 @@ describe('HeadersInput', () => {
const onChange = vi.fn()
render(<HeadersInput {...defaultProps} headersItems={headersItems} onChange={onChange} />)
- const header2Input = screen.getAllByRole('textbox', {
- name: 'tools.mcp.modal.headerKey',
- })[1]!
+ const header2Input = screen.getByDisplayValue('Header2')
fireEvent.change(header2Input, { target: { value: 'UpdatedHeader2' } })
expect(onChange).toHaveBeenCalledWith([
@@ -186,16 +183,27 @@ describe('HeadersInput', () => {
const onChange = vi.fn()
render(<HeadersInput {...defaultProps} headersItems={headersItems} onChange={onChange} />)
- // Find all delete buttons and click the second one
- const deleteButtons = document.querySelectorAll('[class*="text-text-destructive"]')
- const secondDeleteButton = deleteButtons[1]?.closest('button')
- if (secondDeleteButton) {
- fireEvent.click(secondDeleteButton)
- expect(onChange).toHaveBeenCalledWith([
- { id: '1', key: 'Header1', value: 'Value1' },
- { id: '3', key: 'Header3', value: 'Value3' },
- ])
- }
+ fireEvent.click(screen.getByRole('button', { name: 'common.operation.delete Header2 2' }))
+ expect(onChange).toHaveBeenCalledWith([
+ { id: '1', key: 'Header1', value: 'Value1' },
+ { id: '3', key: 'Header3', value: 'Value3' },
+ ])
+ })
+
+ it('should distinguish delete actions for duplicate header keys', () => {
+ const onChange = vi.fn()
+ const duplicateHeaders = [
+ { id: '1', key: 'X-Header', value: 'Value1' },
+ { id: '2', key: 'X-Header', value: 'Value2' },
+ ]
+ render(<HeadersInput {...defaultProps} headersItems={duplicateHeaders} onChange={onChange} />)
+
+ expect(
+ screen.getByRole('button', { name: 'common.operation.delete X-Header 1' }),
+ ).toBeInTheDocument()
+ fireEvent.click(screen.getByRole('button', { name: 'common.operation.delete X-Header 2' }))
+
+ expect(onChange).toHaveBeenCalledWith([{ id: '1', key: 'X-Header', value: 'Value1' }])
})
})
@@ -225,12 +233,25 @@ describe('HeadersInput', () => {
describe('Edge Cases', () => {
it('should handle empty key and value', () => {
- const headersItems = [{ id: '1', key: '', value: '' }]
+ const headersItems = [
+ { id: '1', key: '', value: '' },
+ { id: '2', key: '', value: '' },
+ ]
render(<HeadersInput {...defaultProps} headersItems={headersItems} />)
- expect(screen.getByRole('textbox', { name: 'tools.mcp.modal.headerKey' })).toBeInTheDocument()
+ expect(screen.getAllByRole('textbox', { name: 'tools.mcp.modal.headerKey' })).toHaveLength(2)
+ expect(screen.getAllByRole('textbox', { name: 'tools.mcp.modal.headerValue' })).toHaveLength(
+ 2,
+ )
+ expect(
+ screen.getByRole('button', {
+ name: 'common.operation.delete tools.mcp.modal.headerKey 1',
+ }),
+ ).toBeInTheDocument()
expect(
- screen.getByRole('textbox', { name: 'tools.mcp.modal.headerValue' }),
+ screen.getByRole('button', {
+ name: 'common.operation.delete tools.mcp.modal.headerKey 2',
+ }),
).toBeInTheDocument()
})
diff --git a/web/app/components/tools/mcp/headers-input.tsx b/web/app/components/tools/mcp/headers-input.tsx
index 32257bc8748..4be7b9c5393 100644
--- a/web/app/components/tools/mcp/headers-input.tsx
+++ b/web/app/components/tools/mcp/headers-input.tsx
@@ -103,8 +103,12 @@ const HeadersInput = ({ headersItems, onChange, readonly = false, isMasked = fal
readOnly={readonly}
/>
{!readonly && !!headersItems.length && (
- <ActionButton onClick={() => handleRemoveItem(index)} className="mr-2">
- <RiDeleteBinLine className="size-4 text-text-destructive" />
+ <ActionButton
+ aria-label={`${t(($) => $['operation.delete'], { ns: 'common' })} ${item.key.trim() || t(($) => $['mcp.modal.headerKey'], { ns: 'tools' })} ${index + 1}`}
+ onClick={() => handleRemoveItem(index)}
+ className="mr-2"
+ >
+ <RiDeleteBinLine aria-hidden className="size-4 text-text-destructive" />
</ActionButton>
)}
</div>
diff --git a/web/app/components/tools/mcp/sections/__tests__/headers-section.spec.tsx b/web/app/components/tools/mcp/sections/__tests__/headers-section.spec.tsx
index 75693845fe0..a2e273c6cde 100644
--- a/web/app/components/tools/mcp/sections/__tests__/headers-section.spec.tsx
+++ b/web/app/components/tools/mcp/sections/__tests__/headers-section.spec.tsx
@@ -113,8 +113,9 @@ describe('HeadersSection', () => {
<HeadersSection {...defaultProps} headers={headers} onHeadersChange={onHeadersChange} />,
)
- // Find and click the delete button
- const deleteButton = screen.getByRole('button', { name: '' })
+ const deleteButton = screen.getByRole('button', {
+ name: 'common.operation.delete X-Header 1',
+ })
fireEvent.click(deleteButton)
expect(onHeadersChange).toHaveBeenCalledWith([])