0
我有以下商店:笑話 - 測試mobx店@action類型錯誤:不是一個函數
import { observable, action } from "mobx";
class UiStore {
@observable string;
constructor() {
this.string = "My string";
}
@action
setString = value => {
this.string = value;
};
}
export default UiStore;
和一個簡單的測試,我用開玩笑運行:
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的方法?
哦...裝飾與類方法的工作。你以前曾經有過一個班級領域(財產,或者這些天被稱爲w.e.) – MinusFour
'setString = value => {this.string = value; };在你的問題中的代碼將導致一個綁定函數。你可能需要'@action.bound setString(value){this.string = value; }' – Tholle