Skip to content

Commit 8e7d517

Browse files
committed
test: adjust test code for updated @nextcloud/vue version
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent b67712b commit 8e7d517

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

apps/files/src/services/HotKeysService.spec.ts

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ describe('HotKeysService testing', () => {
6161
activeStore.setActiveNode(file)
6262

6363
window.OCA = { Files: { Sidebar: { open: () => {}, setActiveTab: () => {} } } }
64-
// @ts-expect-error We only mock what needed, we do not need Files.Router.goTo or Files.Navigation
6564
window.OCP = { Files: { Router: { goToRoute: goToRouteMock, params: {}, query: {} } } }
6665

6766
initialState = document.createElement('input')
@@ -74,55 +73,56 @@ describe('HotKeysService testing', () => {
7473
})
7574

7675
it('Pressing d should open the sidebar once', () => {
77-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', code: 'KeyD' }))
76+
dispatchEvent('d', 'KeyD')
7877

7978
// Modifier keys should not trigger the action
80-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', code: 'KeyD', ctrlKey: true }))
81-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', code: 'KeyD', altKey: true }))
82-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', code: 'KeyD', shiftKey: true }))
83-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', code: 'KeyD', metaKey: true }))
79+
dispatchEvent('d', 'KeyD', { ctrlKey: true })
80+
dispatchEvent('d', 'KeyD', { altKey: true })
81+
dispatchEvent('d', 'KeyD', { shiftKey: true })
82+
dispatchEvent('d', 'KeyD', { metaKey: true })
8483

8584
expect(sidebarAction.enabled).toHaveReturnedWith(true)
8685
expect(sidebarAction.exec).toHaveBeenCalledOnce()
8786
})
8887

8988
it('Pressing F2 should rename the file', () => {
90-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'F2', code: 'F2' }))
89+
dispatchEvent('F2', 'F2')
9190

9291
// Modifier keys should not trigger the action
93-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'F2', code: 'F2', ctrlKey: true }))
94-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'F2', code: 'F2', altKey: true }))
95-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'F2', code: 'F2', shiftKey: true }))
96-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'F2', code: 'F2', metaKey: true }))
92+
dispatchEvent('F2', 'F2', { ctrlKey: true })
93+
dispatchEvent('F2', 'F2', { altKey: true })
94+
dispatchEvent('F2', 'F2', { shiftKey: true })
95+
dispatchEvent('F2', 'F2', { metaKey: true })
9796

9897
expect(renameAction.enabled).toHaveReturnedWith(true)
9998
expect(renameAction.exec).toHaveBeenCalledOnce()
10099
})
101100

102101
it('Pressing s should toggle favorite', () => {
103102
vi.spyOn(axios, 'post').mockImplementationOnce(() => Promise.resolve())
104-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 's', code: 'KeyS' }))
103+
dispatchEvent('s', 'KeyS')
105104

106105
// Modifier keys should not trigger the action
107-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 's', code: 'KeyS', ctrlKey: true }))
108-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 's', code: 'KeyS', altKey: true }))
109-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 's', code: 'KeyS', shiftKey: true }))
110-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 's', code: 'KeyS', metaKey: true }))
106+
dispatchEvent('s', 'KeyS', { ctrlKey: true })
107+
dispatchEvent('s', 'KeyS', { altKey: true })
108+
dispatchEvent('s', 'KeyS', { shiftKey: true })
109+
dispatchEvent('s', 'KeyS', { metaKey: true })
111110

112111
expect(favoriteAction.enabled).toHaveReturnedWith(true)
113112
expect(favoriteAction.exec).toHaveBeenCalledOnce()
114113
})
115114

