Skip to content

Instantly share code, notes, and snippets.

@thenoseman
Last active July 13, 2020 19:49
Show Gist options
  • Save thenoseman/7a2f8666b967feccb2bfd107634c5ba4 to your computer and use it in GitHub Desktop.
Save thenoseman/7a2f8666b967feccb2bfd107634c5ba4 to your computer and use it in GitHub Desktop.
How to test watchers in vue
it("calls #loadCities if the zipCode is correct and there are 1+ chars in the field", async () => {
// This is how to test watchers:
// 1. set the starting data if neccessary
wrapper.setData({ searchTermCity: "", address: { zipCode: "12345" } });
// 2. Change the current value
wrapper.vm.searchTermCity = "Ha";
// 3. Manually trigger watcher since vue-test-utils doesn't do that anymore:(
wrapper.vm._watchers.find(w => w.expression === "searchTermCity").run();
expect(wrapper.vm.loadCities).toHaveBeenCalledWith("Ha");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment