unit-testing - 难以对注入(inject)多个存储的 MobX 观察者组件进行单元测试

标签 unit-testing reactjs enzyme mobx

我对 MobX 注入(inject)装饰器的理解是,使用 Enzyme,我应该能够简单地在单元测试中初始化一个存储,然后作为 prop 传递给我正在安装的组件。 [src:https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest 并滚动到集成测试部分。]但我一直收到商店不可用!错误。这往往是一个问题,特别是如果我要注入(inject)多个商店。

所以在我的组件中:

export default inject('errorStore', 'someOtherStore', 'andTheThirdStore')(observer(MyComponent));

我的测试应该是这样的。

import errorStore from './stores/errorStore';
import someOtherStore from './stores/someOtherStore';
import andTheThirdStore from './stores/andTheThirdStore';
import Component from './components/Component';

describe('My Component', () => {
  someOtherStore.initializeWithData('./examples','TEST-123-45678-90', 'USERID');
  andTheThirdStore.initialize();
  const storeProp = { errorStore, someOtherStore, andTheThirdStore };

  beforeEach(() => {
      const wrapper = mount(<Component {...storeProp} />
  }

  it ('does all the things', () => {...});

我是否需要其他类型的提供商,或者我只是错过了一些明显的东西?

最佳答案

据我所知,您的设置是正确的。您有可重现的设置吗?请注意,您还可以使用 wrappedComponent 直接挂载原始组件,另请参阅 this issue 中的示例。

关于unit-testing - 难以对注入(inject)多个存储的 MobX 观察者组件进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980793/

相关文章:

ios - 集成 Firebase SDK 后无法运行单元测试(Xcode 8GM,swift 3)

javascript - 如何使用 React Hooks 执行错误边界

javascript - React Starter Kit 中路由和组件的作用

reactjs - 使用 Jest/Enzyme 测试 Chart.js - 无法创建图表 : can't acquire context from the given item

react-native - 如何将 jest 特定设置添加到 create-react-native-app?

c# - 如何测试在启动期间设置的 Polly 重试策略?

c# - Nsubstitute,改变被替代的属性的值

c++ - 使用 GTest 进行回调单元测试

javascript - react : input validation

reactjs - 使用 Jest 和 Enzyme 进行 react 测试 @react-google-maps/api 返回 TypeError : Cannot read property 'maps' of undefined