From 495afc1a68aeda17c70450aa9846dd46276bc320 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sat, 31 Jan 2026 22:47:56 -0800 Subject: [PATCH] refactor: use singleton mock pattern for useDialogStore in escape test - Replace vi.mocked().mockReturnValue() with reactive singleton pattern - Use reactive() array to match Pinia's ref unwrapping behavior - Reset via array mutation instead of mockReturnValue calls Fixes #8467 Amp-Thread-ID: https://ampcode.com/threads/T-019c17e8-d8c4-72a3-ac99-56a9f2346aca --- .../keybindingService.escape.test.ts | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/platform/keybindings/keybindingService.escape.test.ts b/src/platform/keybindings/keybindingService.escape.test.ts index 962c5b3a2..c9021aebd 100644 --- a/src/platform/keybindings/keybindingService.escape.test.ts +++ b/src/platform/keybindings/keybindingService.escape.test.ts @@ -1,4 +1,5 @@ import { createPinia, setActivePinia } from 'pinia' +import { reactive } from 'vue' import { beforeEach, describe, expect, it, vi } from 'vitest' import { CORE_KEYBINDINGS } from '@/platform/keybindings/defaults' @@ -16,11 +17,12 @@ vi.mock('@/platform/settings/settingStore', () => ({ })) })) -vi.mock('@/stores/dialogStore', () => ({ - useDialogStore: vi.fn(() => ({ - dialogStack: [] - })) -})) +vi.mock('@/stores/dialogStore', () => { + const dialogStack = reactive([]) + return { + useDialogStore: () => ({ dialogStack }) + } +}) vi.mock('@/scripts/app', () => ({ app: { @@ -40,11 +42,8 @@ describe('keybindingService - Escape key handling', () => { mockCommandExecute = vi.fn() commandStore.execute = mockCommandExecute - vi.mocked(useDialogStore).mockReturnValue({ - dialogStack: [] as DialogInstance[] - } as Partial> as ReturnType< - typeof useDialogStore - >) + const dialogStore = useDialogStore() + dialogStore.dialogStack.length = 0 keybindingService = useKeybindingService() keybindingService.registerCoreKeybindings() @@ -76,11 +75,8 @@ describe('keybindingService - Escape key handling', () => { } it('should execute Escape keybinding when no dialogs are open', async () => { - vi.mocked(useDialogStore).mockReturnValue({ - dialogStack: [] as DialogInstance[] - } as Partial> as ReturnType< - typeof useDialogStore - >) + const dialogStore = useDialogStore() + dialogStore.dialogStack.length = 0 const event = createKeyboardEvent('Escape') await keybindingService.keybindHandler(event) @@ -89,11 +85,8 @@ describe('keybindingService - Escape key handling', () => { }) it('should NOT execute Escape keybinding when dialogs are open', async () => { - vi.mocked(useDialogStore).mockReturnValue({ - dialogStack: [{ key: 'test-dialog' } as DialogInstance] - } as Partial> as ReturnType< - typeof useDialogStore - >) + const dialogStore = useDialogStore() + dialogStore.dialogStack.push({ key: 'test-dialog' } as DialogInstance) keybindingService = useKeybindingService() @@ -104,11 +97,8 @@ describe('keybindingService - Escape key handling', () => { }) it('should execute Escape keybinding with modifiers regardless of dialog state', async () => { - vi.mocked(useDialogStore).mockReturnValue({ - dialogStack: [{ key: 'test-dialog' } as DialogInstance] - } as Partial> as ReturnType< - typeof useDialogStore - >) + const dialogStore = useDialogStore() + dialogStore.dialogStack.push({ key: 'test-dialog' } as DialogInstance) const keybindingStore = useKeybindingStore() keybindingStore.addDefaultKeybinding(