Axios Mock Adapter在调度前等待超时?

时间:2017-06-14 10:31:15

标签: unit-testing redux redux-thunk

在我的React Redux应用程序中,我尝试对其中包含settimeout的函数进行一些测试

// MyFunc.js
export function updateSomething(id, data) {
  return (dispatch, getState) => {
    dispatch({type: 'dispatch1'})
    settimeout(() => {
      axios.get('/data')
      .then((res) => {
        dispatch({type: 'timeoutted_dispatch', data: res.data})
      })
      .catch(error => {
        dispatch({type: 'dispatch_error'});
      })
    },3000);

    dispatch({type: 'dispatch_end'});
  }
}

// MyTest.js
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';;
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';

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

describe('Lets do some tests', () => {
  it('Should dispatch timeoutted function', () => {
     const store = mockStore({});
     
     // Get all the dispatches for testing purpose
     store.dispatch(updateSomething(1, {test: 'test'}))
     .then(() => {
       const actionList = store.getAction();
       expect(actionList).toEqual(allDispatches);
       // At this point, I won't be able to get anything inside timeout function
     })
  });
})

如何设置某种Await,以便在尝试检索数据之前等待超时?

0 个答案:

没有答案