我有以下商店:
import { observable, action } from "mobx";
class UiStore {
@observable string;
constructor() {
this.string = "My string";
}
@action
setString = value => {
this.string = value;
};
}
export default UiStore;
我 Jest 运行了一个简单的测试:
import UiStore from "./UiStore";
describe("uiStore", () => {
it("setString sets string to the passed value", () => {
const uiStore = new UiStore();
uiStore.setString("blah");
expect(uiStore.string).toBe("blah");
});
});
但是我收到以下错误:
TypeError: uiStore.setString is not a function
当我删除 @action
装饰器时,测试将通过。但是根据mobx docs ,当函数修改状态时,@action
装饰器显着提高了性能。有人知道测试 mobx @actions 的方法吗?
最佳答案
不使用箭头函数解决了这个问题:
import { observable, action } from "mobx";
class UiStore {
@observable string;
constructor() {
this.string = "My string";
}
@action
setString(value) {
this.string = value;
}
}
export default UiStore;
不知道为什么...
关于javascript - 开 Jest - 测试 mobx store @action TypeError : is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46621580/