lundi 27 février 2017

Karma tests undefined promise in redux

I'm writing some tests in Karma for react/redux and I can't seem to fix this bug. Every time a test is ran, it will run through the action, send the correct value to the reducer, but I always seem to get a response return of undefined. We console logged all the values up until updating the state, but once this line executes:

dispatch(duck.addLabRequest(newLab.toJS())).then(() => {

We get a .then of undefined. What could be the root cause of this?

Error

PhantomJS 2.1.1 (Mac OS X 0.0.0) creating a new lab sends the body of the form in the request FAILED
        undefined is not an object (evaluating 'promise.then')

Test

describe('creating a new lab', () => {
  let promise;
  beforeEach(() => {
    let mockAdapter = new MockAdapter(axios);
     mockAdapter.onPost(labsURL).reply(201, {
       lab: newLabWithID,
       message: "Lab created successfully"
     });
     dispatch(duck.addLabRequest(newLab.toJS())).then(() => {
       expect(1).to.equal(2);
     })
  })
})

Action

export function addLabRequest(lab) {
  console.log("lab is: ")
  console.log(JSON.stringify(lab));
  return (dispatch) => {
    axios.post(`${API_URL}/Labs`, lab, {
      headers: {'Content-Type': 'application/json'}
    })
      .then((resData) => {
        console.log("WE GOT HERE");
        console.log(JSON.stringify(resData))
        dispatch(addLab(resData))
      })
      .catch((err) => {
        console.log(err.response.data.message);
      });
  }
}

. . .

Reducer

 case ADD_LAB:

  console.log("ADDING LAB");
  console.log(JSON.stringify(action.payload));
  return state.update(
    'labs',
    (labs) => labs.push(Immutable.fromJS(action.payload))
  );

Aucun commentaire:

Enregistrer un commentaire