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

88
Views
How to test if inside a function in React Native Background Timer Jest

Hei, I;m new at react native testing, can someone help me solving this test. I'm never tried this before so im confuse to solve this function test, here's the code :

export const startTimer = (setSecondLeft: Function): void => {
  reactNativeBackgroundTimer.runBackgroundTimer(() => {
    setSecondLeft(secs => {
      if (secs > 0) {
        return secs - 1;
      } else if (secs === 0) {
        reactNativeBackgroundTimer.stopBackgroundTimer();
      }
    });
  }, 1000);
};

This code is use to running a timer, and for every 1 second, the timer will decrease (secs-1) from react native background timer.

my test code :

    import reactNativeBackgroundTimer from 'react-native-background-timer';
    
    jest.mock('react-native-background-timer',() =>{
      return{
        runBackgroundTimer: jest.fn(),
        stopBackgroundTimer : jest.fn()
      }
    })
    

    const runTimer = jest.spyOn(reactNativeBackgroundTimer,'runBackgroundTimer')

    describe('Timer Handler Unit Test',()=>{
        const setSecondLeft = jest.fn()
        it('Should be called',()=>{
          startTimer(setSecondLeft)
          expect(runTimer).toBeCalled()
        })
        it('Should return secs - 1',()=>{
          const secs = 10
          runTimer()
          expect(startTimer(setSecondLeft)).toBe(9)
        })
      })

can someone help me for this help. I'm really appreciate the help, thankyou so much!

about 4 years ago · Juan Pablo Isaza
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!