vendredi 22 décembre 2017

Using nock to test POST request in async action creator

I am using nock to mock my asynchronous thunk action creators. Everything works as expected for GET requests, but I haven't been able to make a POST request work. I have read everything I can find online but nothing fixed my problem. I can see the response body in the failed test run. The issue is that there is no nock match for the url. Can anyone spot the problem?

Test file:

describe('#saveReminder', () => {
    it(`creates ${constants.actions.REQUEST_SAVE_REMINDER}
      and ${constants.actions.RECEIVE_SAVE_REMINDER}`, () => {

      data = { payload: 'payload' };

      nock(constants.paths.API_AUTHORITY)
        .post('api/prospect/add-reminder', {
          prospect: 1,
          reminder_datetime: '2017-12-22T18:42:00.000Z',
          description: 'description',
        })
        .reply(200, { data } )

      const expectedActions = [
        { type: constants.actions.REQUEST_SAVE_REMINDER },
        { type: constants.actions.RECEIVE_SAVE_REMINDER, data }
      ]

      return store.dispatch(actions.saveReminder({
        id: 1,
        description: 'description',
        date: moment(),
        time: moment(),
      })).then(() => {
        expect(store.getActions()).toEqual(expectedActions)
      })
    })
  })

Async action:

export function saveReminder({ id, description, date, time }) {
  return (dispatch) => {
    requestSaveReminder();
    return fetch(`${constants.paths.API_AUTHORITY}api/prospect/add-reminder`, {
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'X-Csrf-Token': Cookie.get('X-Csrf-Token'),
      },
      credentials: 'same-origin',
      method: 'POST',
      body: JSON.stringify(
        {
          prospect: id,
          reminder_datetime: moment(`${date.format('MM/DD/YYYY')} ${time.format('HH:mm')}`, 'MM/DD/YYYY HH:mm'),
          description,
        }
      ),
    }).then(response => {
      response.json();
    })
      .then(json => dispatch(receiveSaveReminder(json.data)))
      .catch(ex => dispatch(requestSaveReminderFailure(ex)));
  };
}

Test Failure:

Object {
    -     "type": "REQUEST_SAVE_REMINDER",
    +     "data": [FetchError: request to http://localhost:8080/api/prospect/add-reminder failed, reason: Nock: No match for request {
    +   "method": "POST",
    +   "url": "http://localhost:8080/api/prospect/add-reminder",
    +   "headers": {
    +     "accept": [
    +       "application/json"
    +     ],
    +     "content-type": [
    +       "application/json"
    +     ],
    +     "accept-encoding": [
    +       "gzip,deflate"
    +     ],
    +     "user-agent": [
    +       "node-fetch/1.0 (+http://ift.tt/18q0Hew)"
    +     ],
    +     "connection": [
    +       "close"
    +     ],
    +     "content-length": [
    +       89
    +     ]
    +   },
    +   "body": "{\"prospect\":1,\"reminder_datetime\":\"2017-12-22T18:56:00.000Z\",\"description\":\"description\"}"
    + }],
    +     "type": "REQUEST_SAVE_REMINDER_FAILURE",
        },
    -   Object {
    -     "data": Object {
    -       "payload": "payload",
    -     },
    -     "type": "RECEIVE_SAVE_REMINDER",
    -   },

The urls appear to match. Why does it say Nock: No match for request? Thanks!

Aucun commentaire:

Enregistrer un commentaire