jquigs62
jquigs62

Reputation: 1

How to mock an async function that makes multiple http requests that return promises in AngularJS using httpbackend?

Im trying to write unit tests for a function that I know works through thorough testing but I can not for the life of me figure out why my unit tests arent working

The function is very simple I have an initial function which does some preprocessing, then makes a call to a second helper async function. That second function makes a post request to start a job which takes some time, then it makes a get request to grab the results from the job.:

_this.getFormula = async function() {
    //does some preprocessing and sets improvedformula
    const resultFromService = await _this.getFormulaFromService();
    //does some postprocessing and sets improvedformula to result
  };

  _this.getFormulaFromService = async function() {
    try {
      // Request 1:
      const firstResponse = await $http.post(url, query, headers);
      // Request 2:
      // We need to wait to let it finish
      const jobId = firstResponse.data.job_id;
      let finalResult;
      finalResult = await $http.get(newurl);
      return finalResult.data.results[0].result.formula || NO_FORMULA_FOUND_MESSAGE;
    } catch (e) {
      return 'Error!';
    }
  };

My unit test:

it('gets formula from service', function (done) {
        ctrlInstance.searchQuery = 'query';
    
        const jobIDResponse = {
          job_id: 'dummyJobID',
        };
    
        const jobResultResponse = {
          results: [
            {
              result: {
                formula: 'formula',
              },
            },
          ],
        };
        $httpBackend
        .whenPOST(/.*/)
        .respond(jobIDResponse);
      $httpBackend
        .whenGET(/.*/)
        .respond(jobResultResponse);
        ctrlInstance.getFormula().then(() => {
              expect(ctrlInstance.improvedFormula).toEqual('formula');
              done();
            });
        $httpBackend.flush();
      });

The unit test should be very simple but it never seems to progress past the POST, im not sure if something is going wrong with resolving that promise, but it never seems to even reach the get. I added some debugging to modify improvedFormula at various parts and when i run the tests it will return a value such that it never even made it past the POST

I tried using async/await in the tests but that just resulted in timeout

I tried wrapping things in rootscope.apply to force digest but that just said it was already trying to apply

I tried using timeout but then the function just hung

I tried using async/await and done together but that didnt work either

Upvotes: 0

Views: 52

Answers (1)

jquigs62
jquigs62

Reputation: 1

https://dev.to/vantanev/async-functions-and-angularjs-1-x-do-not-mix-3kb3

Basically you cannot use async/await with angularjs. Its not possible. Use oldschool promises like this

function updateItem(item) {
    return $http.put(`/items/${item.id}`, item).then(({ data }) => data)
}

Upvotes: 0

Related Questions