55import { action } from './editLocallyAction'
66import { expect } from '@jest/globals'
77import { File , Permission , View , FileAction } from '@nextcloud/files'
8- import * as ncDialogs from '@nextcloud/dialogs'
8+ import { DialogBuilder , showError } from '@nextcloud/dialogs'
99import axios from '@nextcloud/axios'
1010
11+ const dialogBuilder = {
12+ setName : jest . fn ( ) . mockReturnThis ( ) ,
13+ setText : jest . fn ( ) . mockReturnThis ( ) ,
14+ setButtons : jest . fn ( ) . mockReturnThis ( ) ,
15+ build : jest . fn ( ) . mockReturnValue ( {
16+ show : jest . fn ( ) . mockResolvedValue ( true ) ,
17+ } ) ,
18+ } as unknown as DialogBuilder
19+
20+ jest . mock ( '@nextcloud/dialogs' , ( ) => ( {
21+ DialogBuilder : jest . fn ( ( ) => dialogBuilder ) ,
22+ showError : jest . fn ( ) ,
23+ } ) )
24+
1125const view = {
1226 id : 'files' ,
1327 name : 'Files' ,
@@ -16,7 +30,8 @@ const view = {
1630// Mock webroot variable
1731beforeAll ( ( ) => {
1832 // eslint-disable-next-line @typescript-eslint/no-explicit-any
19- ( window as any ) . _oc_webroot = ''
33+ ( window as any ) . _oc_webroot = '' ;
34+ ( window as any ) . OCA = { Viewer : { open : jest . fn ( ) } }
2035} )
2136
2237describe ( 'Edit locally action conditions tests' , ( ) => {
@@ -44,7 +59,7 @@ describe('Edit locally action enabled tests', () => {
4459 expect ( action . enabled ! ( [ file ] , view ) ) . toBe ( true )
4560 } )
4661
47- test ( 'Disabled for non-dav ressources ' , ( ) => {
62+ test ( 'Disabled for non-dav resources ' , ( ) => {
4863 const file = new File ( {
4964 id : 1 ,
5065 source : 'https://domain.com/data/foobar.txt' ,
@@ -76,7 +91,7 @@ describe('Edit locally action enabled tests', () => {
7691 expect ( action . enabled ! ( [ file1 , file2 ] , view ) ) . toBe ( false )
7792 } )
7893
79- test ( 'Disabled for files ' , ( ) => {
94+ test ( 'Disabled for directories ' , ( ) => {
8095 const file = new File ( {
8196 id : 1 ,
8297 source : 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/' ,
@@ -104,8 +119,11 @@ describe('Edit locally action enabled tests', () => {
104119
105120describe ( 'Edit locally action execute tests' , ( ) => {
106121 test ( 'Edit locally opens proper URL' , async ( ) => {
107- jest . spyOn ( axios , 'post' ) . mockImplementation ( async ( ) => ( { data : { ocs : { data : { token : 'foobar' } } } } ) )
108- jest . spyOn ( ncDialogs , 'showError' )
122+ jest . spyOn ( axios , 'post' ) . mockImplementation ( async ( ) => ( {
123+ data : { ocs : { data : { token : 'foobar' } } }
124+ } ) )
125+ const mockedShowError = jest . mocked ( showError )
126+ const spyDialogBuilder = jest . spyOn ( dialogBuilder , 'build' )
109127
110128 const file = new File ( {
111129 id : 1 ,
@@ -117,17 +135,20 @@ describe('Edit locally action execute tests', () => {
117135
118136 const exec = await action . exec ( file , view , '/' )
119137
138+ expect ( spyDialogBuilder ) . toBeCalled ( )
139+
120140 // Silent action
121141 expect ( exec ) . toBe ( null )
122142 expect ( axios . post ) . toBeCalledTimes ( 1 )
123143 expect ( axios . post ) . toBeCalledWith ( 'http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json' , { path : '/foobar.txt' } )
124- expect ( ncDialogs . showError ) . toBeCalledTimes ( 0 )
144+ expect ( mockedShowError ) . toBeCalledTimes ( 0 )
125145 expect ( window . location . href ) . toBe ( 'nc://open/test@localhost/foobar.txt?token=foobar' )
126146 } )
127147
128- test ( 'Edit locally fails and show error' , async ( ) => {
148+ test ( 'Edit locally fails and shows error' , async ( ) => {
129149 jest . spyOn ( axios , 'post' ) . mockImplementation ( async ( ) => ( { } ) )
130- jest . spyOn ( ncDialogs , 'showError' )
150+ const mockedShowError = jest . mocked ( showError )
151+ const spyDialogBuilder = jest . spyOn ( dialogBuilder , 'build' )
131152
132153 const file = new File ( {
133154 id : 1 ,
@@ -139,12 +160,14 @@ describe('Edit locally action execute tests', () => {
139160
140161 const exec = await action . exec ( file , view , '/' )
141162
163+ expect ( spyDialogBuilder ) . toBeCalled ( )
164+
142165 // Silent action
143166 expect ( exec ) . toBe ( null )
144167 expect ( axios . post ) . toBeCalledTimes ( 1 )
145168 expect ( axios . post ) . toBeCalledWith ( 'http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json' , { path : '/foobar.txt' } )
146- expect ( ncDialogs . showError ) . toBeCalledTimes ( 1 )
147- expect ( ncDialogs . showError ) . toBeCalledWith ( 'Failed to redirect to client' )
169+ expect ( mockedShowError ) . toBeCalledTimes ( 1 ) // Use showError directly
170+ expect ( mockedShowError ) . toBeCalledWith ( 'Failed to redirect to client' ) // Use showError directly
148171 expect ( window . location . href ) . toBe ( 'http://localhost/' )
149172 } )
150173} )
0 commit comments