【发布时间】:2021-03-16 12:17:38
【问题描述】:
我正在努力模拟列表组件中的删除功能。
我现在的测试是这样的
describe("delete a todo", () => {
test("should have todo removed", async () => {
const deleteItem = jest.fn();
const items = [{ id: 1, name: "ana", isComplete: false }];
const wrapper = shallowMount(Todo, items);
console.log(wrapper);
const deleteButton = ".delete";
wrapper.find(deleteButton).trigger("click");
expect(deleteItem).toHaveBeenCalledWith("1");
});
目前,当我运行测试时,读取的错误。 Test Error
应用程序运行良好,但我没有在测试中正确模拟删除功能,因为“新注释”仍在通过。我做错了什么?
以防万一,这是我正在测试的文件的一部分。
methods: {
addItem() {
if (this.newItem.trim() != "") {
this.items.unshift({
// id: createUID(10),
id: uuid.v4(),
completed: false,
name: this.newItem
});
this.newItem = "";
localStorage.setItem("list", JSON.stringify(this.items));
this.itemsLeft = this.itemsFiltered.length;
}
},
removeItem(item) {
const itemIndex = this.items.indexOf(item);
this.items.splice(itemIndex, 1);
localStorage.setItem("list", JSON.stringify(this.items));
this.itemsLeft = this.itemsFiltered.length;
},
还可以从以下链接获取更多代码: https://github.com/oliseulean/ToDoApp-VueJS
【问题讨论】:
标签: javascript vue.js unit-testing jestjs vue-component