@@ -95,6 +95,56 @@ describe('useReverificationWithState', () => {
9595 expect ( result . current [ 1 ] . isActive ) . toBe ( false ) ;
9696 } ) ;
9797
98+ it ( 'cancels an overlapping challenge and preserves the active challenge' , ( ) => {
99+ const { result } = renderHook ( ( ) => useReverificationWithState ( fetcher ) ) ;
100+ const firstComplete = vi . fn ( ) ;
101+ const firstCancel = vi . fn ( ) ;
102+ const secondComplete = vi . fn ( ) ;
103+ const secondCancel = vi . fn ( ) ;
104+
105+ act ( ( ) => {
106+ capturedOnNeeds ?.( { complete : firstComplete , cancel : firstCancel , level : 'first_factor' } ) ;
107+ capturedOnNeeds ?.( { complete : secondComplete , cancel : secondCancel , level : 'multi_factor' } ) ;
108+ } ) ;
109+
110+ expect ( secondCancel ) . toHaveBeenCalledOnce ( ) ;
111+ expect ( result . current [ 1 ] ) . toMatchObject ( { isActive : true , level : 'first_factor' } ) ;
112+
113+ act ( ( ) => {
114+ const [ , state ] = result . current ;
115+ if ( state . isActive ) {
116+ state . complete ( ) ;
117+ }
118+ } ) ;
119+
120+ expect ( firstComplete ) . toHaveBeenCalledOnce ( ) ;
121+ expect ( firstCancel ) . not . toHaveBeenCalled ( ) ;
122+ expect ( secondComplete ) . not . toHaveBeenCalled ( ) ;
123+ } ) ;
124+
125+ it ( 'allows another challenge after the active challenge settles' , ( ) => {
126+ const { result } = renderHook ( ( ) => useReverificationWithState ( fetcher ) ) ;
127+ const firstComplete = vi . fn ( ) ;
128+ const secondCancel = vi . fn ( ) ;
129+
130+ act ( ( ) => {
131+ capturedOnNeeds ?.( { complete : firstComplete , cancel : vi . fn ( ) , level : 'first_factor' } ) ;
132+ } ) ;
133+ act ( ( ) => {
134+ const [ , state ] = result . current ;
135+ if ( state . isActive ) {
136+ state . complete ( ) ;
137+ }
138+ } ) ;
139+ act ( ( ) => {
140+ capturedOnNeeds ?.( { complete : vi . fn ( ) , cancel : secondCancel , level : 'multi_factor' } ) ;
141+ } ) ;
142+
143+ expect ( firstComplete ) . toHaveBeenCalledOnce ( ) ;
144+ expect ( secondCancel ) . not . toHaveBeenCalled ( ) ;
145+ expect ( result . current [ 1 ] ) . toMatchObject ( { isActive : true , level : 'multi_factor' } ) ;
146+ } ) ;
147+
98148 it ( 'cancels when the session changes after reverification opens' , async ( ) => {
99149 const { result, rerender } = renderHook ( ( ) => useReverificationWithState ( fetcher ) ) ;
100150 const cancel = vi . fn ( ) ;
0 commit comments