2222import { action } from './deleteAction'
2323import { expect } from '@jest/globals'
2424import { File , Folder , Permission , View , FileAction } from '@nextcloud/files'
25- import eventBus from '@nextcloud/event-bus '
25+ import * as capabilities from '@nextcloud/capabilities '
2626import axios from '@nextcloud/axios'
27+ import eventBus from '@nextcloud/event-bus'
2728
2829import logger from '../logger'
2930
@@ -111,6 +112,16 @@ describe('Delete action conditions tests', () => {
111112 expect ( action . displayName ( [ file ] , trashbinView ) ) . toBe ( 'Delete permanently' )
112113 } )
113114
115+ test ( 'Trashbin disabled displayName' , ( ) => {
116+ jest . spyOn ( capabilities , 'getCapabilities' ) . mockImplementation ( ( ) => {
117+ return {
118+ files : { } ,
119+ }
120+ } )
121+ expect ( action . displayName ( [ file ] , view ) ) . toBe ( 'Delete permanently' )
122+ expect ( capabilities . getCapabilities ) . toBeCalledTimes ( 1 )
123+ } )
124+
114125 test ( 'Shared root node displayName' , ( ) => {
115126 expect ( action . displayName ( [ file2 ] , view ) ) . toBe ( 'Leave this share' )
116127 expect ( action . displayName ( [ folder2 ] , view ) ) . toBe ( 'Leave this share' )
@@ -181,6 +192,9 @@ describe('Delete action enabled tests', () => {
181192} )
182193
183194describe ( 'Delete action execute tests' , ( ) => {
195+ afterEach ( ( ) => {
196+ jest . restoreAllMocks ( )
197+ } )
184198 test ( 'Delete action' , async ( ) => {
185199 jest . spyOn ( axios , 'delete' )
186200 jest . spyOn ( eventBus , 'emit' )
@@ -235,9 +249,125 @@ describe('Delete action execute tests', () => {
235249 expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 2 , 'files:node:deleted' , file2 )
236250 } )
237251
252+ test ( 'Delete action batch large set' , async ( ) => {
253+ jest . spyOn ( axios , 'delete' )
254+ jest . spyOn ( eventBus , 'emit' )
255+
256+ // Emulate the confirmation dialog to always confirm
257+ const confirmMock = jest . fn ( ) . mockImplementation ( ( a , b , c , resolve ) => resolve ( true ) )
258+ // @ts -expect-error We only mock what needed
259+ window . OC = { dialogs : { confirmDestructive : confirmMock } }
260+
261+ const file1 = new File ( {
262+ id : 1 ,
263+ source : 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt' ,
264+ owner : 'test' ,
265+ mime : 'text/plain' ,
266+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
267+ } )
268+
269+ const file2 = new File ( {
270+ id : 2 ,
271+ source : 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt' ,
272+ owner : 'test' ,
273+ mime : 'text/plain' ,
274+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
275+ } )
276+
277+ const file3 = new File ( {
278+ id : 3 ,
279+ source : 'https://cloud.domain.com/remote.php/dav/files/test/baz.txt' ,
280+ owner : 'test' ,
281+ mime : 'text/plain' ,
282+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
283+ } )
284+
285+ const file4 = new File ( {
286+ id : 4 ,
287+ source : 'https://cloud.domain.com/remote.php/dav/files/test/qux.txt' ,
288+ owner : 'test' ,
289+ mime : 'text/plain' ,
290+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
291+ } )
292+
293+ const file5 = new File ( {
294+ id : 5 ,
295+ source : 'https://cloud.domain.com/remote.php/dav/files/test/quux.txt' ,
296+ owner : 'test' ,
297+ mime : 'text/plain' ,
298+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
299+ } )
300+
301+ const exec = await action . execBatch ! ( [ file1 , file2 , file3 , file4 , file5 ] , view , '/' )
302+
303+ // Enough nodes to trigger a confirmation dialog
304+ expect ( confirmMock ) . toBeCalledTimes ( 1 )
305+
306+ expect ( exec ) . toStrictEqual ( [ true , true , true , true , true ] )
307+ expect ( axios . delete ) . toBeCalledTimes ( 5 )
308+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 1 , 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt' )
309+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 2 , 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt' )
310+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 3 , 'https://cloud.domain.com/remote.php/dav/files/test/baz.txt' )
311+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 4 , 'https://cloud.domain.com/remote.php/dav/files/test/qux.txt' )
312+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 5 , 'https://cloud.domain.com/remote.php/dav/files/test/quux.txt' )
313+
314+ expect ( eventBus . emit ) . toBeCalledTimes ( 5 )
315+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 1 , 'files:node:deleted' , file1 )
316+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 2 , 'files:node:deleted' , file2 )
317+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 3 , 'files:node:deleted' , file3 )
318+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 4 , 'files:node:deleted' , file4 )
319+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 5 , 'files:node:deleted' , file5 )
320+ } )
321+
322+ test ( 'Delete action batch trashbin disabled' , async ( ) => {
323+ jest . spyOn ( axios , 'delete' )
324+ jest . spyOn ( eventBus , 'emit' )
325+ jest . spyOn ( capabilities , 'getCapabilities' ) . mockImplementation ( ( ) => {
326+ return {
327+ files : { } ,
328+ }
329+ } )
330+
331+ // Emulate the confirmation dialog to always confirm
332+ const confirmMock = jest . fn ( ) . mockImplementation ( ( a , b , c , resolve ) => resolve ( true ) )
333+ // @ts -expect-error We only mock what needed
334+ window . OC = { dialogs : { confirmDestructive : confirmMock } }
335+
336+ const file1 = new File ( {
337+ id : 1 ,
338+ source : 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt' ,
339+ owner : 'test' ,
340+ mime : 'text/plain' ,
341+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
342+ } )
343+
344+ const file2 = new File ( {
345+ id : 2 ,
346+ source : 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt' ,
347+ owner : 'test' ,
348+ mime : 'text/plain' ,
349+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
350+ } )
351+
352+ const exec = await action . execBatch ! ( [ file1 , file2 ] , view , '/' )
353+
354+ // Will trigger a confirmation dialog because trashbin app is disabled
355+ expect ( confirmMock ) . toBeCalledTimes ( 1 )
356+
357+ expect ( exec ) . toStrictEqual ( [ true , true ] )
358+ expect ( axios . delete ) . toBeCalledTimes ( 2 )
359+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 1 , 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt' )
360+ expect ( axios . delete ) . toHaveBeenNthCalledWith ( 2 , 'https://cloud.domain.com/remote.php/dav/files/test/bar.txt' )
361+
362+ expect ( eventBus . emit ) . toBeCalledTimes ( 2 )
363+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 1 , 'files:node:deleted' , file1 )
364+ expect ( eventBus . emit ) . toHaveBeenNthCalledWith ( 2 , 'files:node:deleted' , file2 )
365+ } )
366+
238367 test ( 'Delete fails' , async ( ) => {
239368 jest . spyOn ( axios , 'delete' ) . mockImplementation ( ( ) => { throw new Error ( 'Mock error' ) } )
240369 jest . spyOn ( logger , 'error' ) . mockImplementation ( ( ) => jest . fn ( ) )
370+ jest . spyOn ( eventBus , 'emit' )
241371
242372 const file = new File ( {
243373 id : 1 ,
@@ -256,4 +386,36 @@ describe('Delete action execute tests', () => {
256386 expect ( eventBus . emit ) . toBeCalledTimes ( 0 )
257387 expect ( logger . error ) . toBeCalledTimes ( 1 )
258388 } )
389+
390+ test ( 'Delete is cancelled' , async ( ) => {
391+ jest . spyOn ( axios , 'delete' )
392+ jest . spyOn ( eventBus , 'emit' )
393+ jest . spyOn ( capabilities , 'getCapabilities' ) . mockImplementation ( ( ) => {
394+ return {
395+ files : { } ,
396+ }
397+ } )
398+
399+ // Emulate the confirmation dialog to always confirm
400+ const confirmMock = jest . fn ( ) . mockImplementation ( ( a , b , c , resolve ) => resolve ( false ) )
401+ // @ts -expect-error We only mock what needed
402+ window . OC = { dialogs : { confirmDestructive : confirmMock } }
403+
404+ const file1 = new File ( {
405+ id : 1 ,
406+ source : 'https://cloud.domain.com/remote.php/dav/files/test/foo.txt' ,
407+ owner : 'test' ,
408+ mime : 'text/plain' ,
409+ permissions : Permission . READ | Permission . UPDATE | Permission . DELETE ,
410+ } )
411+
412+ const exec = await action . execBatch ! ( [ file1 ] , view , '/' )
413+
414+ expect ( confirmMock ) . toBeCalledTimes ( 1 )
415+
416+ expect ( exec ) . toStrictEqual ( [ null ] )
417+ expect ( axios . delete ) . toBeCalledTimes ( 0 )
418+
419+ expect ( eventBus . emit ) . toBeCalledTimes ( 0 )
420+ } )
259421} )
0 commit comments