joshuajr's avatar

How to test a vue component that has methods that do ajax calls

Using karma and jasmine. How do I properly write a test that runs a method like the following:

methods {
    getAddress: function () {
          Addresses.show()
            .then((response) => {
                if (response.error && response.code === 404) {
                    this.editAddress = true;
                }
                    this.address = response;
            });
      }
}

Addresss.show() is calling an api to get an address. I've read that some people are faking the responses, but I don't know how to do that in this case. Any thoughts?

I tried changing the function getAddress() to take a parameter (Addresses) in which I'd call getAddress(Addresses), with a modified Addresses.show() method. But it still needs to return a promise, because the method getAddresses is using a .then to set the data.

0 likes
2 replies
palemajki's avatar

did you find any answer? I'm facing the same issue... Maybe you can share a file with test for this component?

joshuajr's avatar

I got pulled over to another assignment and didn't finish setting up the tests for this project. What I've decided to try is to just actually hit the api on my local dev environment for my backend. Then I'll just reset that database after it makes the changes. Since all my api were relative, I just had to make sure the correct base_url is set when the tests are run.

Please or to participate in this conversation.