How to write test case for below function ? Expected is : If successPath is provided then onSignInSuccess should redirect it to successPath
export const onSignInSuccess = ( data ) => {
return ( ) => {
global.location.href = data?.detail?.data?.successPath;
}
}
What I tried so far is but its not working
const data = { detail : { data: { redirectPage: true, successPath: 'test.com' } } }
onSignInSuccess( data )()
expect( jest.fn() ).toHaveBeenCalledWith( 'test.com' )
I found the solution by mocking window.location.href
it( 'should redirect to successPath if given', ()=>{
global.window = Object.create( window );
const url = 'http://test.com';
Object.defineProperty( window, 'location', {
value: {
href: url
}
} );
const data = { detail : { data: { redirectPage: true, successPath: 'test.com' } } }
onSignInSuccess( data )()
expect( global.location.href ).toBe( 'test.com' )
} );