|
| 1 | +/** |
| 2 | + * Behavioral tests for `VolumeBreadcrumb.svelte` focused on the favorite-rename |
| 3 | + * keyboard guard (Fix E): while a favorite is being renamed inline, the dropdown |
| 4 | + * must NOT consume arrow / Home / End / Enter keys, so the textbox keeps them and |
| 5 | + * the panes behind the dropdown stay inert. The cross-pane suppression itself |
| 6 | + * lives in `DualPaneExplorer.routeToVolumeChooser`; here we pin the leaf guard. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 10 | +import { mount, tick, flushSync } from 'svelte' |
| 11 | +import VolumeBreadcrumb from './VolumeBreadcrumb.svelte' |
| 12 | + |
| 13 | +vi.mock('$lib/tauri-commands', () => ({ |
| 14 | + resolvePathVolume: vi.fn(() => Promise.resolve({ volume: { id: 'root', path: '/' } })), |
| 15 | + upgradeToSmbVolume: vi.fn(() => Promise.resolve({ status: 'success' })), |
| 16 | + ejectVolume: vi.fn(() => Promise.resolve()), |
| 17 | + getIpcErrorMessage: (e: unknown) => String(e), |
| 18 | + getVolumeSpace: vi.fn(() => Promise.resolve(null)), |
| 19 | + systemHasSavedSmbPassword: vi.fn(() => Promise.resolve(false)), |
| 20 | + upgradeToSmbVolumeUsingSavedPassword: vi.fn(() => Promise.resolve({ status: 'success' })), |
| 21 | + removeFavorite: vi.fn(() => Promise.resolve()), |
| 22 | + renameFavorite: vi.fn(() => Promise.resolve()), |
| 23 | + reorderFavorites: vi.fn(() => Promise.resolve()), |
| 24 | + stripFavoritePrefix: (id: string) => (id.startsWith('fav-') ? id.slice(4) : id), |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('$lib/stores/volume-store.svelte', () => ({ |
| 28 | + getVolumes: () => [ |
| 29 | + { id: 'root', name: 'Macintosh HD', path: '/', category: 'main_volume', isEjectable: false }, |
| 30 | + { id: 'fav-1', name: 'Documents', path: '/Users/test/Documents', category: 'favorite', isEjectable: false }, |
| 31 | + ], |
| 32 | + getVolumesTimedOut: () => false, |
| 33 | + isVolumesRefreshing: () => false, |
| 34 | + isVolumeRetryFailed: () => false, |
| 35 | + requestVolumeRefresh: vi.fn(), |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('$lib/stores/volume-busy-store.svelte', () => ({ isVolumeBusy: () => false })) |
| 39 | + |
| 40 | +vi.mock('$lib/ui/toast', () => ({ addToast: vi.fn(() => 'toast-id'), dismissToast: vi.fn() })) |
| 41 | + |
| 42 | +vi.mock('$lib/settings/reactive-settings.svelte', () => ({ |
| 43 | + formatFileSize: (n: number) => `${String(n)} B`, |
| 44 | + getFileSizeFormat: () => 'binary', |
| 45 | + getFileSizeUnit: () => 'bytes', |
| 46 | + getNetworkEnabled: () => true, |
| 47 | + getUseAppIconsForDocuments: () => false, |
| 48 | +})) |
| 49 | + |
| 50 | +vi.mock('$lib/icon-cache', async () => { |
| 51 | + const { writable } = await import('svelte/store') |
| 52 | + return { |
| 53 | + getCachedIcon: vi.fn().mockReturnValue('/icons/dir.png'), |
| 54 | + iconCacheVersion: writable(0), |
| 55 | + prefetchIcons: vi.fn().mockResolvedValue(undefined), |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +interface BreadcrumbInstance { |
| 60 | + open: () => void |
| 61 | + getIsOpen: () => boolean |
| 62 | + handleKeyDown: (e: KeyboardEvent) => boolean |
| 63 | +} |
| 64 | + |
| 65 | +function mountBreadcrumb(): { instance: BreadcrumbInstance; target: HTMLDivElement } { |
| 66 | + const target = document.createElement('div') |
| 67 | + document.body.appendChild(target) |
| 68 | + const instance = mount(VolumeBreadcrumb, { |
| 69 | + target, |
| 70 | + props: { volumeId: 'root', currentPath: '/Users/test' }, |
| 71 | + }) as unknown as BreadcrumbInstance |
| 72 | + return { instance, target } |
| 73 | +} |
| 74 | + |
| 75 | +describe('VolumeBreadcrumb favorite-rename keyboard guard', () => { |
| 76 | + beforeEach(() => { |
| 77 | + document.body.innerHTML = '' |
| 78 | + }) |
| 79 | + |
| 80 | + it('handleKeyDown returns false when the dropdown is closed', () => { |
| 81 | + const { instance } = mountBreadcrumb() |
| 82 | + expect(instance.handleKeyDown(new KeyboardEvent('keydown', { key: 'ArrowDown' }))).toBe(false) |
| 83 | + }) |
| 84 | + |
| 85 | + it('consumes ArrowDown when the dropdown is open and not renaming', async () => { |
| 86 | + const { instance } = mountBreadcrumb() |
| 87 | + instance.open() |
| 88 | + await tick() |
| 89 | + flushSync() |
| 90 | + expect(instance.getIsOpen()).toBe(true) |
| 91 | + expect(instance.handleKeyDown(new KeyboardEvent('keydown', { key: 'ArrowDown' }))).toBe(true) |
| 92 | + }) |
| 93 | + |
| 94 | + it('does NOT consume ArrowDown / Home / End while a favorite rename is active', async () => { |
| 95 | + const { instance, target } = mountBreadcrumb() |
| 96 | + instance.open() |
| 97 | + await tick() |
| 98 | + flushSync() |
| 99 | + |
| 100 | + // Start the inline rename: right-click the favorite row, then click "Rename". |
| 101 | + const favRow = target.querySelector('.favorite-item') as HTMLElement |
| 102 | + expect(favRow).toBeTruthy() |
| 103 | + favRow.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true })) |
| 104 | + await tick() |
| 105 | + flushSync() |
| 106 | + const renameItem = [...target.querySelectorAll('.row-menu-item')].find( |
| 107 | + (el) => el.textContent.trim() === 'Rename', |
| 108 | + ) as HTMLElement |
| 109 | + expect(renameItem).toBeTruthy() |
| 110 | + renameItem.click() |
| 111 | + await tick() |
| 112 | + flushSync() |
| 113 | + |
| 114 | + expect(target.querySelector('.favorite-rename-input')).toBeTruthy() |
| 115 | + |
| 116 | + // The guard: keys the dropdown would otherwise eat must fall through (false) |
| 117 | + // so the rename textbox keeps them. |
| 118 | + for (const key of ['ArrowDown', 'ArrowUp', 'Home', 'End']) { |
| 119 | + expect(instance.handleKeyDown(new KeyboardEvent('keydown', { key }))).toBe(false) |
| 120 | + } |
| 121 | + }) |
| 122 | +}) |
0 commit comments