jeudi 28 mai 2020

How to test a Redux-Saga API GET request which sends data to the API

Saga Code, works fine, it shows a list of restaurants based on the city you input.

export function* fetchRestaurantsAsync({ cityName }) {
  try {
    const data = yield call(fetchRestaurants,cityName);
    yield put(receiveRestaurants(data));
  } catch (e) {
    yield put(requestFailed(e));
  }
}

export default function* watchFetchRestaurant() {
  yield takeLatest(RESTAURANTS_REQUEST, fetchRestaurantsAsync);
}

Redux-Saga Test File

import fetchRestaurants from "api";
import { runSaga } from 'redux-saga';
import {fetchRestaurantsAsync} from 'sagas'

test("should load restaurants in case of success", async () => {

 const dispatchedActions = [];

  const fakeStore = {
    getState: () => ({cityName:'Toronto'}),
    dispatch: (action) => dispatchedActions.push(action),
  };

  await runSaga(fakeStore, fetchRestaurantsAsync).done;
  console.log(dispatchedActions)
});

This is my test error


    TypeError: Cannot destructure property 'cityName' of 'undefined' as it is undefined.

      4 | import fetchRestaurants from "api";
      5 | 
    > 6 | export function* fetchRestaurantsAsync({ cityName }) {
        |                                          ^
      7 |   try {
      8 |     const data = yield call(fetchRestaurants,cityName);
      9 |     yield put(receiveRestaurants(data));

      at fetchRestaurantsAsync (src/sagas/index.js:6:42)

all the examples I have found don't show how to pass in the data, in this case the name of the city. They are just fetch requests that don't manipulate the URL. I am passing in the city name at the end of the url.

Aucun commentaire:

Enregistrer un commentaire