AngularJS - $q-promisified function failing to resolve in unit tests

Utilizing the jsonapi-serializer library for deserializing API data, I have promisified the callback using angular's $q constructor and encapsulated it in a service. The functionality works as expected in the browser, however, when testing it with jasmine on the karma runner, the promise fails to resolve. Below is the TypeScript method implemented in the service:

public deserialize(type: string, data: any): any {

// fetch predefined options for resource type
let deserializeOpts: any = this.deserializeOpts[type];

// utilize jsonapi-serializer
// encountering issues with the options
let deserializer: any = new JAS.Deserializer({});

console.log(data);
// return a promise containing resolved object
return this._$q((resolve: any, reject: any) => {
  deserializer.deserialize(data, (err: any, result: any) => {
    if (result) {
      console.log(resolve);
      resolve(result);
    } else {
      console.log(err);
      reject(err);
    }
  });
});
}

This snippet showcases my debugging efforts during testing:

it('should flatten jsonapi user', function (done) {
  var deserialized;
  JsonapiParser.deserialize(type, apiUser).then(
    (result) => {
      deserialized = result;
      expect(deserialized).toEqual(apiUser);
      done();
    }
  );
});

Here is an example of how the deserializer service is utilized:

// returns the promise so controller can display errors
return this.$http.get(url)
  .then(
    (response: any) => {
      if (response.data.data.length !== 0) {// deserialize data
        return this._deserializer.deserialize('activities', response.data) // calling the deserializer service;
      } else { // throw error if data is empty
        return this.$q.reject({ error: this.ACTIVITY.empty });
      }
    },
    () => {
      return this.$q.reject({ error: this.ACTIVITY.connectionError });
    }
  ).then(
    (deserialized: any) => { // copy data back to original list to preserve bindings
      angular.copy(deserialized, this.list); // utilizing results from deserializer
      console.log(deserialized);
      return this.list;
  });

The above code functions properly when compiled and executed in the browser. However, the tests are timing out. Upon logging inside the deserialize method, I observed that the callback is being resolved, but the promise does not seem to digest. Adding $rootScope.$digest() after the resolve call resolves the test issue, but hardcoding it is not ideal especially since the deployed code operates effectively.

Answer №1

Instead of relying on $rootScope.$digest() in your application code, consider triggering the digest using $rootScope.$apply() from your tests.

For more information on testing promises, check out Testing Promises and Service Testing.

Answer №2

Everything seems to be running smoothly in the browser

It's astonishing! You never actually call resolve! Instead of

console.log(resolve);

you should be using

console.log(result);
resolve(result);

By the way, the common node-style callback promisification uses if (err), not if (result). Perhaps you meant to use if (err || !result).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a Variable with AngularJS ng-bind-html Feature

Recently I started learning AngularJS and I'm facing an issue with a HTML table in my project. Here's the structure of the table: <table> <thead> <tr> <th>ID</th> <th>other stuff</th> </tr> ...

Tips for resolving the error message "TypeError: props.scoreboard.map is not a function" that only occurs after refreshing the page

I'm encountering an unusual problem while attempting to iterate over the response from my backend. Despite logging the response as an array, I still face issues similar to those where the response is not considered an array. I am passing the scoreboa ...

Optimizing Static File Caching in Yii

Having a frustrating issue with Yii where my local development environment caches CSS and JS files. Despite making changes to the file, the edits do not reflect in the output and sometimes causes corruption leading to broken functionality. This problem see ...

Embed images within the JavaScript bundle

Here is my scenario: I have developed a components library for React. Within this library, there is a package bundled with Rollup that contains various assets, including a GIF picture used in one of the components. The specific component utilizing this p ...

Concealing Popover with internal click

I am currently implementing Vue-PopperJS in my project, following the setup provided on the linked page with a few modifications: <template> <Popper ref="popover" trigger="clickToToggle" :options="{ pla ...

Add 1 to the count if the order ID is located in local storage

