33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
55
6- import type { FileActionData } from '../../lib/actions/index.ts'
6+ import type { IFileAction } from '../../lib/actions/index.ts'
77import type { View } from '../../lib/navigation/view.ts'
88import type { Folder , Node } from '../../lib/node/index.ts'
99
1010import { beforeEach , describe , expect , test , vi } from 'vitest'
11- import { DefaultType , FileAction , getFileActions , registerFileAction } from '../../lib/actions/index.ts'
11+ import { DefaultType , getFileActions , registerFileAction } from '../../lib/actions/index.ts'
1212import logger from '../../lib/utils/logger.ts'
1313
1414const folder = { } as Folder
@@ -19,74 +19,69 @@ describe('FileActions init', () => {
1919 } )
2020
2121 test ( 'Getting empty uninitialized FileActions' , ( ) => {
22- logger . debug = vi . fn ( )
2322 const fileActions = getFileActions ( )
24- expect ( window . _nc_fileactions ) . toBeDefined ( )
23+ expect ( Array . isArray ( fileActions ) ) . toBeTruthy ( )
2524 expect ( fileActions ) . toHaveLength ( 0 )
26- expect ( logger . debug ) . toHaveBeenCalledTimes ( 1 )
2725 } )
2826
2927 test ( 'Initializing FileActions' , ( ) => {
30- logger . debug = vi . fn ( )
31- const action = new FileAction ( {
28+ const action : IFileAction = {
3229 id : 'test' ,
3330 displayName : ( ) => 'Test' ,
3431 iconSvgInline : ( ) => '<svg></svg>' ,
3532 exec : async ( ) => true ,
36- } )
33+ }
3734
3835 expect ( action . id ) . toBe ( 'test' )
39- expect ( action . displayName ( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( 'Test' )
40- expect ( action . iconSvgInline ( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( '<svg></svg>' )
36+ expect ( action . displayName ( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( 'Test' )
37+ expect ( action . iconSvgInline ( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( '<svg></svg>' )
4138
4239 registerFileAction ( action )
4340
4441 expect ( window . _nc_fileactions ) . toHaveLength ( 1 )
4542 expect ( getFileActions ( ) ) . toHaveLength ( 1 )
4643 expect ( getFileActions ( ) [ 0 ] ) . toStrictEqual ( action )
47- expect ( logger . debug ) . toHaveBeenCalled ( )
4844 } )
4945
5046 test ( 'getFileActions() returned array is reactive' , ( ) => {
5147 logger . debug = vi . fn ( )
5248
53- const actions = getFileActions ( )
5449 // is empty for now
55- expect ( actions ) . toHaveLength ( 0 )
50+ expect ( getFileActions ( ) ) . toHaveLength ( 0 )
5651
57- const action = new FileAction ( {
52+ const action : IFileAction = {
5853 id : 'test' ,
5954 displayName : ( ) => 'Test' ,
6055 iconSvgInline : ( ) => '<svg></svg>' ,
6156 exec : async ( ) => true ,
62- } )
57+ }
6358
6459 registerFileAction ( action )
6560
66- // Now the array changed as it should be reactive
61+ const actions = getFileActions ( )
6762 expect ( actions ) . toHaveLength ( 1 )
6863 expect ( actions [ 0 ] ) . toStrictEqual ( action )
6964 } )
7065
7166 test ( 'Duplicate FileAction gets rejected' , ( ) => {
7267 logger . error = vi . fn ( )
73- const action = new FileAction ( {
68+ const action : IFileAction = {
7469 id : 'test' ,
7570 displayName : ( ) => 'Test' ,
7671 iconSvgInline : ( ) => '<svg></svg>' ,
7772 exec : async ( ) => true ,
78- } )
73+ }
7974
8075 registerFileAction ( action )
8176 expect ( getFileActions ( ) ) . toHaveLength ( 1 )
8277 expect ( getFileActions ( ) [ 0 ] ) . toStrictEqual ( action )
8378
84- const action2 = new FileAction ( {
79+ const action2 : IFileAction = {
8580 id : 'test' ,
8681 displayName : ( ) => 'Test 2' ,
8782 iconSvgInline : ( ) => '<svg></svg>' ,
8883 exec : async ( ) => true ,
89- } )
84+ }
9085
9186 registerFileAction ( action2 )
9287 expect ( getFileActions ( ) ) . toHaveLength ( 1 )
@@ -95,125 +90,125 @@ describe('FileActions init', () => {
9590 } )
9691} )
9792
98- describe ( 'Invalid FileAction creation ' , ( ) => {
93+ describe ( 'Invalid FileAction registration ' , ( ) => {
9994 test ( 'Invalid id' , ( ) => {
100- expect ( ( ) => new FileAction ( {
95+ expect ( ( ) => registerFileAction ( {
10196 displayName : ( ) => 'Test' ,
10297 iconSvgInline : ( ) => '<svg></svg>' ,
10398 exec : async ( ) => true ,
104- } as unknown as FileAction ) ) . toThrowError ( 'Invalid id' )
99+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid id' )
105100 } )
106101 test ( 'Invalid displayName' , ( ) => {
107- expect ( ( ) => new FileAction ( {
102+ expect ( ( ) => registerFileAction ( {
108103 id : 'test' ,
109104 displayName : 'Test' ,
110105 iconSvgInline : ( ) => '<svg></svg>' ,
111106 exec : async ( ) => true ,
112- } as unknown as FileAction ) ) . toThrowError ( 'Invalid displayName function' )
107+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid displayName function' )
113108 } )
114109 test ( 'Invalid title' , ( ) => {
115- expect ( ( ) => new FileAction ( {
110+ expect ( ( ) => registerFileAction ( {
116111 id : 'test' ,
117112 displayName : ( ) => 'Test' ,
118113 title : 'Test' ,
119114 iconSvgInline : ( ) => '<svg></svg>' ,
120115 exec : async ( ) => true ,
121- } as unknown as FileAction ) ) . toThrowError ( 'Invalid title function' )
116+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid title function' )
122117 } )
123118 test ( 'Invalid iconSvgInline' , ( ) => {
124- expect ( ( ) => new FileAction ( {
119+ expect ( ( ) => registerFileAction ( {
125120 id : 'test' ,
126121 displayName : ( ) => 'Test' ,
127122 iconSvgInline : '<svg></svg>' ,
128123 exec : async ( ) => true ,
129- } as unknown as FileAction ) ) . toThrowError ( 'Invalid iconSvgInline function' )
124+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid iconSvgInline function' )
130125 } )
131126 test ( 'Invalid exec' , ( ) => {
132- expect ( ( ) => new FileAction ( {
127+ expect ( ( ) => registerFileAction ( {
133128 id : 'test' ,
134129 displayName : ( ) => 'Test' ,
135130 iconSvgInline : ( ) => '<svg></svg>' ,
136131 exec : false ,
137- } as unknown as FileAction ) ) . toThrowError ( 'Invalid exec function' )
132+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid exec function' )
138133 } )
139134 test ( 'Invalid enabled' , ( ) => {
140- expect ( ( ) => new FileAction ( {
135+ expect ( ( ) => registerFileAction ( {
141136 id : 'test' ,
142137 displayName : ( ) => 'Test' ,
143138 iconSvgInline : ( ) => '<svg></svg>' ,
144139 exec : async ( ) => true ,
145140 enabled : false ,
146- } as unknown as FileAction ) ) . toThrowError ( 'Invalid enabled function' )
141+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid enabled function' )
147142 } )
148143 test ( 'Invalid execBatch' , ( ) => {
149- expect ( ( ) => new FileAction ( {
144+ expect ( ( ) => registerFileAction ( {
150145 id : 'test' ,
151146 displayName : ( ) => 'Test' ,
152147 iconSvgInline : ( ) => '<svg></svg>' ,
153148 exec : async ( ) => true ,
154149 execBatch : false ,
155- } as unknown as FileAction ) ) . toThrowError ( 'Invalid execBatch function' )
150+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid execBatch function' )
156151 } )
157152 test ( 'Invalid order' , ( ) => {
158- expect ( ( ) => new FileAction ( {
153+ expect ( ( ) => registerFileAction ( {
159154 id : 'test' ,
160155 displayName : ( ) => 'Test' ,
161156 iconSvgInline : ( ) => '<svg></svg>' ,
162157 exec : async ( ) => true ,
163158 order : 'invalid' ,
164- } as unknown as FileAction ) ) . toThrowError ( 'Invalid order' )
159+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid order' )
165160 } )
166161 test ( 'Invalid parent' , ( ) => {
167- expect ( ( ) => new FileAction ( {
162+ expect ( ( ) => registerFileAction ( {
168163 id : 'test' ,
169164 displayName : ( ) => 'Test' ,
170165 iconSvgInline : ( ) => '<svg></svg>' ,
171166 exec : async ( ) => true ,
172167 parent : true ,
173- } as unknown as FileAction ) ) . toThrowError ( 'Invalid parent' )
168+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid parent' )
174169 } )
175170 test ( 'Invalid default' , ( ) => {
176- expect ( ( ) => new FileAction ( {
171+ expect ( ( ) => registerFileAction ( {
177172 id : 'test' ,
178173 displayName : ( ) => 'Test' ,
179174 iconSvgInline : ( ) => '<svg></svg>' ,
180175 exec : async ( ) => true ,
181176 default : 'invalid' ,
182- } as unknown as FileAction ) ) . toThrowError ( 'Invalid default' )
177+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid default' )
183178 } )
184179 test ( 'Invalid destructives' , ( ) => {
185- expect ( ( ) => new FileAction ( {
180+ expect ( ( ) => registerFileAction ( {
186181 id : 'test' ,
187182 displayName : ( ) => 'Test' ,
188183 iconSvgInline : ( ) => '<svg></svg>' ,
189184 exec : async ( ) => true ,
190185 destructive : 'false' ,
191- } as unknown as FileActionData ) ) . toThrowError ( 'Invalid destructive' )
186+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid destructive' )
192187 } )
193188 test ( 'Invalid inline' , ( ) => {
194- expect ( ( ) => new FileAction ( {
189+ expect ( ( ) => registerFileAction ( {
195190 id : 'test' ,
196191 displayName : ( ) => 'Test' ,
197192 iconSvgInline : ( ) => '<svg></svg>' ,
198193 exec : async ( ) => true ,
199194 inline : true ,
200- } as unknown as FileAction ) ) . toThrowError ( 'Invalid inline function' )
195+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid inline function' )
201196
202- expect ( ( ) => new FileAction ( {
197+ expect ( ( ) => registerFileAction ( {
203198 id : 'test' ,
204199 displayName : ( ) => 'Test' ,
205200 iconSvgInline : ( ) => '<svg></svg>' ,
206201 exec : async ( ) => true ,
207202 inline : ( ) => true ,
208203 renderInline : false ,
209- } as unknown as FileAction ) ) . toThrowError ( 'Invalid renderInline function' )
204+ } as unknown as IFileAction ) ) . toThrowError ( 'Invalid renderInline function' )
210205 } )
211206} )
212207
213208describe ( 'FileActions creation' , ( ) => {
214209 test ( 'create valid FileAction' , async ( ) => {
215210 logger . debug = vi . fn ( )
216- const action = new FileAction ( {
211+ const action : IFileAction = {
217212 id : 'test' ,
218213 displayName : ( ) => 'Test' ,
219214 title : ( ) => 'Test title' ,
@@ -231,22 +226,22 @@ describe('FileActions creation', () => {
231226 span . textContent = 'test'
232227 return span
233228 } ,
234- } )
229+ }
235230
236231 const node = { } as Node
237232
238233 expect ( action . id ) . toBe ( 'test' )
239- expect ( action . displayName ( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( 'Test' )
240- expect ( action . title ?.( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( 'Test title' )
241- expect ( action . iconSvgInline ( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( '<svg></svg>' )
242- await expect ( action . exec ( { view, folder, nodes : [ node ] , content : [ ] } ) ) . resolves . toBe ( true )
243- await expect ( action . execBatch ?.( { view, folder, nodes : [ ] , content : [ ] } ) ) . resolves . toStrictEqual ( [ true ] )
244- expect ( action . enabled ?.( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( true )
234+ expect ( action . displayName ( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( 'Test' )
235+ expect ( action . title ?.( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( 'Test title' )
236+ expect ( action . iconSvgInline ( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( '<svg></svg>' )
237+ await expect ( action . exec ( { view, folder, nodes : [ node ] , contents : [ ] } ) ) . resolves . toBe ( true )
238+ await expect ( action . execBatch ?.( { view, folder, nodes : [ ] , contents : [ ] } ) ) . resolves . toStrictEqual ( [ true ] )
239+ expect ( action . enabled ?.( { view, folder, nodes : [ ] , contents : [ ] } ) ) . toBe ( true )
245240 expect ( action . order ) . toBe ( 100 )
246241 expect ( action . parent ) . toBe ( '123' )
247242 expect ( action . destructive ) . toBe ( true )
248243 expect ( action . default ) . toBe ( DefaultType . DEFAULT )
249- expect ( action . inline ?.( { view, folder, nodes : [ ] , content : [ ] } ) ) . toBe ( true )
250- expect ( ( await action . renderInline ?.( { view, folder, nodes : [ ] , content : [ ] } ) ) ?. outerHTML ) . toBe ( '<span>test</span>' )
244+ expect ( action . inline ?.( { view, folder, nodes : [ node ] , contents : [ ] } ) ) . toBe ( true )
245+ expect ( ( await action . renderInline ?.( { view, folder, nodes : [ node ] , contents : [ ] } ) ) ?. outerHTML ) . toBe ( '<span>test</span>' )
251246 } )
252247} )
0 commit comments