Last active
September 5, 2018 18:14
-
-
Save johnmarinelli/b8b80676a275d2d89044a2aaf7bbbea0 to your computer and use it in GitHub Desktop.
redux-saga call() not working?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { put, call, takeEvery } from 'redux-saga/effects' | |
import * as Actions from '@actions/Artsy' | |
export function* fetchData(client, artworkId) { | |
yield call(Actions.artsyFetchStart) | |
try { | |
const artwork = yield call(client.getData, { id: artworkId }) // this returns undefined | |
yield call(Actions.artsyFetchSuccess) | |
yield call(Actions.recieveArtwork, artwork) | |
return artwork | |
} catch (error) { | |
yield call(Actions.artsyFetchFailure) | |
} | |
} | |
export function* watchFetchArtwork() { | |
yield takeEvery(Actions.ARTSY_FETCH_ARTWORK, fetchArtwork) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fetchData } from './saga' | |
describe('Saga', () => { | |
const mockClient = { | |
getData: ({ id }) => | |
new Promise(res => { | |
res({ id }) | |
return { id } | |
}), | |
} | |
describe('fetchData', () => { | |
it('should call getData', () => { | |
const gen = fetchData(mockClient, 1) | |
expect(gen.next().value).toEqual(call(artsyFetchStart)) // this works | |
expect(gen.next().value).toEqual(call(mockClient.getData, { id: 1 })) // this works | |
expect(gen.next().value).toEqual(call(artsyFetchSuccess)) // this works | |
expect(gen.next().value).toEqual(call(recieveArtwork, { id: 1 })) // this fails, says argument is called with `undefined` | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment