从“ isomorphic-fetch”导入获取与用于测试异步操作的导入“ isomorphic-fetch”之间的区别

时间:2019-01-28 17:06:11

标签: redux fetch isomorphic-fetch-api redux-mock-store

我正在为异步redux动作编写单元测试,但在fetch调用中收到一个错误,抱怨单元测试中的相对URL。我可以通过将import fetch from isomorphic-fetch'更改为import 'isomorphic-fetch'来解决此问题。但是,我无法弄清楚这两行之间的区别以及为什么它可以解决此问题。我在这里先向您的帮助表示感谢。

错误:

only absolute urls are supported

      27 | 
      28 | const request = (url, options) => (
    > 29 |   fetch(url, options)
         |   ^
      30 |     .then(handlePromise)
      31 |     .then(handleErrors)
      32 | );

测试用例:

describe('async actions', () => {
  afterEach(() => {
    fetchMock.restore();
  });

  it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
    fetchMock.postOnce('web-api/v1/collection/search', {
      body: { todos: ['do something'] },
      headers: { 'content-type': 'application/json' }
    });

    const expectedActions = [
      { type: types.RECEIVE_SEARCH }
    ];
    const store = mockStore({ todos: [] });

    return store.dispatch(actions.testFetch({})).then(() => {
      // return of async actions
      expect(store.getActions()).toEqual(expectedActions);
    });
  });
}); 

0 个答案:

没有答案