【问题标题】:React testing use mockstore with custom store to test itReact 测试使用 mockstore 和自定义 store 来测试它
【发布时间】:2021-04-21 06:36:05
【问题描述】:

我想测试在我的项目中使用 redux 存储的组件。这是我用来获取一些商店元素的函数:

const variable = store.getState().serverConfiguration.testValue;

当我测试这个组件时,我使用mockStore 来执行此操作,所以我的商店是空的,对吧?这就是为什么我说TypeError: Cannot read property 'testValue' of undefined

有没有办法将我的自定义存储传递给mockStore,以便可以正确测试它? 我试图这样做,但它不起作用:

const store = configureMockStore()({
    serverConfiguration: {
       testValue: "Admin"
    }
});

【问题讨论】:

    标签: reactjs testing jestjs enzyme


    【解决方案1】:

    试试这个:

    import configureStore from "redux-mock-store"
    
    const mockStore = configureStore([]);
    
    const store = mockStore({
        serverConfiguration: {
            testValue: "Admin"
        }
    })
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2022-08-18
      • 2021-04-24
      • 2015-07-19
      • 1970-01-01
      • 2019-03-09
      • 2017-12-11
      • 2021-04-26
      • 2020-03-24
      相关资源
      最近更新 更多