Reputation: 355
I am trying to implement a test for redux saga as follows but I ran into a problem. The error I get is cannot read property payload of undefined. The 'message' var that I passed to the saga function is undefined for some reason, can anyone tell me why? Thanks
saga.spec.js
import test from 'tape'
import { put,take,call } from 'redux-saga/effects'
import { onCreateMessage } from '../src/common/sagas/messages'
import { addMessage,createMessage } from '../src/common/reducers/messages'
test('createMessage', assert => {
const gen = onCreateMessage()
var message = {
id: 1234,
channelID: "AA",
text: "text",
user: "user"
}
assert.deepEqual(
gen.next(message).value,
put(addMessage(message)),
'createMessage should dispatch addMessage action'
)
})
saga/index.js
export default function* rootSaga(){
yield [
takeEvery('CREATE_MESSAGE', onCreateMessage),
......
]
When I console logged message below I get 'undefined'
export function* onCreateMessage(message) {
console.log(message)
yield put(addMessage(message.payload))
try {
yield call(fetch,'/api/newmessage',
{
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(message.payload)}
)
} catch (error){
throw error
}
}
I am using actionCreator from redux-actions:
export const addMessage = createAction(ADD_MESSAGE);
Upvotes: 1
Views: 1086
Reputation: 1891
change
const gen = onCreateMesaage()
to
const gen = onCreateMessage(message)
Explanation:
message
is the arguments passed to generator function rather than the yield
result.
Previous code works for yielded result. e.g
export function* onCreateMessage() {
const message = yield
console.log(message)
yield put(addMessage(message.payload))
}
Upvotes: 3