Заголовок авторизации не проверяется - PullRequest
0 голосов
/ 14 мая 2018

thunk, который делает асинхронный вызов

buyerAction = (data = {}, cb) => (dispatch) => {
  console.log('x-access-token', authService.getAccessToken());
  axios({
    method: 'POST',
    url: `http://localhost:3001/api/manageUsers`,
    headers: {
      'x-access-token': authService.getAccessToken()
    },
    data
  }).then((res) => {
    if (res.status === 200 && res.data) {
      dispatch({ type: 'buyer_created', data: res.data.message });
      if (data.role === 'buyer') {
        axios({
          method: 'POST',
          url: `http://localhost:3001/api/populateBuyerLookUp`,
          headers: {
            'x-access-token': authService.getAccessToken()
          },
          data
        }).then((response) => {
          console.log(`${response} lookup is created`);
        }).catch((err) => { console.log(err); });
      }
      cb(res.data.message);
    }
  })
    .catch((err) => { console.log('_+_+_+', err); });
};

пытается проверить:

const middleware = [thunk];
const mockStore = configureMockStore(middleware);

describe('Async Actions', () => {
  const host = 'http://localhost';

  axios.defaults.host = host;
  axios.defaults.adapter = httpAdapter;

  afterEach(() => {
    nock.cleanAll();
  });

  it('should create Async and load data', (done) => {
    nock('http://localhost:3001', {
      reqheaders: {
        'x-access-token': 'authtoken'
      }
    })
      .post('/api/manageUsers')
      .reply(200, { body: { name: 'prakash' } });

    const store = mockStore({}, [{ type: 'buyer_created', data: 'message' }]);

    store.dispatch(buyerAction(undefined, () => {}));

    done();
  });
});

Ошибка: x-access-token не определен Ошибка типа [ERR_HTTP_INVALID_HEADER_VALUE]: недопустимое значение «undefined» для заголовка «x-access-token»

Я пытаюсь проверить thunk с помощью nock, есть ли лучший и простой подход для проверки thunk и mock header?

...