Repository navigation
Description
Problem Statement
The SlashCommandDropdown component handles critical slash command selection UI with keyboard navigation and event propagation - completely untested. Nearly identical to NodeAutocomplete in structure but different domain (commands vs nodes).
Untested Code:
- Lines 67-115: Keyboard event handling (slash-command-dropdown.svelte)
- Lines 74, 80, 86, 94:
stopPropagation()calls - Lines 110-115: Event listener lifecycle
- Lines 22-52: Smart positioning logic
Why This Matters
Risks:
- Slash commands (/, /h1-h6, /task) could fail to insert if events don't propagate correctly
- Modal could trap focus or fail to close
- Memory leaks from unremoved listeners
- Positioning bugs causing off-screen modals
Files Involved
Component: src/lib/components/ui/slash-command-dropdown/slash-command-dropdown.svelte
Test File: src/tests/components/slash-command-dropdown.test.ts
Dependencies: Issue #151 (svelte-test-utils.ts)
Acceptance Criteria
Create comprehensive test file with 22+ test cases covering:
Test Categories (Same Pattern as NodeAutocomplete)
-
Rendering (3 tests)
- Renders when visible
- Hides when not visible
- Displays all commands with icons
-
Keyboard Navigation (6 tests)
- ArrowDown/Up navigation
- Boundary conditions (top/bottom)
- Enter selects command
- Escape closes modal
-
Event Propagation (3 tests)
- stopPropagation for all keys
- preventDefault for navigation keys
- No events when not visible
-
Event Dispatching (2 tests)
- 'select' event with SlashCommand
- 'close' event on Escape
-
Lifecycle (2 tests)
- addEventListener on mount
- removeEventListener on unmount with same handler
-
Smart Positioning (3 tests)
- Below cursor when space available
- Above cursor when near bottom
- Viewport edge handling
-
Edge Cases (3 tests)
- Empty commands array
- Query changes reset selection
- Rapid keyboard input
Implementation Example
// src/tests/components/slash-command-dropdown.test.ts
import { describe, it, expect, vi } from 'vitest';
import { screen } from '@testing-library/svelte';
import SlashCommandDropdown from '$lib/components/ui/slash-command-dropdown/slash-command-dropdown.svelte';
import { renderComponent, createUserEvents, waitForEffects } from './svelte-test-utils';
import type { SlashCommand } from '$lib/services/slashCommandService';
describe('SlashCommandDropdown Component', () => {
const mockCommands: SlashCommand[] = [
{ id: 'h1', trigger: '/h1', label: 'Heading 1', nodeType: 'text', content: '', headerLevel: 1 },
{ id: 'h2', trigger: '/h2', label: 'Heading 2', nodeType: 'text', content: '', headerLevel: 2 },
{ id: 'task', trigger: '/task', label: 'Task', nodeType: 'task', content: '' }
];
describe('Rendering', () => {
it('should render visible dropdown with commands', () => {
renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 100, y: 200 }
});
expect(screen.getByRole('listbox')).toBeInTheDocument();
expect(screen.getByLabelText('Slash command palette')).toBeInTheDocument();
expect(screen.getByText('Heading 1')).toBeInTheDocument();
});
it('should not render when visible is false', () => {
renderComponent(SlashCommandDropdown, {
visible: false,
commands: mockCommands,
position: { x: 0, y: 0 }
});
expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
});
});
describe('Keyboard Navigation', () => {
it('should navigate with arrow keys', async () => {
const user = createUserEvents();
renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 0, y: 0 }
});
const items = screen.getAllByRole('option');
expect(items[0]).toHaveAttribute('aria-selected', 'true');
await user.keyboard('{ArrowDown}');
await waitForEffects();
expect(items[1]).toHaveAttribute('aria-selected', 'true');
});
it('should select command with Enter', async () => {
const user = createUserEvents();
const selectHandler = vi.fn();
const { component } = renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 0, y: 0 }
});
component.$on('select', selectHandler);
await user.keyboard('{Enter}');
await waitForEffects();
expect(selectHandler).toHaveBeenCalledWith(
expect.objectContaining({ detail: mockCommands[0] })
);
});
it('should close with Escape', async () => {
const user = createUserEvents();
const closeHandler = vi.fn();
const { component } = renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 0, y: 0 }
});
component.$on('close', closeHandler);
await user.keyboard('{Escape}');
await waitForEffects();
expect(closeHandler).toHaveBeenCalled();
});
});
describe('Event Propagation', () => {
it('should stop propagation to prevent background node creation', () => {
renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 0, y: 0 }
});
const stopPropagationSpy = vi.fn();
const preventDefaultSpy = vi.fn();
const event = new KeyboardEvent('keydown', { key: 'Enter' });
Object.defineProperty(event, 'stopPropagation', { value: stopPropagationSpy });
Object.defineProperty(event, 'preventDefault', { value: preventDefaultSpy });
document.dispatchEvent(event);
// CRITICAL for preventing double-actions
expect(stopPropagationSpy).toHaveBeenCalled();
expect(preventDefaultSpy).toHaveBeenCalled();
});
});
describe('Lifecycle Management', () => {
it('should cleanup event listeners on unmount', () => {
const addEventListenerSpy = vi.spyOn(document, 'addEventListener');
const removeEventListenerSpy = vi.spyOn(document, 'removeEventListener');
const { unmount } = renderComponent(SlashCommandDropdown, {
visible: true,
commands: mockCommands,
position: { x: 0, y: 0 }
});
const addedHandler = addEventListenerSpy.mock.calls[0][1];
unmount();
expect(removeEventListenerSpy).toHaveBeenCalledWith('keydown', addedHandler);
});
});
// Add remaining test categories...
});Test Requirements
- Coverage: >90% line coverage for slash-command-dropdown.svelte
- Execution:
bun run test -- slash-command-dropdown.test.tspasses - No warnings or errors
- All event propagation scenarios verified
Dependencies
Requires: Issue #151 (testing infrastructure)
Similar To: Issue #152 (use same patterns)
Blocks: Issue #4, #5 (integration tests)
Definition of Done
- 22+ test cases implemented
- >90% coverage achieved
- Event propagation verified
- Lifecycle cleanup verified
- Tests pass without errors
- Code review approved
Estimated Effort
2-3 hours (can reuse patterns from NodeAutocomplete tests)
Activity
Add comprehensive event tests for SlashCommandDropdown component (closes
Add comprehensive event tests for SlashCommandDropdown component (#166)
Add comprehensive event tests for SlashCommandDropdown component (#166)
Add comprehensive event tests for SlashCommandDropdown component (#166)
Add comprehensive event tests for SlashCommandDropdown component (#166)