Reactjs Mobx+时的单元测试用例;RootStore&x2B;useContext钩子

Reactjs Mobx+时的单元测试用例;RootStore&x2B;useContext钩子,reactjs,typescript,mobx,mobx-react,Reactjs,Typescript,Mobx,Mobx React,当从useContext()钩子使用mobx存储时,编写单元测试用例的最佳方法是什么?我们应该使用酶还是反应测试库?此处未使用提供程序或@inject() 店铺设计: //StoreA export class StoreA { rootStore: RootStore; constructor(rootStore: RootStore) { this.rootStore = rootStore; } //StoreA implementations... } //S

当从useContext()钩子使用mobx存储时,编写单元测试用例的最佳方法是什么?我们应该使用酶还是反应测试库?此处未使用提供程序或@inject()

店铺设计:

//StoreA
export class StoreA {
  rootStore: RootStore;
  constructor(rootStore: RootStore) {
    this.rootStore = rootStore;
  }
  //StoreA implementations...
}

 //StoreB
export class StoreB {
  rootStore: RootStore;
  constructor(rootStore: RootStore) {
    this.rootStore = rootStore;
  }
  //StoreB implementations...
}

//RootStore
export class RootStore {
  StoreA: StoreA;
  StoreB: StoreB;
  constructor() {
    this.storeA = new storeA(this);
    this.storeB = new storeB(this);
  }
}

export const RootStoreContext = createContext(new RootStore());
在组件存储中使用useContext钩子

const SomeComponent = ({}) => {
  const rootStore = useContext(RootStoreContext);
  const { storeAdata, storeAmethods } = rootStore.storeA;
  //Component code....
}
Stackblitz链接:

在下面的场景中

  • 如何仅为单个存储编写单元测试用例(其具有循环依赖性)
  • 如果使用react测试库,如何模拟存储和使用上下文
  • 可以用酶吗
  • 哪些库适合编写UTC

  • 如果你只是单元测试商店,不要使用组件。直接实例化存储并测试它们

    如果您正在测试组件交互(集成),那么我建议您使用react测试库。创建上下文提供程序,并为每个测试使用提供程序包装组件

    更多信息: