Reputation: 1579
I have a saga:
export function* loadData() {
const requestURL = 'http://www.web.address/thing';
try {
const data = yield call(getRequest, requestURL);
yield put(dataLoaded(data));
} catch (err) {
yield put(dataError(err));
}
}
I'm trying to test it like this:
describe('loadData saga', () => {
describe('200 response', () => {
const getReq = jest.fn();
const requestURL = 'mock.url.com';
const generator = cloneableGenerator(loadData)();
it('sends the request to the correct url', () => {
expect(generator.next(getReq, requestURL).value).toEqual(call(getReq, requestURL));
});
});
This test fails because the generator doesn't seem to heed what I pass into the next function, and I don't know why. That is to say, the test receives http://www.web.address/thing
as the url, and the getRequest
function, instead of what I've tried to pass in in the .next()
function.
This also did not work:
generator.next(getReq(requestURL))
What am I misunderstanding?
Upvotes: 1
Views: 1887
Reputation: 4512
You you shouldn't actually be passing any args into next() here, as your saga has no previous yields and doesn't pass any dynamic data from previous yield's to the call
- it has everything it needs already in scope
So in your test you would check next().value is as expected using the non-mocked values
import {getRequest} from 'same/place/as/saga/loads/getRequest';
const requestURL = 'http://www.web.address/thing';
expect(generator.next().value).toEqual(call(getRequest, requestURL));
The problem you have is you are attempting to mock and pass in stubs which the saga has no way of injecting them in this case
Remember that a yield
will stop execution of the generator until the next iteration so you are not actually running a request here, only stopping and receiving back an object of instructions of how to call getRequest
with what args to pass.
Using .next(someData)
after your above test will however allow you to pass in mock data as a simulation of the response of your request
const mockData = {};
expect(generator.next(mockData).value).toEqual(put(dataLoaded(mockData)))
Think of args to next() as allowing mocking the return value of the previous yield
Upvotes: 3