How do you test a generator function in Redux-Saga

0 votes
With the help of code can i know How do you test a generator function in Redux-Saga?
9 hours ago in Node-js by Ashutosh
• 27,850 points
9 views

1 answer to this question.

0 votes

Testing a saga means manually stepping through the generator function and asserting that it yields the expected effects in order.

1. Sample Saga to Test

import { call, put } from 'redux-saga/effects';

import { fetchDataApi } from './api';

function* fetchDataSaga(action) {

  try {

    const data = yield call(fetchDataApi, action.payload);

    yield put({ type: 'FETCH_SUCCESS', payload: data });

  } catch (error) {

    yield put({ type: 'FETCH_FAILURE', error: error.message });

  }

}

2. Test Using Jest

import { call, put } from 'redux-saga/effects';

import { fetchDataSaga } from './sagas';

import { fetchDataApi } from './api';

describe('fetchDataSaga', () => {

  const action = { type: 'FETCH_REQUEST', payload: 1 };

  const generator = fetchDataSaga(action);

  it('should call fetchDataApi with correct payload', () => {

    expect(generator.next().value).toEqual(call(fetchDataApi, 1));

  });

  it('should dispatch FETCH_SUCCESS on success', () => {

    const mockResponse = { name: 'Test' };

    expect(generator.next(mockResponse).value).toEqual(

      put({ type: 'FETCH_SUCCESS', payload: mockResponse })

    );

  });

  it('should handle errors with FETCH_FAILURE', () => {

    const errorGen = fetchDataSaga(action);

    errorGen.next(); // skip to call

    const error = new Error('API failed');

    expect(errorGen.throw(error).value).toEqual(

      put({ type: 'FETCH_FAILURE', error: 'API failed' })

    );

  });

});

answered 3 hours ago by anonymous

Related Questions In Node-js

0 votes
1 answer

How do you log content of a JSON object in Node.js?

Hello @kartik, Try this one: console.log("Session: %j", session); If the ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,840 points
1,004 views
0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22 in Node-js by Kavya
81 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22 in Node-js by Kavya
117 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
133 views
0 votes
1 answer

How does Redux middleware handle async actions?

Redux middleware manages asynchronous actions by intercepting ...READ MORE

answered 2 hours ago in Node-js by anonymous
10 views
0 votes
1 answer

How does put() help in dispatching actions in Sagas?

put() is a Redux-Saga effect that allows ...READ MORE

answered 2 hours ago in Node-js by anonymous
10 views
0 votes
1 answer

How do reducers handle async action types in Redux?

Reducers are inherently pure functions, which means ...READ MORE

answered 3 hours ago in Laravel by anonymous
9 views
0 votes
0 answers

How do action creators work with async operations?

Can you tell me How do action ...READ MORE

9 hours ago in Node-js by Ashutosh
• 27,850 points
11 views
0 votes
1 answer

How do you write a generator function in Redux-Saga?

In Redux-Saga, generator functions are used to ...READ MORE

answered 3 hours ago in Node-js by anonymous
9 views
0 votes
1 answer

How do you cancel a Saga task in Redux-Saga?

In Redux-Saga, you can terminate an active ...READ MORE

answered 3 hours ago in Node-js by anonymous
8 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP