@@ -2,12 +2,13 @@ import React from 'react';
22import { render , waitFor , act } from '@testing-library/react' ;
33import '@testing-library/jest-dom' ;
44import { AuthKitProvider , useAuth } from '../src/components/authkit-provider.js' ;
5- import { checkSessionAction , getAuthAction , refreshAuthAction } from '../src/actions.js' ;
5+ import { checkSessionAction , getAuthAction , refreshAuthAction , handleSignOutAction } from '../src/actions.js' ;
66
77jest . mock ( '../src/actions' , ( ) => ( {
88 checkSessionAction : jest . fn ( ) ,
99 getAuthAction : jest . fn ( ) ,
1010 refreshAuthAction : jest . fn ( ) ,
11+ handleSignOutAction : jest . fn ( ) ,
1112} ) ) ;
1213
1314describe ( 'AuthKitProvider' , ( ) => {
@@ -331,7 +332,7 @@ describe('useAuth', () => {
331332 } ) ;
332333 } ) ;
333334
334- it ( 'should receive an error when refreshAuth fails with an errstringor ' , async ( ) => {
335+ it ( 'should receive an error when refreshAuth fails with a string error ' , async ( ) => {
335336 ( refreshAuthAction as jest . Mock ) . mockRejectedValueOnce ( 'Refresh failed' ) ;
336337
337338 let error : string | undefined ;
@@ -367,4 +368,56 @@ describe('useAuth', () => {
367368 expect ( error ) . toBe ( 'Refresh failed' ) ;
368369 } ) ;
369370 } ) ;
371+
372+ it ( 'should call handleSignOutAction when signOut is called' , async ( ) => {
373+ ( handleSignOutAction as jest . Mock ) . mockResolvedValueOnce ( { } ) ;
374+
375+ const TestComponent = ( ) => {
376+ const auth = useAuth ( ) ;
377+ return (
378+ < div >
379+ < div data-testid = "session" > { auth . sessionId } </ div >
380+ < button onClick = { ( ) => auth . signOut ( ) } > Sign out</ button >
381+ </ div >
382+ ) ;
383+ } ;
384+
385+ const { getByRole } = render (
386+ < AuthKitProvider >
387+ < TestComponent />
388+ </ AuthKitProvider > ,
389+ ) ;
390+
391+ await act ( async ( ) => {
392+ getByRole ( 'button' ) . click ( ) ;
393+ } ) ;
394+
395+ expect ( handleSignOutAction ) . toHaveBeenCalled ( ) ;
396+ } ) ;
397+
398+ it ( 'should pass returnTo parameter to handleSignOutAction' , async ( ) => {
399+ ( handleSignOutAction as jest . Mock ) . mockResolvedValueOnce ( { } ) ;
400+
401+ const TestComponent = ( ) => {
402+ const auth = useAuth ( ) ;
403+ return (
404+ < div >
405+ < div data-testid = "session" > { auth . sessionId } </ div >
406+ < button onClick = { ( ) => auth . signOut ( { returnTo : '/home' } ) } > Sign out</ button >
407+ </ div >
408+ ) ;
409+ } ;
410+
411+ const { getByRole } = render (
412+ < AuthKitProvider >
413+ < TestComponent />
414+ </ AuthKitProvider > ,
415+ ) ;
416+
417+ await act ( async ( ) => {
418+ getByRole ( 'button' ) . click ( ) ;
419+ } ) ;
420+
421+ expect ( handleSignOutAction ) . toHaveBeenCalledWith ( { returnTo : '/home' } ) ;
422+ } ) ;
370423} ) ;
0 commit comments