55
66import { mount } from '@vue/test-utils'
77import { beforeEach , describe , expect , it , vi } from 'vitest'
8+
9+ // `<script setup>` children and `useStore()` are invisible to VTU stubs/mocks,
10+ // so swap them via `vi.mock` (see dialogTestHelpers).
11+ const { dispatch } = vi . hoisted ( ( ) => ( { dispatch : vi . fn ( ) } ) )
12+
13+ vi . mock ( '../../store/index.js' , ( ) => ( {
14+ useStore : ( ) => ( {
15+ dispatch,
16+ getters : {
17+ getServerData : { newUserGenerateUserID : false , newUserRequireEmail : false } ,
18+ getPasswordPolicyMinLength : 8 ,
19+ } ,
20+ } ) ,
21+ } ) )
22+ vi . mock ( '@nextcloud/vue/components/NcDialog' , async ( ) => ( { default : ( await import ( './dialogTestHelpers.ts' ) ) . NcDialogStub } ) )
23+ vi . mock ( '@nextcloud/vue/components/NcButton' , async ( ) => ( { default : ( await import ( './dialogTestHelpers.ts' ) ) . NcButtonStub } ) )
24+ vi . mock ( './UserFormFields.vue' , async ( ) => ( { default : ( await import ( './dialogTestHelpers.ts' ) ) . UserFormFieldsStub } ) )
25+
826import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
927import NewUserDialog from './NewUserDialog.vue'
10- import { flushPromises , NcButtonStub , NcDialogStub , UserFormFieldsStub } from './dialogTestHelpers.ts'
28+ import { flushPromises , NcDialogStub } from './dialogTestHelpers.ts'
1129
1230function makeNewUser ( overrides = { } ) {
1331 return {
@@ -24,28 +42,13 @@ function makeNewUser(overrides = {}) {
2442 }
2543}
2644
27- function mountDialog ( { dispatch = vi . fn ( ) , loading = { all : false } } = { } ) {
45+ function mountDialog ( { loading = { all : false } } = { } ) {
2846 return mount ( NewUserDialog , {
2947 propsData : {
3048 loading,
3149 newUser : makeNewUser ( ) ,
3250 quotaOptions : [ ] ,
3351 } ,
34- mocks : {
35- t : ( _app : string , text : string ) => text ,
36- $store : {
37- dispatch,
38- getters : {
39- getServerData : { newUserGenerateUserID : false , newUserRequireEmail : false } ,
40- getPasswordPolicyMinLength : 8 ,
41- } ,
42- } ,
43- } ,
44- stubs : {
45- NcDialog : NcDialogStub ,
46- NcButton : NcButtonStub ,
47- UserFormFields : UserFormFieldsStub ,
48- } ,
4952 } )
5053}
5154
@@ -55,8 +58,8 @@ describe('NewUserDialog loading feedback', () => {
5558 } )
5659
5760 it ( 'does not dispatch a second create request while one is in flight' , async ( ) => {
58- const dispatch = vi . fn ( ) . mockReturnValue ( new Promise ( ( ) => { } ) )
59- const wrapper = mountDialog ( { dispatch } )
61+ dispatch . mockReturnValue ( new Promise ( ( ) => { } ) )
62+ const wrapper = mountDialog ( )
6063
6164 await wrapper . find ( 'form' ) . trigger ( 'submit' )
6265 await wrapper . find ( 'form' ) . trigger ( 'submit' )
@@ -66,8 +69,8 @@ describe('NewUserDialog loading feedback', () => {
6669 } )
6770
6871 it ( 'marks the form as busy and inert while creating' , async ( ) => {
69- const dispatch = vi . fn ( ) . mockReturnValue ( new Promise ( ( ) => { } ) )
70- const wrapper = mountDialog ( { dispatch } )
72+ dispatch . mockReturnValue ( new Promise ( ( ) => { } ) )
73+ const wrapper = mountDialog ( )
7174
7275 await wrapper . find ( 'form' ) . trigger ( 'submit' )
7376
@@ -77,8 +80,8 @@ describe('NewUserDialog loading feedback', () => {
7780 } )
7881
7982 it ( 'shows a spinner and busy label on the submit button while creating' , async ( ) => {
80- const dispatch = vi . fn ( ) . mockReturnValue ( new Promise ( ( ) => { } ) )
81- const wrapper = mountDialog ( { dispatch } )
83+ dispatch . mockReturnValue ( new Promise ( ( ) => { } ) )
84+ const wrapper = mountDialog ( )
8285
8386 await wrapper . find ( 'form' ) . trigger ( 'submit' )
8487
@@ -87,8 +90,8 @@ describe('NewUserDialog loading feedback', () => {
8790 } )
8891
8992 it ( 'sets aria-disabled (not disabled) on the submit button while creating' , async ( ) => {
90- const dispatch = vi . fn ( ) . mockReturnValue ( new Promise ( ( ) => { } ) )
91- const wrapper = mountDialog ( { dispatch } )
93+ dispatch . mockReturnValue ( new Promise ( ( ) => { } ) )
94+ const wrapper = mountDialog ( )
9295 const submit = wrapper . find ( '[data-test="submit"]' )
9396
9497 expect ( submit . attributes ( 'aria-disabled' ) ) . toBe ( 'false' )
@@ -101,8 +104,8 @@ describe('NewUserDialog loading feedback', () => {
101104 } )
102105
103106 it ( 'prevents closing the dialog while creating' , async ( ) => {
104- const dispatch = vi . fn ( ) . mockReturnValue ( new Promise ( ( ) => { } ) )
105- const wrapper = mountDialog ( { dispatch } )
107+ dispatch . mockReturnValue ( new Promise ( ( ) => { } ) )
108+ const wrapper = mountDialog ( )
106109 const dialog = wrapper . findComponent ( NcDialogStub )
107110
108111 expect ( dialog . props ( 'noClose' ) ) . toBe ( false )
@@ -114,9 +117,9 @@ describe('NewUserDialog loading feedback', () => {
114117
115118 it ( 're-enables the form when the request fails' , async ( ) => {
116119 const error = { response : { data : { ocs : { meta : { statuscode : 0 } } } } }
117- const dispatch = vi . fn ( ) . mockRejectedValue ( error )
120+ dispatch . mockRejectedValue ( error )
118121 const loading = { all : false }
119- const wrapper = mountDialog ( { dispatch , loading } )
122+ const wrapper = mountDialog ( { loading } )
120123
121124 await wrapper . find ( 'form' ) . trigger ( 'submit' )
122125 await flushPromises ( )
0 commit comments