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
This commit is contained in:
bymyself
2026-01-31 22:47:56 -08:00
parent 544ef5bb70
commit 495afc1a68

View File

@@ -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<DialogInstance[]>([])
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<ReturnType<typeof useDialogStore>> 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<ReturnType<typeof useDialogStore>> 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<ReturnType<typeof useDialogStore>> 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<ReturnType<typeof useDialogStore>> as ReturnType<
typeof useDialogStore
>)
const dialogStore = useDialogStore()
dialogStore.dialogStack.push({ key: 'test-dialog' } as DialogInstance)
const keybindingStore = useKeybindingStore()
keybindingStore.addDefaultKeybinding(