@@ -2,7 +2,7 @@ import type { EnterpriseAccountConnectionJSON, PhoneNumberJSON } from '@clerk/sh
22import { act , screen , waitFor , within } from '@testing-library/react' ;
33import userEvent from '@testing-library/user-event' ;
44import { http , HttpResponse } from 'msw' ;
5- import { describe , expect , it } from 'vitest' ;
5+ import { describe , expect , it , vi } from 'vitest' ;
66
77import {
88 demandReverificationOnce ,
@@ -159,6 +159,56 @@ describe('the user profile phone section', () => {
159159 expect ( await screen . findByRole ( 'button' , { name : / R e s e n d \( \d + \) / } ) ) . toBeDisabled ( ) ;
160160 } ) ;
161161
162+ it ( 'disables Cancel while verifying a phone code and allows cancellation after an error' , async ( ) => {
163+ const { actor } = await renderSection ( ) ;
164+ await addPhone ( actor , '8015550100' ) ;
165+ const attempt = holdRequests ( 'post' , '/v1/me/phone_numbers/:id/attempt_verification' ) ;
166+
167+ try {
168+ await enterCode ( actor , '000000' ) ;
169+ await waitFor ( ( ) => expect ( attempt . requests ) . toHaveLength ( 1 ) ) ;
170+ expect ( screen . getByRole ( 'button' , { name : 'Cancel' } ) ) . toBeDisabled ( ) ;
171+ expect ( screen . getByRole ( 'dialog' , { name : 'Verify your phone number' } ) ) . toBeVisible ( ) ;
172+ } finally {
173+ attempt . release ( ) ;
174+ }
175+
176+ expect ( await screen . findByText ( 'Incorrect code' ) ) . toBeVisible ( ) ;
177+ const cancel = screen . getByRole ( 'button' , { name : 'Cancel' } ) ;
178+ expect ( cancel ) . toBeEnabled ( ) ;
179+ await actor . click ( cancel ) ;
180+ await waitFor ( ( ) => expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ) ;
181+ } ) ;
182+
183+ it ( 'disables Cancel while resending a phone code and allows cancellation once sent' , async ( { onTestFinished } ) => {
184+ vi . useFakeTimers ( { shouldAdvanceTime : true } ) ;
185+ onTestFinished ( ( ) => vi . useRealTimers ( ) ) ;
186+ await renderSection ( ) ;
187+ const actor = userEvent . setup ( { advanceTimers : vi . advanceTimersByTime } ) ;
188+ await addPhone ( actor , '8015550100' ) ;
189+ await screen . findByRole ( 'textbox' , { name : 'Verification code' } ) ;
190+ const prepare = holdRequests ( 'post' , '/v1/me/phone_numbers/:id/prepare_verification' ) ;
191+
192+ try {
193+ for ( let second = 0 ; second < 30 ; second ++ ) {
194+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 1000 ) ) ;
195+ }
196+ const resend = screen . getByRole ( 'button' , { name : / R e s e n d / } ) ;
197+ await waitFor ( ( ) => expect ( resend ) . toBeEnabled ( ) ) ;
198+ await actor . click ( resend ) ;
199+ await waitFor ( ( ) => expect ( prepare . requests ) . toHaveLength ( 1 ) ) ;
200+ expect ( screen . getByRole ( 'button' , { name : 'Cancel' } ) ) . toBeDisabled ( ) ;
201+ expect ( screen . getByRole ( 'dialog' , { name : 'Verify your phone number' } ) ) . toBeVisible ( ) ;
202+ } finally {
203+ prepare . release ( ) ;
204+ }
205+
206+ const cancel = screen . getByRole ( 'button' , { name : 'Cancel' } ) ;
207+ await waitFor ( ( ) => expect ( cancel ) . toBeEnabled ( ) ) ;
208+ await actor . click ( cancel ) ;
209+ await waitFor ( ( ) => expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ) ;
210+ } ) ;
211+
162212 it ( 'verifies a number that was left unverified' , async ( ) => {
163213 const pending = fapiPhoneNumber ( { id : 'idn_new' , phone_number : '+18015550199' } ) ;
164214 const { actor } = await renderSection ( signedIn ( [ HOME , pending ] ) ) ;
0 commit comments