如何使用带有 Jest 的 Mobx 商店测试组件
How to test components using Mobx stores with Jest
我正在尝试使用带有 Jest 和 React-testing-library 的 Mobx 存储来测试我的 React 组件。
问题是我不知道如何为测试注入我的商店。
这是我的简化代码。
StaffInfo.js(组件)
import React, { useState } from "react";
import { observer, inject } from "mobx-react";
const StaffInfo = props => {
const store = props.instituteStore;
const [staffs, setStaffs] = useState(store.staffs);
return (
<div>
....
</div>
);
}
export default inject(rootStore => ({
instituteStore : rootStore.instituteStore
}))(observer(StaffInfo));
index.js(根存储)
import LoginStore from "./LoginStore";
import InstituteStore from "./InstituteStore";
class RootStore {
constructor(){
this.loginStore = new LoginStore (this);
this.instituteStore = new InstituteStore(this);
}
}
export default RootStore;
InstituteStore.js(目标店铺)
import { observable, action } from "mobx";
class InstituteStore {
constructor(root){
this.root = root;
}
@observable
staffs = [];
}
export default InstituteStore;
StaffInfo.test.js(测试文件)
import React from "react";
import ReactDom from "react-dom";
import { MemoryRouter } from "react-router-dom";
import { Provider } from "mobx-react";
import StaffInfo from "./StaffInfo";
import InstituteStore from "../stores/InstituteStore";
describe("Staff Component testing", () => {
test("should be rendered without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(
<MemoryRouter initialEntries={["/staff"]}>
<StaffInfo instituteStore={RootStore.instituteStore} />
</MemoryRouter>,
div
);
ReactDOM.unmountComponentAtNode(div);
});
});
一旦运行这个测试文件,错误信息就像:
TypeError : Cannot read property 'staffs' of undefined
请告诉我代码的哪些部分是错误的。
提前致谢!
Mobx-react
的Inject
用于向深层子组件插入存储。这些星星由基于上下文的 API Provider
提供。
所以无论你在哪里为子组件提供商店,都使用类似的东西。
import rootStore from 'path_to_rootStore'
<Provider rootStore={rootStore}>
...
...
<App/>
...
...
<.Provider>
感谢@uneet7:
传奇!终于有人给出了一个明智的答案:D
这就是我的组件的样子
@inject('routing', 'navigationStore')
@observer
export default class PageTitle extends React.Component<*> {...}
这就是我的工作方式:
let view = mount(
<Provider {...getStores()}>
<UserPage notificationStore={notificationStore} routing={routing} />
</Provider>
);
所以 UserPage 有组件(很多),其中一个组件有 PageTitle 组件。显然 PageTitle 上有 @inject。没关系,因为 Provider HOC 将通过 inject
函数向组件道具提供商店。
我正在尝试使用带有 Jest 和 React-testing-library 的 Mobx 存储来测试我的 React 组件。
问题是我不知道如何为测试注入我的商店。
这是我的简化代码。
StaffInfo.js(组件)
import React, { useState } from "react";
import { observer, inject } from "mobx-react";
const StaffInfo = props => {
const store = props.instituteStore;
const [staffs, setStaffs] = useState(store.staffs);
return (
<div>
....
</div>
);
}
export default inject(rootStore => ({
instituteStore : rootStore.instituteStore
}))(observer(StaffInfo));
index.js(根存储)
import LoginStore from "./LoginStore";
import InstituteStore from "./InstituteStore";
class RootStore {
constructor(){
this.loginStore = new LoginStore (this);
this.instituteStore = new InstituteStore(this);
}
}
export default RootStore;
InstituteStore.js(目标店铺)
import { observable, action } from "mobx";
class InstituteStore {
constructor(root){
this.root = root;
}
@observable
staffs = [];
}
export default InstituteStore;
StaffInfo.test.js(测试文件)
import React from "react";
import ReactDom from "react-dom";
import { MemoryRouter } from "react-router-dom";
import { Provider } from "mobx-react";
import StaffInfo from "./StaffInfo";
import InstituteStore from "../stores/InstituteStore";
describe("Staff Component testing", () => {
test("should be rendered without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(
<MemoryRouter initialEntries={["/staff"]}>
<StaffInfo instituteStore={RootStore.instituteStore} />
</MemoryRouter>,
div
);
ReactDOM.unmountComponentAtNode(div);
});
});
一旦运行这个测试文件,错误信息就像:
TypeError : Cannot read property 'staffs' of undefined
请告诉我代码的哪些部分是错误的。 提前致谢!
Mobx-react
的Inject
用于向深层子组件插入存储。这些星星由基于上下文的 API Provider
提供。
所以无论你在哪里为子组件提供商店,都使用类似的东西。
import rootStore from 'path_to_rootStore'
<Provider rootStore={rootStore}>
...
...
<App/>
...
...
<.Provider>
感谢@uneet7:
传奇!终于有人给出了一个明智的答案:D 这就是我的组件的样子
@inject('routing', 'navigationStore')
@observer
export default class PageTitle extends React.Component<*> {...}
这就是我的工作方式:
let view = mount(
<Provider {...getStores()}>
<UserPage notificationStore={notificationStore} routing={routing} />
</Provider>
);
所以 UserPage 有组件(很多),其中一个组件有 PageTitle 组件。显然 PageTitle 上有 @inject。没关系,因为 Provider HOC 将通过 inject
函数向组件道具提供商店。