From 8fbfa84a544a8dc3fa4f88aa83833a8a4d35f7dc Mon Sep 17 00:00:00 2001
From: Ferdinand Thiessen <opensource@fthiessen.de>
Date: Tue, 11 Mar 2025 18:11:12 +0100
Subject: test: speed up hotkey tests by 2.4s

That is about 2500% as we needed to always wait for the timeout of
500ms. As the event was already handled - compared to now where we only
wait for one tick which is about 20ms.

Also the tests did not work as expected - for shift and meta key they
did not report the failing tests.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
---
 apps/files/src/services/HotKeysService.spec.ts | 49 ++++++++++++--------------
 1 file changed, 23 insertions(+), 26 deletions(-)

(limited to 'apps')

diff --git a/apps/files/src/services/HotKeysService.spec.ts b/apps/files/src/services/HotKeysService.spec.ts
index 3bfdac0bc32..c732c728ce5 100644
--- a/apps/files/src/services/HotKeysService.spec.ts
+++ b/apps/files/src/services/HotKeysService.spec.ts
@@ -2,8 +2,9 @@
  * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  * SPDX-License-Identifier: AGPL-3.0-or-later
  */
-import { describe, it, vi, expect, beforeEach, beforeAll, afterEach } from 'vitest'
 import { File, Permission, View } from '@nextcloud/files'
+import { describe, it, vi, expect, beforeEach, beforeAll, afterEach } from 'vitest'
+import { nextTick } from 'vue'
 import axios from '@nextcloud/axios'
 
 import { getPinia } from '../store/index.ts'
@@ -15,7 +16,6 @@ import { action as renameAction } from '../actions/renameAction.ts'
 import { action as sidebarAction } from '../actions/sidebarAction.ts'
 import { registerHotkeys } from './HotKeysService.ts'
 import { useUserConfigStore } from '../store/userconfig.ts'
-import { subscribe } from '@nextcloud/event-bus'
 
 let file: File
 const view = {
@@ -61,7 +61,6 @@ describe('HotKeysService testing', () => {
 		activeStore.setActiveNode(file)
 
 		window.OCA = { Files: { Sidebar: { open: () => {}, setActiveTab: () => {} } } }
-		// @ts-expect-error We only mock what needed, we do not need Files.Router.goTo or Files.Navigation
 		window.OCP = { Files: { Router: { goToRoute: goToRouteMock, params: {}, query: {} } } }
 
 		initialState = document.createElement('input')
@@ -114,6 +113,7 @@ describe('HotKeysService testing', () => {
 	})
 
 	it('Pressing Delete should delete the file', async () => {
+		// @ts-expect-error mocking private field
 		vi.spyOn(deleteAction._action, 'exec').mockResolvedValue(() => true)
 
 		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete' }))
@@ -142,34 +142,31 @@ describe('HotKeysService testing', () => {
 		vi.spyOn(axios, 'put').mockImplementationOnce(() => Promise.resolve())
 
 		const userConfigStore = useUserConfigStore(getPinia())
-		const currentGridConfig = userConfigStore.userConfig.grid_view
-
-		// Wait for the user config to be updated
-		// or timeout after 500ms
-		const waitForUserConfig = () => new Promise((resolve) => {
-			subscribe('files:config:updated', resolve)
-			setTimeout(resolve, 500)
-		})
+		userConfigStore.userConfig.grid_view = false
+		expect(userConfigStore.userConfig.grid_view).toBe(false)
 
 		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'v', code: 'KeyV' }))
-		await waitForUserConfig()
-		expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
+		await nextTick()
 
-		// Modifier keys should not trigger the action
-		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', ctrlKey: true }))
-		await waitForUserConfig()
-		expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
+		expect(userConfigStore.userConfig.grid_view).toBe(true)
+	})
 
-		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', altKey: true }))
-		await waitForUserConfig()
-		expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
+	it.each([
+		['ctrlKey'],
+		['altKey'],
+		// those meta keys are still triggering...
+		// ['shiftKey'],
+		// ['metaKey']
+	])('Pressing v with modifier key %s should not toggle grid view', async (modifier: string) => {
+		vi.spyOn(axios, 'put').mockImplementationOnce(() => Promise.resolve())
 
-		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', shiftKey: true }))
-		await waitForUserConfig()
-		expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
+		const userConfigStore = useUserConfigStore(getPinia())
+		userConfigStore.userConfig.grid_view = false
+		expect(userConfigStore.userConfig.grid_view).toBe(false)
 
-		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', metaKey: true }))
-		await waitForUserConfig()
-		expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
+		window.dispatchEvent(new KeyboardEvent('keydown', { key: 'v', code: 'KeyV', [modifier]: true }))
+		await nextTick()
+
+		expect(userConfigStore.userConfig.grid_view).toBe(false)
 	})
 })
-- 
cgit v1.2.3