1+ /* eslint-disable @typescript-eslint/no-explicit-any */
12/**
23 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
34 * SPDX-License-Identifier: AGPL-3.0-or-later
45 */
56
67import type { Wrapper } from '@vue/test-utils'
7- import type { ComponentProps } from 'vue-component-type-helpers'
88
99import { afterEach , describe , expect , it , vi } from 'vitest'
1010import { File , Folder , Permission } from '@nextcloud/files'
@@ -13,46 +13,9 @@ import { shallowMount } from '@vue/test-utils'
1313import FileListRow from './FileListRow.vue'
1414import { nextTick } from 'vue'
1515
16- /* eslint-disable @typescript-eslint/no-explicit-any, jsdoc/require-jsdoc */
17-
1816type SubmitAction = ( wrapper : Wrapper < any > ) => Promise < void >
1917type ElementEvent = { 'update:selected' : boolean | undefined , 'enter-directory' : Folder | undefined }
20-
21- async function clickCheckboxAction ( wrapper : Wrapper < any > ) {
22- wrapper . find ( 'input[type="checkbox"]' ) . trigger ( 'click' )
23- }
24-
25- async function clickElementAction ( wrapper : Wrapper < any > ) {
26- wrapper . find ( '[data-testid="row-name"]' ) . trigger ( 'click' )
27- }
28-
29- async function pressEnterAction ( wrapper : Wrapper < any > ) {
30- wrapper . element . dispatchEvent ( new KeyboardEvent ( 'keydown' , { bubbles : true , key : 'Enter' } ) )
31- await nextTick ( )
32- }
33-
34- function testSubmitNode ( name : string , propsData : ComponentProps < typeof FileListRow > , eventPayload : ElementEvent , actionCallback : SubmitAction ) {
35- it ( name , async ( ) => {
36- const wrapper = shallowMount ( FileListRow as any , {
37- propsData,
38- stubs : {
39- NcCheckboxRadioSwitch : {
40- template : '<label><input type="checkbox" @click="$emit(\'update:model-value\', true)" ></label>' ,
41- } ,
42- } ,
43- } )
44-
45- await actionCallback ( wrapper )
46-
47- for ( const [ event , payload ] of Object . entries ( eventPayload ) ) {
48- if ( payload === undefined ) {
49- expect ( wrapper . emitted ( event ) ) . toBeUndefined ( )
50- } else {
51- expect ( wrapper . emitted ( event ) ) . toEqual ( [ [ payload ] ] )
52- }
53- }
54- } )
55- }
18+ type FileListRowProps = InstanceType < typeof FileListRow > [ '$props' ]
5619
5720const node = new File ( {
5821 owner : 'alice' ,
@@ -222,3 +185,62 @@ describe('FilePicker: FileListRow', () => {
222185 } )
223186 } )
224187} )
188+
189+ /**
190+ * Helper function to test the emitted events when submitting a node (file or folder)
191+ *
192+ * @param name - the name of the test case
193+ * @param propsData - the props to pass to the FileListRow component
194+ * @param eventPayload - the expected emitted events and their payloads
195+ * @param actionCallback - the action to perform to submit the node
196+ */
197+ function testSubmitNode ( name : string , propsData : FileListRowProps , eventPayload : ElementEvent , actionCallback : SubmitAction ) {
198+ it ( name , async ( ) => {
199+ const wrapper = shallowMount ( FileListRow as any , {
200+ propsData,
201+ stubs : {
202+ NcCheckboxRadioSwitch : {
203+ template : '<label><input type="checkbox" @click="$emit(\'update:model-value\', true)" ></label>' ,
204+ } ,
205+ } ,
206+ } )
207+
208+ await actionCallback ( wrapper )
209+
210+ for ( const [ event , payload ] of Object . entries ( eventPayload ) ) {
211+ if ( payload === undefined ) {
212+ expect ( wrapper . emitted ( event ) ) . toBeUndefined ( )
213+ } else {
214+ expect ( wrapper . emitted ( event ) ) . toEqual ( [ [ payload ] ] )
215+ }
216+ }
217+ } )
218+ }
219+
220+ /**
221+ * Click on the checkbox element
222+ *
223+ * @param wrapper - the wrapper of the FileListRow component
224+ */
225+ async function clickCheckboxAction ( wrapper : Wrapper < any > ) {
226+ wrapper . find ( 'input[type="checkbox"]' ) . trigger ( 'click' )
227+ }
228+
229+ /**
230+ * Click on the row element
231+ *
232+ * @param wrapper - the wrapper of the FileListRow component
233+ */
234+ async function clickElementAction ( wrapper : Wrapper < any > ) {
235+ wrapper . find ( '[data-testid="row-name"]' ) . trigger ( 'click' )
236+ }
237+
238+ /**
239+ * Press Enter key on the row element
240+ *
241+ * @param wrapper - the wrapper of the FileListRow component
242+ */
243+ async function pressEnterAction ( wrapper : Wrapper < any > ) {
244+ wrapper . element . dispatchEvent ( new KeyboardEvent ( 'keydown' , { bubbles : true , key : 'Enter' } ) )
245+ await nextTick ( )
246+ }
0 commit comments