44 */
55
66import type { View } from '@nextcloud/files'
7- import type { Mock } from 'vitest'
87import type { Location } from 'vue-router'
98
109import axios from '@nextcloud/axios'
11- import { File , Folder , Permission } from '@nextcloud/files'
10+ import { File , Folder , Permission , registerFileAction } from '@nextcloud/files'
1211import { enableAutoDestroy , mount } from '@vue/test-utils'
13- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
12+ import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest'
1413import { defineComponent , nextTick } from 'vue'
1514import { action as deleteAction } from '../actions/deleteAction.ts'
16- import { action as favoriteAction } from '../actions/favoriteAction.ts'
17- import { action as renameAction } from '../actions/renameAction.ts'
18- import { action as sidebarAction } from '../actions/sidebarAction.ts'
1915import { useActiveStore } from '../store/active.ts'
2016import { useFilesStore } from '../store/files.ts'
2117import { getPinia } from '../store/index.ts'
@@ -63,10 +59,23 @@ const TestComponent = defineComponent({
6359 template : '<div />' ,
6460} )
6561
62+ beforeAll ( ( ) => {
63+ // @ts -expect-error mocking for tests
64+ window . OCP ??= { }
65+ // @ts -expect-error mocking for tests
66+ window . OCP . Files ??= { }
67+ // @ts -expect-error mocking for tests
68+ window . OCP . Files . Router ??= {
69+ ...router ,
70+ goToRoute : vi . fn ( ) ,
71+ }
72+ } )
73+
6674describe ( 'HotKeysService testing' , ( ) => {
6775 const activeStore = useActiveStore ( getPinia ( ) )
6876
6977 let initialState : HTMLInputElement
78+ let component : ReturnType < typeof mount >
7079
7180 enableAutoDestroy ( afterEach )
7281
@@ -114,54 +123,15 @@ describe('HotKeysService testing', () => {
114123 } ) ) )
115124 document . body . appendChild ( initialState )
116125
117- mount ( TestComponent )
118- } )
119-
120- it ( 'Pressing d should open the sidebar once' , ( ) => {
121- dispatchEvent ( { key : 'd' , code : 'KeyD' } )
122-
123- // Modifier keys should not trigger the action
124- dispatchEvent ( { key : 'd' , code : 'KeyD' , ctrlKey : true } )
125- dispatchEvent ( { key : 'd' , code : 'KeyD' , altKey : true } )
126- dispatchEvent ( { key : 'd' , code : 'KeyD' , shiftKey : true } )
127- dispatchEvent ( { key : 'd' , code : 'KeyD' , metaKey : true } )
128-
129- expect ( sidebarAction . enabled ) . toHaveReturnedWith ( true )
130- expect ( sidebarAction . exec ) . toHaveBeenCalledOnce ( )
131- } )
132-
133- it ( 'Pressing F2 should rename the file' , ( ) => {
134- dispatchEvent ( { key : 'F2' , code : 'F2' } )
135-
136- // Modifier keys should not trigger the action
137- dispatchEvent ( { key : 'F2' , code : 'F2' , ctrlKey : true } )
138- dispatchEvent ( { key : 'F2' , code : 'F2' , altKey : true } )
139- dispatchEvent ( { key : 'F2' , code : 'F2' , shiftKey : true } )
140- dispatchEvent ( { key : 'F2' , code : 'F2' , metaKey : true } )
141-
142- expect ( renameAction . enabled ) . toHaveReturnedWith ( true )
143- expect ( renameAction . exec ) . toHaveBeenCalledOnce ( )
126+ component = mount ( TestComponent )
144127 } )
145128
146- it ( 'Pressing s should toggle favorite' , ( ) => {
147- ( favoriteAction . enabled as Mock ) . mockReturnValue ( true ) ;
148- ( favoriteAction . exec as Mock ) . mockImplementationOnce ( ( ) => Promise . resolve ( null ) )
129+ // tests for register action handling
149130
150- vi . spyOn ( axios , 'post' ) . mockImplementationOnce ( ( ) => Promise . resolve ( ) )
151- dispatchEvent ( { key : 's' , code : 'KeyS' } )
152-
153- // Modifier keys should not trigger the action
154- dispatchEvent ( { key : 's' , code : 'KeyS' , ctrlKey : true } )
155- dispatchEvent ( { key : 's' , code : 'KeyS' , altKey : true } )
156- dispatchEvent ( { key : 's' , code : 'KeyS' , shiftKey : true } )
157- dispatchEvent ( { key : 's' , code : 'KeyS' , metaKey : true } )
158-
159- expect ( favoriteAction . exec ) . toHaveBeenCalledOnce ( )
160- } )
161-
162- it ( 'Pressing Delete should delete the file' , async ( ) => {
163- // @ts -expect-error unit testing - private method access
164- vi . spyOn ( deleteAction . _action , 'exec' ) . mockResolvedValue ( ( ) => true )
131+ it ( 'registeres actions' , ( ) => {
132+ component . destroy ( )
133+ registerFileAction ( deleteAction )
134+ component = mount ( TestComponent )
165135
166136 dispatchEvent ( { key : 'Delete' , code : 'Delete' } )
167137
@@ -175,6 +145,8 @@ describe('HotKeysService testing', () => {
175145 expect ( deleteAction . exec ) . toHaveBeenCalledOnce ( )
176146 } )
177147
148+ // actions implemented by the composable
149+
178150 it ( 'Pressing alt+up should go to parent directory' , ( ) => {
179151 expect ( router . push ) . toHaveBeenCalledTimes ( 0 )
180152 dispatchEvent ( { key : 'ArrowUp' , code : 'ArrowUp' , altKey : true } )
@@ -197,9 +169,8 @@ describe('HotKeysService testing', () => {
197169 it . each ( [
198170 [ 'ctrlKey' ] ,
199171 [ 'altKey' ] ,
200- // those meta keys are still triggering...
201- // ['shiftKey'],
202- // ['metaKey']
172+ [ 'shiftKey' ] ,
173+ [ 'metaKey' ] ,
203174 ] ) ( 'Pressing v with modifier key %s should not toggle grid view' , async ( modifier : string ) => {
204175 vi . spyOn ( axios , 'put' ) . mockImplementationOnce ( ( ) => Promise . resolve ( ) )
205176
0 commit comments