0

I am testing react native app using jest and react native testing library which uses redux saga for api calls.Below is my generators and watchers.

//dispatch action
 deleteStock: stock => {
    console.log(stock); 
    dispatch({type: DELETE_STOCK.REQUEST, payload: stock});
  }

The above function is called from the component while testing as console log prints the stock but logs are not getting printed in generator function which implies that this generator function is not calling. But the app works perfectly!

yield takeLatest(DELETE_STOCK.REQUEST, deleteStockFromWatchlist);

function* deleteStockFromWatchlist(stock) {
 console.log('stock before delete', stock);
 try {
   const res = yield call(
     request,
     REMOVE_SYMBOL,
     DELETE,
     {},
     {},
     stock.payload,
     true,
   );
   console.log('stock after delete', res);
   if (res?.success) {
     yield put({type: DELETE_STOCK.SUCCESS, data: stock?.payload});
   } else {
     yield put({type: DELETE_STOCK.FAILED});
   }
 } catch (error) {
   yield put({type: DELETE_STOCK.FAILED});
 }
}

My test case :

 test('stock removes on deleting', async () => {
    fireEvent.press(screen.getByRole('button', {name: 'delete INFY'}));
    axios.mockResolvedValueOnce({status: 200, data: 'stock deleted'});
    await screen.findByRole('tab', {name: 'INFY'});
})
  • Please provide a https://stackoverflow.com/help/minimal-reproducible-example. We need runnable code – Lin Du Nov 04 '22 at 03:24

0 Answers0