let items = JSON.parse(localStorage.getItem('cart')) || []; let newItem = {}; $.each(items, function () { if (newItem.order_id == this.order.id) { newItem.order_id = '1'; newItem.order_name = 'cake'; ...

next-auth: after hitting login, user will be redirected to /api/auth/error

I'm encountering an issue where NextAuth consistently redirects to the error page without returning a user as expected based on my code in: ./app/account/login/page.tsx "use client"; import React, { useState } from "react"; import ...

AngularJs - receiving '404 Error: Page not found' error message after setting $locationProvider.html5Mode to true

By placing this in my .config method, the # is removed from the URL: $locationProvider.html5Mode({ enabled: true, requireBase: false }); To address the issue of manual refresh causing problems, I have come across various solutions that involve ...

How can I easily implement basic password protection on a straightforward Next.js web app hosted on Vercel?

Adding a simple password validation step to a dashboard that only a select few would access. The data is not highly sensitive, but some basic protection is desired to limit unauthorized access. While not expecting targeted attacks from hackers, the goal is ...

What is the reason for Jquery AJAX appending additional path to the relative path?

I am encountering an issue with the following code snippet $.ajax({ url: "search/prefetch", success: function (data) { $(".result").html(data); alert("Load was performed."); }, dataType: "json" } ...

Error in D3: stream_layers function is not defined

Utilizing nvd3.js to construct a basic stacked bar chart as detailed here I inserted the code provided in the link into an Angular directive like this: app.directive('stackBar', function() { return { restrict: 'A', ...

The onChange Event triggers only once in a checkbox input

I am implementing a checkbox component that emits an event to the parent component. However, in the parent component, I am facing an issue where I need to perform different actions based on whether the checkbox is checked or not, but it seems to only work ...

Send a JsonArray via Ajax

Struggling with posting a JSON Array to SENDGRID using Ajax. Postman works fine, but encountering error (bad request = missing parameters) in my .js file. Any assistance would be greatly appreciated. Note: The values are valid, sensitive information has ...

Exploring the best practices for handling Ajax requests in React flux architecture

I have developed a react js application where users can register by creating an account, and then I send an HTTP post request to the backend server. My action function for creating a user looks like this: export function createUser(name, username, passwo ...

Displaying a highchart once a form has been submitted in a separate file

Can someone assist with this issue? I am trying to display a Highchart from file_2 in file_1 using PHP, jQuery, and AJAX. Below is the script I am working with: SCR_test02.php <?php require "function.inc.php"; //require "showscr.php"; include "c ...

Having trouble locating and interacting with the textarea element in Salesforce using Selenium Webdriver

Encountering an issue with the Selenium Webdriver where it throws an error stating that the element is not visible and cannot be interacted with when attempting to access a textarea. 1. The textarea is located within a pop-up window, which can be accessed ...

Is it possible to store a JWT token in local storage when working with Next.js?

We are considering using Next.js for our application, with a focus on client-side rendering for data fetching. The API we will be interacting with is external and requires authentication to access specific user dashboard content. While the homepage will ...

I am encountering problems with converting Array to JSON format in PHP for utilization in Javascript

I always face challenges when it comes to converting Array into JSON format. Currently, I am utilizing a selectbox plugin developed by TexoTela. For this plugin to work, it requires a specific JSON structure as shown below: { "ajax1": "AJAX option 1 ...

Automated method for triggering button clicks on a webpage using JavaScript with a tailored combination of unique tag and class name

Currently, I am exploring methods to automatically trigger a button click on a webpage with a specific tag and class. My approach involves using Tampermonkey (Javascript userscripts) to accomplish this task. Referencing the image of the CSS/HTML elements r ...

Creating a website that adapts based on the subdomain used

Allow me to clarify my issue as best I can. If you need more details, please don't hesitate to ask, and forgive any errors in English as it is not my native language. Main Objective I am managing a website, www.mywebsite.com, that I intend to use fo ...