1- import { screen , waitFor , within } from '@testing-library/react' ;
1+ import { act , screen , waitFor , within } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
33import { http , HttpResponse } from 'msw' ;
44import { describe , expect , it , vi } from 'vitest' ;
@@ -355,7 +355,7 @@ describe('User profile MFA management', () => {
355355 expect ( clerk . user ?. id ) . toBe ( 'user_2' ) ;
356356 } ) ;
357357
358- it ( 'opens the backup-code surface while regeneration is pending ' , async ( ) => {
358+ it ( 'keeps the profile visible until regenerated backup codes are ready ' , async ( ) => {
359359 await renderMfa (
360360 fapiUser ( { id : 'user_1' , totp_enabled : true , backup_code_enabled : true , two_factor_enabled : true } ) ,
361361 ) ;
@@ -364,22 +364,46 @@ describe('User profile MFA management', () => {
364364 await openMenu ( user , screen . getByRole ( 'button' , { name : 'Manage Backup codes' } ) ) ;
365365 await user . click ( screen . getByRole ( 'menuitem' , { name : 'Regenerate' } ) ) ;
366366 await waitFor ( ( ) => expect ( held . requests ) . toHaveLength ( 1 ) ) ;
367- await waitFor ( ( ) => expect ( screen . getByRole ( 'status' , { name : 'Generating backup codes' } ) ) . toBeVisible ( ) ) ;
368- for ( const name of [ 'Print' , 'Download' ] ) {
369- expect ( screen . getByRole ( 'button' , { name } ) ) . toBeVisible ( ) ;
370- expect ( screen . getByRole ( 'button' , { name } ) ) . toBeDisabled ( ) ;
367+ try {
368+ expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
369+ expect ( screen . getByRole ( 'button' , { name : 'Add verification method' } ) ) . toBeDisabled ( ) ;
370+ expect ( screen . getByRole ( 'button' , { name : 'Manage Backup codes' } ) ) . toBeVisible ( ) ;
371+ } finally {
372+ held . release ( ) ;
371373 }
372- expect ( screen . getByRole ( 'button' , { name : 'Copy and close' } ) ) . toBeVisible ( ) ;
373- expect ( screen . getByRole ( 'button' , { name : 'Copy and close' } ) ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
374- expect ( screen . queryByRole ( 'button' , { name : 'Close' } ) ) . toBeNull ( ) ;
375- held . release ( ) ;
376374 await waitFor ( ( ) => expect ( screen . getByText ( 'CODE0100' ) ) . toBeVisible ( ) ) ;
377375 expect ( screen . getByRole ( 'button' , { name : 'Close' } ) ) . toBeVisible ( ) ;
378376 for ( const name of [ 'Print' , 'Download' , 'Copy and close' ] ) {
379377 expect ( screen . getByRole ( 'button' , { name } ) ) . toBeEnabled ( ) ;
380378 }
381379 } ) ;
382380
381+ it ( 'returns failed regeneration to the section and retries from the menu' , async ( ) => {
382+ const fapi = await renderMfa (
383+ fapiUser ( { id : 'user_1' , totp_enabled : true , backup_code_enabled : true , two_factor_enabled : true } ) ,
384+ ) ;
385+ const handlers = worker . listHandlers ( ) ;
386+ const held = holdRequests ( 'post' , '/v1/me/backup_codes/' ) ;
387+ const user = userEvent . setup ( ) ;
388+ const trigger = screen . getByRole ( 'button' , { name : 'Manage Backup codes' } ) ;
389+ await openMenu ( user , trigger ) ;
390+ await user . click ( screen . getByRole ( 'menuitem' , { name : 'Regenerate' } ) ) ;
391+ await waitFor ( ( ) => expect ( held . requests ) . toHaveLength ( 1 ) ) ;
392+ held . fail ( 'action_blocked' , 'Unable to create backup codes.' ) ;
393+ await waitFor ( ( ) =>
394+ expect ( screen . getByRole ( 'alert' ) ) . toHaveTextContent (
395+ "This action couldn't be completed. Please try again later or contact support if this persists." ,
396+ ) ,
397+ ) ;
398+ expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
399+ await waitFor ( ( ) => expect ( screen . queryByRole ( 'menu' ) ) . toBeNull ( ) ) ;
400+ worker . resetHandlers ( ...handlers ) ;
401+ await openMenu ( user , trigger ) ;
402+ await user . click ( screen . getByRole ( 'menuitem' , { name : 'Regenerate' } ) ) ;
403+ expect ( await screen . findByText ( 'CODE0100' ) ) . toBeVisible ( ) ;
404+ expect ( fapi . mfa . backupCodeCreations ) . toBe ( 1 ) ;
405+ } ) ;
406+
383407 it ( 'uses fresh server codes for explicit regeneration' , async ( ) => {
384408 const fapi = await renderMfa (
385409 fapiUser ( { id : 'user_1' , totp_enabled : true , backup_code_enabled : true , two_factor_enabled : true } ) ,
@@ -414,7 +438,21 @@ describe('User profile MFA management', () => {
414438
415439 await regenerate ( user ) ;
416440 expect ( await screen . findByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
417- await user . type ( screen . getByLabelText ( 'Password' ) , 'hunter2{Enter}' ) ;
441+ await waitFor ( ( ) => expect ( screen . getByLabelText ( 'Password' ) ) . toHaveFocus ( ) ) ;
442+ const refresh = holdRequests ( 'get' , '/v1/me' ) ;
443+ try {
444+ await user . keyboard ( 'hunter2{Enter}' ) ;
445+ await act ( async ( ) => {
446+ await waitFor ( ( ) => expect ( refresh . requests ) . toHaveLength ( 1 ) ) ;
447+ } ) ;
448+ await user . keyboard ( '{Escape}' ) ;
449+ expect ( screen . getByRole ( 'dialog' ) ) . toHaveAttribute ( 'data-open' ) ;
450+ expect ( screen . getByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
451+ expect ( screen . queryByRole ( 'button' , { name : 'Close' } ) ) . toBeNull ( ) ;
452+ expect ( screen . getByRole ( 'button' , { name : 'Continue' } ) ) . toHaveAttribute ( 'aria-busy' , 'true' ) ;
453+ } finally {
454+ refresh . release ( ) ;
455+ }
418456
419457 const list = await screen . findByRole ( 'list' , { name : 'Backup codes' } ) ;
420458 expect ( within ( list ) . getAllByRole ( 'listitem' ) ) . toHaveLength ( 10 ) ;
@@ -445,6 +483,7 @@ describe('User profile MFA management', () => {
445483 await regenerate ( user ) ;
446484 await screen . findByText ( 'Enter your current password to continue' ) ;
447485 const dialog = screen . getByRole ( 'dialog' ) ;
486+ const handlers = worker . listHandlers ( ) ;
448487 const replay = holdRequests ( 'post' , '/v1/me/backup_codes/' ) ;
449488 await user . type ( screen . getByLabelText ( 'Password' ) , 'hunter2{Enter}' ) ;
450489 await waitFor ( ( ) => expect ( replay . requests ) . toHaveLength ( 1 ) ) ;
@@ -454,47 +493,53 @@ describe('User profile MFA management', () => {
454493 expect ( dialog ) . toBeVisible ( ) ;
455494
456495 replay . fail ( 'action_blocked' , 'Unable to create backup codes.' ) ;
457- await waitFor ( ( ) => expect ( within ( screen . getByRole ( 'dialog' ) ) . getByRole ( 'alert' ) ) . toBeVisible ( ) ) ;
496+ await waitFor ( ( ) => expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ) ;
497+ expect ( screen . getByRole ( 'alert' ) ) . toHaveTextContent (
498+ "This action couldn't be completed. Please try again later or contact support if this persists." ,
499+ ) ;
458500 expect ( screen . queryByRole ( 'list' , { name : 'Backup codes' } ) ) . toBeNull ( ) ;
459501 expect ( fapi . mfa . backupCodeCreations ) . toBe ( 0 ) ;
502+ worker . resetHandlers ( ...handlers ) ;
503+ await regenerate ( user ) ;
504+ await screen . findByRole ( 'list' , { name : 'Backup codes' } ) ;
505+ expect ( fapi . mfa . backupCodeCreations ) . toBe ( 1 ) ;
460506 } ) ;
461507
462- it ( 'cancels a loading verification when the backup-code dialog closes so regeneration can start again ' , async ( ) => {
508+ it ( 'keeps the profile visible until verification preparation is ready ' , async ( ) => {
463509 const fapi = await renderBackupCodesNeedingReverification ( ) ;
464510 const start = holdRequests ( 'post' , '/v1/client/sessions/:id/verify' ) ;
465511 const user = userEvent . setup ( ) ;
466512
467513 await regenerate ( user ) ;
468514 await waitFor ( ( ) => expect ( start . requests ) . toHaveLength ( 1 ) ) ;
469- await user . click ( within ( screen . getByRole ( 'dialog' ) ) . getByRole ( 'button' , { name : 'Close' } ) ) ;
470- await waitFor ( ( ) => expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ) ;
471- start . release ( ) ;
472-
473- await regenerate ( user ) ;
515+ try {
516+ expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
517+ expect ( screen . getByRole ( 'button' , { name : 'Manage Backup codes' } ) ) . toBeVisible ( ) ;
518+ } finally {
519+ start . release ( ) ;
520+ }
521+ await screen . findByText ( 'Enter your current password to continue' ) ;
522+ await waitFor ( ( ) => expect ( screen . getByRole ( 'dialog' ) ) . toContainElement ( document . activeElement ) ) ;
523+ await user . type ( screen . getByLabelText ( 'Password' ) , 'hunter2{Enter}' ) ;
474524 await screen . findByRole ( 'list' , { name : 'Backup codes' } ) ;
475525 expect ( fapi . mfa . backupCodeCreations ) . toBe ( 1 ) ;
476526 } ) ;
477527
478- it ( 'keeps the backup-code dialog open until the first response so a verification can appear ' , async ( ) => {
479- const fapi = await renderBackupCodesNeedingReverification ( ) ;
528+ it ( 'keeps the profile visible until the first response can request verification' , async ( ) => {
529+ await renderBackupCodesNeedingReverification ( ) ;
480530 const first = holdRequests ( 'post' , '/v1/me/backup_codes/' ) ;
481531 const user = userEvent . setup ( ) ;
482532
483533 await regenerate ( user ) ;
484534 await waitFor ( ( ) => expect ( first . requests ) . toHaveLength ( 1 ) ) ;
485- const dialog = screen . getByRole ( 'dialog' ) ;
486535 try {
487- expect ( within ( dialog ) . queryByRole ( 'button' , { name : 'Close' } ) ) . toBeNull ( ) ;
488- await user . keyboard ( '{Escape}' ) ;
489- expect ( dialog ) . toBeVisible ( ) ;
536+ expect ( screen . queryByRole ( 'dialog' ) ) . toBeNull ( ) ;
537+ expect ( screen . getByRole ( 'button' , { name : 'Manage Backup codes' } ) ) . toBeVisible ( ) ;
490538 } finally {
491539 first . release ( ) ;
492540 }
493541
494- expect ( await within ( dialog ) . findByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
495- await user . type ( screen . getByLabelText ( 'Password' ) , 'hunter2{Enter}' ) ;
496- await screen . findByRole ( 'list' , { name : 'Backup codes' } ) ;
497- expect ( fapi . mfa . backupCodeCreations ) . toBe ( 1 ) ;
542+ expect ( await screen . findByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
498543 } ) ;
499544
500545 const startSms = async ( user : ReturnType < typeof userEvent . setup > ) => {
@@ -517,6 +562,34 @@ describe('User profile MFA management', () => {
517562 expect ( fapi . mfa . phoneUpdates ) . toContainEqual ( { id : 'phone_1' , reserved : true , default : undefined } ) ;
518563 } ) ;
519564
565+ it ( 'keeps session verification on screen until the SMS reservation refresh completes' , async ( ) => {
566+ await renderMfa ( undefined , mfaEnvironment ( ) , { verification : { secrets : { password : 'hunter2' } } } ) ;
567+ demandReverificationOnce ( '/v1/me/phone_numbers/phone_1' ) ;
568+ const user = userEvent . setup ( ) ;
569+
570+ await startSms ( user ) ;
571+ await user . click ( screen . getByRole ( 'button' , { name : 'Continue' } ) ) ;
572+ expect ( await screen . findByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
573+ await waitFor ( ( ) => expect ( screen . getByLabelText ( 'Password' ) ) . toHaveFocus ( ) ) ;
574+ const refresh = holdRequests ( 'get' , '/v1/me' ) ;
575+ try {
576+ await user . keyboard ( 'hunter2{Enter}' ) ;
577+ await act ( async ( ) => {
578+ await waitFor ( ( ) => expect ( refresh . requests ) . toHaveLength ( 1 ) ) ;
579+ } ) ;
580+ await user . keyboard ( '{Escape}' ) ;
581+ expect ( screen . getByRole ( 'dialog' ) ) . toHaveAttribute ( 'data-open' ) ;
582+ expect ( screen . getByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
583+ expect ( screen . queryByRole ( 'button' , { name : 'Close' } ) ) . toBeNull ( ) ;
584+ expect ( screen . getByRole ( 'button' , { name : 'Continue' } ) ) . toHaveAttribute ( 'aria-busy' , 'true' ) ;
585+ expect ( screen . queryByText ( 'CODE0001' ) ) . toBeNull ( ) ;
586+ } finally {
587+ refresh . release ( ) ;
588+ }
589+
590+ expect ( await screen . findByText ( 'CODE0001' ) ) . toBeVisible ( ) ;
591+ } ) ;
592+
520593 it ( 'keeps SMS code verification open until its reservation challenge can be cancelled' , async ( ) => {
521594 const unverified = fapiPhoneNumber ( { ...phone , verification : { ...phone . verification , status : 'unverified' } } ) ;
522595 const fapi = await renderMfa ( fapiUser ( { id : 'user_1' , phone_numbers : [ unverified ] } ) , mfaEnvironment ( ) , {
@@ -567,6 +640,36 @@ describe('User profile MFA management', () => {
567640 expect ( await screen . findByRole ( 'button' , { name : 'Verify' } ) ) . toBeVisible ( ) ;
568641 } ) ;
569642
643+ it ( 'keeps session verification on screen until the new phone code is sent' , async ( ) => {
644+ const fapi = await renderMfa ( fapiUser ( { id : 'user_1' } ) , mfaEnvironment ( ) , {
645+ verification : { secrets : { password : 'hunter2' } } ,
646+ } ) ;
647+ demandReverificationOnce ( '/v1/me/phone_numbers' ) ;
648+ const user = userEvent . setup ( ) ;
649+
650+ await startSms ( user ) ;
651+ await user . type ( screen . getByRole ( 'textbox' , { name : 'Phone' } ) , '5555550303' ) ;
652+ await user . click ( screen . getByRole ( 'button' , { name : 'Send code' } ) ) ;
653+ expect ( await screen . findByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
654+ await waitFor ( ( ) => expect ( screen . getByLabelText ( 'Password' ) ) . toHaveFocus ( ) ) ;
655+ const prepare = holdRequests ( 'post' , '/v1/me/phone_numbers/:id/prepare_verification' ) ;
656+ try {
657+ await user . keyboard ( 'hunter2{Enter}' ) ;
658+ await act ( async ( ) => {
659+ await waitFor ( ( ) => expect ( prepare . requests ) . toHaveLength ( 1 ) ) ;
660+ } ) ;
661+ expect ( screen . getByText ( 'Enter your current password to continue' ) ) . toBeVisible ( ) ;
662+ expect ( screen . getByRole ( 'button' , { name : 'Continue' } ) ) . toHaveAttribute ( 'aria-busy' , 'true' ) ;
663+ expect ( screen . queryByRole ( 'textbox' , { name : 'Phone' } ) ) . toBeNull ( ) ;
664+ } finally {
665+ prepare . release ( ) ;
666+ }
667+
668+ expect ( await screen . findByRole ( 'textbox' , { name : 'Verification code' } ) ) . toBeVisible ( ) ;
669+ expect ( screen . queryByRole ( 'textbox' , { name : 'Phone' } ) ) . toBeNull ( ) ;
670+ expect ( fapi . mfa . phonePreparations ) . toHaveLength ( 1 ) ;
671+ } ) ;
672+
570673 it ( 'lets the user cancel SMS MFA verification quietly and add the method again' , async ( ) => {
571674 const fapi = await renderMfa ( undefined , mfaEnvironment ( ) , { verification : { secrets : { password : 'hunter2' } } } ) ;
572675 demandReverificationOnce ( '/v1/me/phone_numbers/phone_1' ) ;
0 commit comments