Answer the question
In order to leave comments, you need to log in
How to write an asynchronous jest test for action // mobx?
The action itself
addItem = text => {
axios.post("/task", {
headers: { "Content-Type": "application/json" },
text: text
})
.then(res => {
this.arr.push(res.data.task);
})
.catch(e => console.log(e));
};
describe("STORE", () => {
it("create new task", () => {
const store = new Store();
const text = "test";
store.addItem(text).then( () => {
return expect(store.arr.length).toBe(1);
})
});
});
Answer the question
In order to leave comments, you need to log in
Firstly, addItem does not return anything, naturally, you cannot call the then method on anything (undefined), since undefined, in principle, cannot have methods. You need to add a return:
addItem = text => {
return axios.post("/task", {
headers: { "Content-Type": "application/json" },
text: text
})
.then(res => {
this.arr.push(res.data.task);
})
.catch(e => console.log(e));
};
describe("STORE", () => {
it("create new task", () => {
const store = new Store();
const text = "test";
expect(store.addItem(text)).resolves.toBe(1);
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question