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!