mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +00:00
fix(webui): prevent duplicate name in sort/filter selectors and add toolbar quick actions (#523)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { FieldInfo } from '@shumai/dtos'
|
||||
import { FilterPanel } from './filter-panel'
|
||||
|
||||
vi.mock('@/ui/hooks/use-team-id', () => ({
|
||||
useTeamId: () => 'team-1',
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/stores/members', () => ({
|
||||
useMemberStore: () => ({
|
||||
members: [],
|
||||
fetchMembers: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/hooks/use-archive-config', () => ({
|
||||
useArchiveConfig: () => ({
|
||||
archiveStorageEnabled: false,
|
||||
}),
|
||||
}))
|
||||
|
||||
describe('FilterPanel', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('renders "Name" once without duplicating when fields contains system name field', () => {
|
||||
const mockFields: FieldInfo[] = [
|
||||
{
|
||||
id: 'name',
|
||||
scope: 'SYSTEM',
|
||||
readOnly: true,
|
||||
visible: true,
|
||||
description: '',
|
||||
config: {
|
||||
name: 'Name',
|
||||
type: 'text',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
render(
|
||||
<FilterPanel
|
||||
fields={mockFields}
|
||||
conditions={[{ field: 'name', operator: 'eq', value: 'sample' }]}
|
||||
onChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
const selectTrigger = document.querySelector('[data-slot="select-trigger"]')
|
||||
expect(selectTrigger).not.toBeNull()
|
||||
expect(selectTrigger?.textContent).toBe('Name')
|
||||
})
|
||||
})
|
||||
@@ -76,7 +76,14 @@ export function FilterPanel({
|
||||
: f.config?.select?.options || f.config?.selectMulti?.options,
|
||||
config: f.config,
|
||||
}))
|
||||
return [...sys, ...custom].filter((f) => !excludeFields?.includes(f.id))
|
||||
const seen = new Set<string>()
|
||||
return [...sys, ...custom]
|
||||
.filter((f) => !excludeFields?.includes(f.id))
|
||||
.filter((f) => {
|
||||
if (!f.id || seen.has(f.id)) return false
|
||||
seen.add(f.id)
|
||||
return true
|
||||
})
|
||||
}, [fields, memberOptions, excludeFields, archiveStorageEnabled])
|
||||
|
||||
const handleAddCondition = () => {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { FieldInfo } from '@shumai/dtos'
|
||||
import { SortControl } from './sort-control'
|
||||
|
||||
describe('SortControl', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('renders "Name" once in the selector when fields contains system name field', () => {
|
||||
const mockFields: FieldInfo[] = [
|
||||
{
|
||||
id: 'name',
|
||||
scope: 'SYSTEM',
|
||||
readOnly: true,
|
||||
visible: true,
|
||||
description: '',
|
||||
config: {
|
||||
name: 'Name',
|
||||
type: 'text',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'custom_rating',
|
||||
scope: 'PROJECT',
|
||||
readOnly: false,
|
||||
visible: true,
|
||||
description: '',
|
||||
config: {
|
||||
name: 'Rating',
|
||||
type: 'rating',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
render(
|
||||
<SortControl
|
||||
fields={mockFields}
|
||||
sort={{ field: 'name', order: 'asc' }}
|
||||
onSortChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
// Open the sort popover
|
||||
const popoverButton = screen.getByRole('button')
|
||||
fireEvent.click(popoverButton)
|
||||
|
||||
// The select trigger inside the popover must display "Name", not "NameName"
|
||||
const selectTrigger = document.querySelector('[data-slot="select-trigger"]')
|
||||
expect(selectTrigger).not.toBeNull()
|
||||
expect(selectTrigger?.textContent).toBe('Name')
|
||||
})
|
||||
|
||||
it('calls onSortChange when changing sort field', () => {
|
||||
const onSortChange = vi.fn()
|
||||
const mockFields: FieldInfo[] = [
|
||||
{
|
||||
id: 'custom_status',
|
||||
scope: 'PROJECT',
|
||||
readOnly: false,
|
||||
visible: true,
|
||||
description: '',
|
||||
config: {
|
||||
name: 'Status',
|
||||
type: 'select',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
render(
|
||||
<SortControl
|
||||
fields={mockFields}
|
||||
sort={{ field: 'name', order: 'asc' }}
|
||||
onSortChange={onSortChange}
|
||||
/>,
|
||||
)
|
||||
|
||||
const popoverButton = screen.getByRole('button')
|
||||
fireEvent.click(popoverButton)
|
||||
|
||||
// Order toggle button should show "A → Z" when sorted by name asc
|
||||
const orderButton = screen.getByRole('button', { name: /A\s*→\s*Z/i })
|
||||
expect(orderButton).toBeDefined()
|
||||
|
||||
// Toggle direction
|
||||
fireEvent.click(orderButton)
|
||||
expect(onSortChange).toHaveBeenCalledWith({
|
||||
field: 'name',
|
||||
order: 'desc',
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -33,7 +33,12 @@ export function SortControl({ fields, sort, onSortChange, disabled }: SortContro
|
||||
id: f.id!,
|
||||
label: f.config?.name || f.description || m.unknown(),
|
||||
}))
|
||||
return [...sys, ...custom]
|
||||
const seen = new Set<string>()
|
||||
return [...sys, ...custom].filter((f) => {
|
||||
if (!f.id || seen.has(f.id)) return false
|
||||
seen.add(f.id)
|
||||
return true
|
||||
})
|
||||
}, [fields])
|
||||
|
||||
// Default to Custom if no sort selected
|
||||
|
||||
Reference in New Issue
Block a user