116115
it('Pressing Delete should delete the file', async () => {
116+
// @ts-expect-error Mocking private access
117117
vi.spyOn(deleteAction._action, 'exec').mockResolvedValue(() => true)
118118

119-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete' }))
119+
dispatchEvent('Delete', 'Delete')
120120

121121
// Modifier keys should not trigger the action
122-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', ctrlKey: true }))
123-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', altKey: true }))
124-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', shiftKey: true }))
125-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', metaKey: true }))
122+
dispatchEvent('Delete', 'Delete', { ctrlKey: true })
123+
dispatchEvent('Delete', 'Delete', { altKey: true })
124+
dispatchEvent('Delete', 'Delete', { shiftKey: true })
125+
dispatchEvent('Delete', 'Delete', { metaKey: true })
126126

127127
expect(deleteAction.enabled).toHaveReturnedWith(true)
128128
expect(deleteAction.exec).toHaveBeenCalledOnce()
@@ -132,7 +132,7 @@ describe('HotKeysService testing', () => {
132132
expect(goToRouteMock).toHaveBeenCalledTimes(0)
133133
window.OCP.Files.Router.query = { dir: '/foo/bar' }
134134

135-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowUp', code: 'ArrowUp', altKey: true }))
135+
dispatchEvent('ArrowUp', 'ArrowUp', { altKey: true })
136136

137137
expect(goToRouteMock).toHaveBeenCalledOnce()
138138
expect(goToRouteMock.mock.calls[0][2].dir).toBe('/foo')
@@ -151,25 +151,34 @@ describe('HotKeysService testing', () => {
151151
setTimeout(resolve, 500)
152152
})
153153

154-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'v', code: 'KeyV' }))
154+
dispatchEvent('v', 'KeyV')
155155
await waitForUserConfig()
156156
expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
157157

158158
// Modifier keys should not trigger the action
159-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', ctrlKey: true }))
159+
dispatchEvent('Delete', 'Delete', { ctrlKey: true })
160160
await waitForUserConfig()
161161
expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
162162

163-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', altKey: true }))
163+
dispatchEvent('Delete', 'Delete', { altKey: true })
164164
await waitForUserConfig()
165165
expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
166166

167-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', shiftKey: true }))
167+
dispatchEvent('Delete', 'Delete', { shiftKey: true })
168168
await waitForUserConfig()
169169
expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
170170

171-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Delete', code: 'Delete', metaKey: true }))
171+
dispatchEvent('Delete', 'Delete', { metaKey: true })
172172
await waitForUserConfig()
173173
expect(userConfigStore.userConfig.grid_view).toBe(!currentGridConfig)
174174
})
175175
})
176+
177+
/**
178+
* @param key - The key to be pressed
179+
* @param code - The code of the pressed key
180+
* @param options - Optional meta keys
181+
*/
182+
function dispatchEvent(key: string, code: string, options?: KeyboardEventInit) {
183+
document.body.dispatchEvent(new KeyboardEvent('keydown', { ...options, key, code, bubbles: true }))
184+
}

apps/files/src/services/HotKeysService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55
import { dirname } from 'path'
6-
import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js'
6+
import { useHotKey } from '@nextcloud/vue/composables/useHotKey'
77

88
import { action as deleteAction } from '../actions/deleteAction.ts'
99
import { action as favoriteAction } from '../actions/favoriteAction.ts'

apps/systemtags/src/services/HotKeysService.spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ describe('HotKeysService testing', () => {
4242
})
4343

4444
it('Pressing t should open the tag management dialog', () => {
45-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 't', code: 'KeyT' }))
45+
dispatchEvent('t')
4646

4747
// Modifier keys should not trigger the action
48-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 't', code: 'KeyT', ctrlKey: true }))
49-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 't', code: 'KeyT', altKey: true }))
50-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 't', code: 'KeyT', shiftKey: true }))
51-
window.dispatchEvent(new KeyboardEvent('keydown', { key: 't', code: 'KeyT', metaKey: true }))
48+
dispatchEvent('t', { ctrlKey: true })
49+
dispatchEvent('t', { altKey: true })
50+
dispatchEvent('t', { shiftKey: true })
51+
dispatchEvent('t', { metaKey: true })
5252

5353
expect(bulkSystemTagsAction.enabled).toHaveReturnedWith(true)
5454
expect(bulkSystemTagsAction.exec).toHaveBeenCalledOnce()
5555
})
5656
})
57+
58+
/**
59+
* @param key - The key to be pressed
60+
* @param options - Optional meta keys
61+
*/
62+
function dispatchEvent(key: string, options?: KeyboardEventInit) {
63+
document.body.dispatchEvent(new KeyboardEvent('keydown', { ...options, key, code: 'Key' + key.toUpperCase(), bubbles: true }))
64+
}

apps/systemtags/src/services/HotKeysService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5-
import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js'
5+
import { useHotKey } from '@nextcloud/vue/composables/useHotKey'
66

77
import { action as manageTagAction } from '../files_actions/bulkSystemTagsAction.ts'
88
import { executeAction } from '../../../files/src/utils/actionUtils.ts'
@@ -18,6 +18,7 @@ export const registerHotkeys = function() {
1818
useHotKey('t', () => executeAction(manageTagAction), {
1919
stop: true,
2020
prevent: true,
21+
window,
2122
})
2223

2324
logger.debug('Hotkeys registered')

vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export default defineConfig({
99
plugins: [vue()],
1010
test: {
1111
include: ['{apps,core}/**/*.{test,spec}.?(c|m)[jt]s?(x)'],
12+
env: {
13+
LANG: 'en-US',
14+
TZ: 'UTC',
15+
},
1216
environment: 'jsdom',
1317
environmentOptions: {
1418
jsdom: {

0 commit comments

Comments
 (0)