我对 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/