1+ import { ref } from 'vue'
12import { mount , config } from '@vue/test-utils'
23import { describe , test , expect , vi , afterEach } from 'vitest'
34import UIkit from 'uikit'
45import type { UserInterface , RegisterRequest } from '@userfrosting/sprinkle-account/interfaces'
6+ import { useRegisterApi } from '@userfrosting/sprinkle-account/composables'
57import type { AlertInterface } from '@userfrosting/sprinkle-core/interfaces'
68import FormRegister from '../../../components/Pages/Account/FormRegister.vue'
79import UFAlert from '../../../components/UFAlert.vue'
@@ -68,17 +70,6 @@ const uikitNotification = {
6870 timeout : 4000
6971}
7072
71- // Mock the register composable
72- const mockedSubmitRegistration = vi . fn ( )
73- vi . mock ( '@userfrosting/sprinkle-account/composables' , ( ) => ( {
74- useRegisterApi : ( ) => ( {
75- defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
76- availableLocales : vi . fn ( ( ) => availableLocales ) ,
77- captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
78- submitRegistration : mockedSubmitRegistration
79- } )
80- } ) )
81-
8273// Mock the config & translator store
8374vi . mock ( '@userfrosting/sprinkle-core/stores' , ( ) => ( {
8475 useConfigStore : ( ) => ( {
@@ -97,21 +88,42 @@ vi.mock('vue-router', () => ({
9788 } )
9889} ) )
9990
91+ vi . mock ( '@userfrosting/sprinkle-account/composables' , ( ) => ( {
92+ useRegisterApi : vi . fn ( )
93+ } ) )
94+
10095describe ( 'FormRegister.vue' , ( ) => {
10196 afterEach ( ( ) => {
10297 vi . clearAllMocks ( )
10398 } )
10499
105100 test ( 'renders correctly' , ( ) => {
101+ vi . mocked ( useRegisterApi ) . mockReturnValue ( {
102+ submitRegistration : vi . fn ( ) ,
103+ defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
104+ availableLocales : vi . fn ( ( ) => availableLocales ) ,
105+ captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
106+ apiLoading : ref ( false ) ,
107+ apiError : ref ( null )
108+ } )
109+
106110 const wrapper = mount ( FormRegister )
107111 expect ( wrapper . exists ( ) ) . toBe ( true )
108112 } )
109113
110114 test ( 'handles successful register' , async ( ) => {
111- vi . mocked ( mockedSubmitRegistration ) . mockResolvedValueOnce ( {
115+ const mockedSubmitRegistration = vi . fn ( ) . mockResolvedValueOnce ( {
112116 user : testUser ,
113117 message : 'Succesfully registered John Doe!'
114118 } )
119+ vi . mocked ( useRegisterApi ) . mockReturnValue ( {
120+ submitRegistration : mockedSubmitRegistration ,
121+ defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
122+ availableLocales : vi . fn ( ( ) => availableLocales ) ,
123+ captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
124+ apiLoading : ref ( true ) ,
125+ apiError : ref ( null )
126+ } )
115127 vi . spyOn ( UIkit , 'notification' )
116128
117129 const wrapper = mount ( FormRegister )
@@ -126,29 +138,59 @@ describe('FormRegister.vue', () => {
126138 expect ( UIkit . notification ) . toHaveBeenCalledWith ( uikitNotification )
127139 } )
128140
129- test ( 'handles registration failure' , async ( ) => {
130- const mockError : AlertInterface = { title : 'Invalid credentials' }
131- vi . mocked ( mockedSubmitRegistration ) . mockRejectedValueOnce ( mockError )
132- vi . spyOn ( UIkit , 'notification' )
141+ test ( 'disables submit button when loading' , ( ) => {
142+ vi . mocked ( useRegisterApi ) . mockReturnValue ( {
143+ submitRegistration : vi . fn ( ) ,
144+ defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
145+ availableLocales : vi . fn ( ( ) => availableLocales ) ,
146+ captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
147+ apiLoading : ref ( true ) , // Set loading state to true
148+ apiError : ref ( null )
149+ } )
133150
134151 const wrapper = mount ( FormRegister )
135- // @ts -ignore
136- wrapper . vm . formData = testForm
137- await ( wrapper . vm as any ) . submitForm ( )
152+ expect ( wrapper . find ( '[data-test="submit"]' ) . exists ( ) ) . toBe ( true )
153+ expect ( wrapper . find ( '[data-test="submit"]' ) . text ( ) ) . toBe ( 'REGISTER_ME' )
154+ expect ( wrapper . find ( '[data-test="submit"]' ) . attributes ( ) . disabled ) . toBeDefined ( )
155+ } )
156+
157+ test ( 'handles registration failure' , async ( ) => {
158+ const mockedApiError : AlertInterface = {
159+ title : 'Registration error' ,
160+ description : 'You did not enter the captcha code correctly.' ,
161+ style : 'Danger' ,
162+ closeBtn : true
163+ }
164+ vi . mocked ( useRegisterApi ) . mockReturnValue ( {
165+ submitRegistration : vi . fn ( ) ,
166+ defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
167+ availableLocales : vi . fn ( ( ) => availableLocales ) ,
168+ captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
169+ apiLoading : ref ( true ) ,
170+ apiError : ref ( mockedApiError )
171+ } )
172+ const wrapper = mount ( FormRegister )
138173
139174 // Spy on the authStore & UIkit notification method
140- expect ( mockedSubmitRegistration ) . toHaveBeenCalledTimes ( 1 )
141- expect ( mockedSubmitRegistration ) . toHaveBeenCalledWith ( testForm )
142- expect ( UIkit . notification ) . not . toHaveBeenCalled ( )
143175 expect ( wrapper . find ( '[data-test="error"]' ) . exists ( ) ) . toBe ( true )
144- expect ( wrapper . get ( '[data-test="error"]' ) . text ( ) ) . toMatch ( 'Invalid credentials' )
176+ expect ( wrapper . get ( '[data-test="error"]' ) . text ( ) ) . toMatch (
177+ 'Registration error You did not enter the captcha code correctly.'
178+ )
145179 } )
146180
147181 test ( 'Handle form using the v-model' , async ( ) => {
148- vi . mocked ( mockedSubmitRegistration ) . mockResolvedValueOnce ( {
182+ const mockedSubmitRegistration = vi . fn ( ) . mockResolvedValueOnce ( {
149183 user : testUser ,
150184 message : 'Succesfully registered John Doe!'
151185 } )
186+ vi . mocked ( useRegisterApi ) . mockReturnValue ( {
187+ submitRegistration : mockedSubmitRegistration ,
188+ defaultRegistrationForm : vi . fn ( ( ) => defaultForm ) ,
189+ availableLocales : vi . fn ( ( ) => availableLocales ) ,
190+ captchaUrl : vi . fn ( ( ) => '/account/captcha' ) ,
191+ apiLoading : ref ( true ) ,
192+ apiError : ref ( null )
193+ } )
152194 vi . spyOn ( UIkit , 'notification' )
153195
154196 const wrapper = mount ( FormRegister )
0 commit comments