Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Jest test function returning a function which is having window.location.href

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' )
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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' )
} );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!