33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
55
6- import { describe , it , expect , vi , beforeEach } from 'vitest'
6+ import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest'
77
88import FilesMindMap from '../mindmap.js'
99import { showMessage as showToast } from '@nextcloud/dialogs'
@@ -21,6 +21,7 @@ vi.mock('@nextcloud/router', () => ({
2121
2222vi . mock ( '@nextcloud/dialogs' , ( ) => ( {
2323 showMessage : vi . fn ( ( ) => ( { hideToast : vi . fn ( ) } ) ) ,
24+ prompt : vi . fn ( ) ,
2425} ) )
2526
2627vi . mock ( '@nextcloud/auth' , ( ) => ( {
@@ -450,4 +451,83 @@ describe('FilesMindMap', () => {
450451 expect ( FilesMindMap . _file . supportedWrite ) . toBe ( false )
451452 } )
452453 } )
454+
455+ // ─── registerNewFileMenuPlugin handler ────────────────────────────────────
456+
457+ describe ( 'registerNewFileMenuPlugin handler' , ( ) => {
458+ let handler
459+ let context
460+ let prompt
461+
462+ beforeEach ( async ( ) => {
463+ ; ( { prompt } = await import ( '@nextcloud/dialogs' ) )
464+
465+ const { addNewFileMenuEntry : legacyAdd } = await import ( '@nextcloud/files-legacy' )
466+ const { emit } = await import ( '@nextcloud/event-bus' )
467+
468+ FilesMindMap . registerNewFileMenuPlugin ( )
469+ handler = legacyAdd . mock . calls [ legacyAdd . mock . calls . length - 1 ] [ 0 ] . handler
470+
471+ context = {
472+ source : 'https://nc.example/remote.php/dav/files/testuser/docs' ,
473+ encodedSource : 'https://nc.example/remote.php/dav/files/testuser/docs' ,
474+ permissions : 4 ,
475+ root : '/files/testuser' ,
476+ }
477+
478+ axios . mockResolvedValue ( {
479+ headers : { 'oc-fileid' : '42' } ,
480+ } )
481+
482+ global . OCA = { Viewer : { openWith : vi . fn ( ) } }
483+ } )
484+
485+ afterEach ( ( ) => {
486+ delete global . OCA
487+ } )
488+
489+ it ( 'creates the file with the user-supplied name' , async ( ) => {
490+ prompt . mockResolvedValue ( 'My map.km' )
491+
492+ await handler ( context , [ ] )
493+
494+ expect ( axios ) . toHaveBeenCalledWith ( expect . objectContaining ( {
495+ method : 'PUT' ,
496+ url : expect . stringContaining ( 'My%20map.km' ) ,
497+ } ) )
498+ } )
499+
500+ it ( 'does not create the file when the dialog is cancelled' , async ( ) => {
501+ prompt . mockResolvedValue ( null )
502+
503+ await handler ( context , [ ] )
504+
505+ expect ( axios ) . not . toHaveBeenCalled ( )
506+ } )
507+
508+ it ( 'falls back to the default name when the user submits an empty string' , async ( ) => {
509+ prompt . mockResolvedValue ( '' )
510+
511+ await handler ( context , [ ] )
512+
513+ expect ( axios ) . toHaveBeenCalledWith ( expect . objectContaining ( {
514+ method : 'PUT' ,
515+ url : expect . stringContaining ( 'New%20mind%20map.km' ) ,
516+ } ) )
517+ } )
518+
519+ it ( 'appends a suffix via getUniqueName when the name already exists' , async ( ) => {
520+ const { getUniqueName } = await import ( '@nextcloud/files' )
521+ getUniqueName . mockReturnValueOnce ( 'My map (2).km' )
522+ prompt . mockResolvedValue ( 'My map.km' )
523+
524+ const content = [ { basename : 'My map.km' } ]
525+ await handler ( context , content )
526+
527+ expect ( getUniqueName ) . toHaveBeenCalledWith ( 'My map.km' , [ 'My map.km' ] )
528+ expect ( axios ) . toHaveBeenCalledWith ( expect . objectContaining ( {
529+ url : expect . stringContaining ( 'My%20map%20(2).km' ) ,
530+ } ) )
531+ } )
532+ } )
453533} )
0 commit comments