|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { cleanup, fireEvent, render, waitFor } from '@testing-library/react' |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +// The bug under test lives purely in how each cmdk `value` is derived in |
| 6 | +// CommandPalette, so stub the data hook with the two collision shapes plus the |
| 7 | +// surrounding context hooks the surface doesn't need here: |
| 8 | +// - same description, different id, same context — the real |
| 9 | +// move_up_from_cm_start / move_left_from_cm_start CM-nav pair (↑ vs ←). |
| 10 | +// - same id, different context, both active at once — the real global vs vim |
| 11 | +// normal-mode `undo` pair (a bare-id value would still collide on these). |
| 12 | +vi.mock('../useCommandPaletteActions.ts', () => ({ |
| 13 | + useCommandPaletteActions: () => ({ |
| 14 | + actions: [ |
| 15 | + {id: 'move_up_from_cm_start', description: 'Move to previous block', context: 'edit_mode_cm'}, |
| 16 | + {id: 'move_left_from_cm_start', description: 'Move to previous block', context: 'edit_mode_cm'}, |
| 17 | + {id: 'undo', description: 'Undo', context: 'global'}, |
| 18 | + {id: 'undo', description: 'Undo', context: 'normal_mode'}, |
| 19 | + ], |
| 20 | + activeContexts: [ |
| 21 | + {config: {type: 'edit_mode_cm', displayName: 'Editing'}, dependencies: {}}, |
| 22 | + {config: {type: 'global', displayName: 'Global'}, dependencies: {}}, |
| 23 | + {config: {type: 'normal_mode', displayName: 'Normal mode'}, dependencies: {}}, |
| 24 | + ], |
| 25 | + bindingsFor: (action: {id: string}) => |
| 26 | + action.id === 'move_up_from_cm_start' ? [{keys: 'ArrowUp'}] |
| 27 | + : action.id === 'move_left_from_cm_start' ? [{keys: 'ArrowLeft'}] |
| 28 | + : [], |
| 29 | + }), |
| 30 | +})) |
| 31 | +vi.mock('@/shortcuts/useActionContext.js', () => ({useActionContext: () => {}})) |
| 32 | +vi.mock('@/shortcuts/runAction.js', () => ({useRunAction: () => () => {}})) |
| 33 | +vi.mock('@/shortcuts/ActiveContexts.js', () => ({ |
| 34 | + useActiveContextsState: () => new Map(), |
| 35 | + editorViewFromActiveContexts: () => undefined, |
| 36 | +})) |
| 37 | + |
| 38 | +import { CommandPalette } from '../CommandPalette.tsx' |
| 39 | +import { commandPaletteToggle } from '../toggleStore.ts' |
| 40 | + |
| 41 | +// cmdk's CommandList observes its size and scrolls the active item into view — |
| 42 | +// neither API exists in jsdom, so stub them to no-ops. |
| 43 | +globalThis.ResizeObserver ??= class { |
| 44 | + observe() {} |
| 45 | + unobserve() {} |
| 46 | + disconnect() {} |
| 47 | +} as never |
| 48 | +Element.prototype.scrollIntoView ??= () => {} |
| 49 | + |
| 50 | +const items = () => Array.from(document.querySelectorAll<HTMLElement>('[cmdk-item]')) |
| 51 | +const selectedItems = () => |
| 52 | + Array.from(document.querySelectorAll<HTMLElement>('[cmdk-item][aria-selected="true"]')) |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + cleanup() |
| 56 | + commandPaletteToggle.close() |
| 57 | +}) |
| 58 | + |
| 59 | +describe('CommandPalette item identity', () => { |
| 60 | + it('gives every action a distinct value so only one row is ever selected', async () => { |
| 61 | + commandPaletteToggle.open() |
| 62 | + render(<CommandPalette/>) |
| 63 | + |
| 64 | + // Every action renders as a separate item with a unique (context-qualified) value... |
| 65 | + await waitFor(() => expect(items()).toHaveLength(4)) |
| 66 | + const values = items().map(el => el.getAttribute('data-value')) |
| 67 | + expect(new Set(values).size).toBe(4) |
| 68 | + // ...including same-id actions that are live in two contexts at once. |
| 69 | + expect(values).toContain('global:undo') |
| 70 | + expect(values).toContain('normal_mode:undo') |
| 71 | + |
| 72 | + // Exactly ONE row is selected; a shared value would mark several at once. |
| 73 | + await waitFor(() => expect(selectedItems()).toHaveLength(1)) |
| 74 | + const firstSelected = selectedItems()[0].getAttribute('data-value') |
| 75 | + |
| 76 | + // ArrowDown advances to a different single item rather than looping/sticking. |
| 77 | + const input = document.querySelector('[cmdk-input]')! |
| 78 | + fireEvent.keyDown(input, {key: 'ArrowDown'}) |
| 79 | + await waitFor(() => { |
| 80 | + expect(selectedItems()).toHaveLength(1) |
| 81 | + expect(selectedItems()[0].getAttribute('data-value')).not.toBe(firstSelected) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('still filters actions by their human description text', async () => { |
| 86 | + commandPaletteToggle.open() |
| 87 | + render(<CommandPalette/>) |
| 88 | + await waitFor(() => expect(items()).toHaveLength(4)) |
| 89 | + |
| 90 | + const input = document.querySelector('[cmdk-input]')! |
| 91 | + fireEvent.change(input, {target: {value: 'previous block'}}) |
| 92 | + |
| 93 | + // Description text must remain searchable even though the value is now an id. |
| 94 | + await waitFor(() => { |
| 95 | + const shown = items().map(el => el.getAttribute('data-value')) |
| 96 | + expect(shown).toEqual([ |
| 97 | + 'edit_mode_cm:move_up_from_cm_start', |
| 98 | + 'edit_mode_cm:move_left_from_cm_start', |
| 99 | + ]) |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments