test: add theme command registry tests and minor fixes

This commit is contained in:
lucaronin
2026-03-02 23:05:03 +01:00
parent 7169634dc3
commit f152e0343d
4 changed files with 41 additions and 3 deletions

View File

@@ -51,7 +51,7 @@ export function CommandPalette({ open, commands, onClose }: CommandPaletteProps)
useEffect(() => {
if (open) {
setQuery('') // eslint-disable-line react-hooks/set-state-in-effect -- reset on open
setSelectedIndex(0) // eslint-disable-line react-hooks/set-state-in-effect -- reset on open
setSelectedIndex(0)
setTimeout(() => inputRef.current?.focus(), 50)
}
}, [open])

View File

@@ -209,7 +209,6 @@ function SearchContent({
onMouseEnter={() => onHover(i)}
>
<div className="flex items-center gap-2">
{/* eslint-disable-next-line react-hooks/static-components -- icon from static map lookup */}
<TypeIcon width={14} height={14} className="shrink-0" style={{ color: typeColor ?? 'var(--muted-foreground)' }} />
<span className="min-w-0 flex-1 truncate text-[13px] font-medium text-foreground">{result.title}</span>
{noteType && (

View File

@@ -437,6 +437,46 @@ describe('useCommandRegistry', () => {
const groups = new Set(result.current.map(c => c.group))
expect(groups).toContain('Appearance')
})
it('generates open-theme commands for each theme when onOpenTheme is provided', () => {
const onOpenTheme = vi.fn()
const { result } = renderHook(() => useCommandRegistry(makeConfig({
themes: themeFixtures, activeThemeId: 'default', onOpenTheme,
})))
const openDefault = result.current.find(c => c.id === 'open-theme-default')
const openDark = result.current.find(c => c.id === 'open-theme-dark')
expect(openDefault).toBeDefined()
expect(openDefault!.label).toBe('Edit Default Theme')
expect(openDefault!.group).toBe('Appearance')
expect(openDefault!.enabled).toBe(true)
expect(openDark).toBeDefined()
expect(openDark!.label).toBe('Edit Dark Theme')
})
it('omits open-theme commands when onOpenTheme is not provided', () => {
const { result } = renderHook(() => useCommandRegistry(makeConfig({
themes: themeFixtures, activeThemeId: 'default',
})))
expect(result.current.find(c => c.id === 'open-theme-default')).toBeUndefined()
expect(result.current.find(c => c.id === 'open-theme-dark')).toBeUndefined()
})
it('calls onOpenTheme with correct themeId when open-theme command executes', () => {
const onOpenTheme = vi.fn()
const { result } = renderHook(() => useCommandRegistry(makeConfig({
themes: themeFixtures, activeThemeId: 'default', onOpenTheme,
})))
result.current.find(c => c.id === 'open-theme-dark')!.execute()
expect(onOpenTheme).toHaveBeenCalledWith('dark')
})
it('open-theme command is always enabled regardless of active theme', () => {
const onOpenTheme = vi.fn()
const { result } = renderHook(() => useCommandRegistry(makeConfig({
themes: themeFixtures, activeThemeId: 'default', onOpenTheme,
})))
expect(result.current.find(c => c.id === 'open-theme-default')!.enabled).toBe(true)
})
})
})

View File

@@ -296,7 +296,6 @@ export function useThemeManager(
const entry = entries.find(e => e.path === activeThemeId)
if (!entry || !isEntryRemoved(entry)) return
clearTheme()
// eslint-disable-next-line react-hooks/set-state-in-effect -- sync fallback when active theme is deleted
setActiveThemeId(null)
if (vaultPath) tauriCall('set_active_theme', { vaultPath, themeId: null }).catch(() => {})
}, [entries, activeThemeId, clearTheme, vaultPath, setActiveThemeId])