React native 如何在ComponentDidMount中测试操作创建者触发的存储更新

React native 如何在ComponentDidMount中测试操作创建者触发的存储更新,react-native,react-redux,jestjs,integration-testing,enzyme,React Native,React Redux,Jestjs,Integration Testing,Enzyme,我正在测试一个连接的组件,该组件从bing API获取一个图像,并将其设置为ComponentDidMount上的背景图像。测试非常简单,我使用Ezyme来呈现组件redux模拟存储,以模拟我的redux存储。期望值应该只是检查ImageBackground组件的源属性的uri键的值是否正确设置。下面是组件的外观(我删除了大部分不相关的代码): 这是我的测试,每次检查uri时,我都会得到一个“”而不是我正在使用moxios模拟的预期数据(模拟实际上作为console.log(“data”,dat

我正在测试一个连接的组件,该组件从bing API获取一个图像,并将其设置为
ComponentDidMount
上的背景图像。测试非常简单,我使用Ezyme来呈现组件redux模拟存储,以模拟我的redux存储。期望值应该只是检查
ImageBackground
组件的源属性的uri键的值是否正确设置。下面是组件的外观(我删除了大部分不相关的代码):

这是我的测试,每次检查uri时,我都会得到一个“”而不是我正在使用moxios模拟的预期数据(模拟实际上作为
console.log(“data”,data.data.images[0].url)运行)
实际上是在
fetchimageofdaysecess
中打印它应该打印的内容

let store
const setup = (initialState = {}) => {
  store = makeMockStore(initialState);
  const wrapper = shallow(
    <Provider store={store}>
      <LoginForm/>
    </Provider>).dive()
  return wrapper
}
let initialState
beforeEach(() => {
  initialState = { error: false, loading: false, authentication: { username: "", password: ""}, backgroundImage: null}
})
test('it fetches and displays an ImageBackground', (done) => {
  moxios.install()
  moxios.stubRequest('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1', {
    status: 200,
    response: getImageOftheDayMock
  })

  wrapper = setup(initialState).dive()

  moxios.wait(()=>{
    wrapper.update()
    console.log(store.getState())
    console.log(wrapper.find(ImageBackground).prop('source').uri)
    expect(wrapper.find(ImageBackground).prop('source').uri).toBe('/th?id=OHR.RedRocksArches_EN-GB2044389872_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp')
    done()
    moxios.uninstall()
  })
})

即使调用
wrapper.update(),存储区似乎也从未更新过
。我做错了什么?

你有没有试过让
componentDidMount
异步,并
等待
调用
FetchImageOfDay
?@WillJenkins我认为没有必要,因为我正在使用redux thunk中间件来处理异步操作。功能性实际上在我的应用程序代码中起作用,这是对我的测试我没能做到writte@WillJenkins只是试过了,但没有更好的效果
export const fetchImageOfTheDay = () =>{
  return (dispatch) => {
    axios.get('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1')
      .then(response => fetchImageOfTheDaySuccess(dispatch, response))
      .catch(error => console.log(error));
    };
}
const fetchImageOfTheDaySuccess = (dispatch, data) => {
  console.log("DATA", data.data.images[0].url)
  dispatch({
    type: SET_BACKGROUND_IMAGE,
    payload: data.data.images[0].url
  })
}
let store
const setup = (initialState = {}) => {
  store = makeMockStore(initialState);
  const wrapper = shallow(
    <Provider store={store}>
      <LoginForm/>
    </Provider>).dive()
  return wrapper
}
let initialState
beforeEach(() => {
  initialState = { error: false, loading: false, authentication: { username: "", password: ""}, backgroundImage: null}
})
test('it fetches and displays an ImageBackground', (done) => {
  moxios.install()
  moxios.stubRequest('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1', {
    status: 200,
    response: getImageOftheDayMock
  })

  wrapper = setup(initialState).dive()

  moxios.wait(()=>{
    wrapper.update()
    console.log(store.getState())
    console.log(wrapper.find(ImageBackground).prop('source').uri)
    expect(wrapper.find(ImageBackground).prop('source').uri).toBe('/th?id=OHR.RedRocksArches_EN-GB2044389872_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp')
    done()
    moxios.uninstall()
  })
})
import configureStore from 'redux-mock-store'
import thunk from 'redux-thunk'

const middlewares = [thunk] // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares)
export const makeMockStore = (state = {}) => {
  return mockStore({
    ...state
  })
}