Testing Restangular with multiple promises using Jasmine

Currently, I am attempting to utilize Jasmine to test if my array (storage) has gained an additional item. Despite having multiple promise chains, I am unable to access the first one as nothing seems to occur within the promise (.then).

Below is my test :

beforeEach(inject(function ($state, $controller, $rootScope, $q) {
    state = $state.get('app.provision_requests_create');
    ctrl = $controller('ProvisionRequestsCreateCtrl', {
      $scope: $rootScope.$new(),
      provider: {
        data: {
          priceStorage: function () {
            return $q(function (resolve) {
              resolve({ data: [] });
            });
          }
        }
      }
    });
  }));

  describe("should test add Volume", function() {
    it('should remove volume properly ', function () {

      ctrl.createVolume(volume);
      ctrl.provider.priceStorage().then(function(){
        console.log('ok');
      });

    });

This is my function :

_this.provider = provider.data;
_this.instance.additional_values.storage = [];

_this.createVolume = function (volume) {
    var newvol = angular.copy(volume);

    //reset input and select
    angular.copy({},volume);

    return _this.provider.priceStorage(newvol.kind).then(function (resp) {
      ...
      return _this.provider.priceSnapshot();
    })
    .then(function (resp) {
      _this.instance.additional_values.storage.push(newvol);
    });
  };

When I log ctrl.provider.priceStorage(), the output is as follows:

d{$$state: Object{status: 1, value: Object{data: ...}}}

Upon logging ctrl.createVolume(), the result obtained is:

d{$$state: Object{status: 0}}

Answer №1

ctrl.provider.priceStorage() returns a promise.

Testing promises can be done in the following manner:

  ctrl.provider.priceStorage().then(function (result) {
    expect(result).toEqual(...);
  });
  $rootScope.$digest();

For simplifying promise chains in specifications, consider using Jasmine Promise matchers.

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

The onLayoutChange function keeps overwriting the data stored in my local storage

When my layout changes, the new changes are saved into local storage and the layout state is updated. onLayoutChange(layouts) { saveToLS("layouts", layouts); } However, an issue arises when the page is refreshed. The layout g ...

The lifespan of an Angular controller and its management of events

What is the best way to manage events, such as $rootScope or socket events, in an Angular controller without causing issues with event duplication? I have noticed that my controllers are not being destroyed properly, leading to problems when it comes to li ...

Utilizing Lodash for organizing data to create a pie chart in AngularJS

Looking to integrate a dataset with a pie chart within my AngularJS application. The purpose of the pie chart is to represent the gender distribution of users, specifically male and female. The required dataset format is: [[60, 40]] User data is structu ...

Why is $(window).load() being executed twice?

When using $(window).load() to resize images in a thumbnail gallery and set up a slideshow, the code seems to be running twice. This can be observed by noticing that a div is being wrapped around another div twice when inspecting the HTML on page load. Af ...

AngularJS does not refresh the DOM as anticipated

I have a fiddle demonstrating the geocoding of an address input into a textbox. The issue I'm facing is that after pressing 'enter', the table does not update immediately; it waits for another change in the textbox. How can I make it update ...

Avoid sending duplicate requests at the same time with Angular resource

On a single page, I have several components. All of these components are requesting the same data from the server. This leads to multiple requests being sent when the page loads. Is there a way to avoid this redundancy? For example, could we cache the p ...

Loading a local FBX file in Three.js without the need to upload it

When attempting to load files selected by users in an HTML input, I encountered a problem with the loader expecting a URL in Linux style. I have tried various methods such as using a blob as a URL object, providing raw data to the FBX loader, and even usin ...

Difficulty encountered with personalized d3.js visualization

I'm currently diving into the world of d3.js and aiming to develop a visualization similar to the example image I've attached in the link below. http://jsfiddle.net/mike_ellan/37PYJ/ While I have a good grasp on creating and binding text elemen ...

Create a duplicate of an external XML file without including the XML namespace

Similar Issue: How to detect and remove unnecessary xmlns:<something> attributes in PHP DOM? I am facing a challenge that I hope you can assist me with. An XML file located on an external server is only accessible through a secure https connecti ...

Using RxJS v5 for Sending a POST Request with Parameters

Snippet of my RxJS code: .mergeMap(action => { const user = store.getState().user; return ajax.post(`${config.API_BASE_URL}/api/v1/rsvps`, { rsvp: { meetup_id: action.payload, user_id: user.id, } }) .map(action => calenda ...

Chart rendering failure: unable to obtain context from the provided item

I am encountering an issue while trying to incorporate a chart from the charts.js library into my Vue.js and Vuetify application. The error message that keeps popping up is: Failed to create chart: can't acquire context from the given item Even af ...

Error message: Authentication timestamp issue with cex.io web socket

Currently in the process of connecting to the CEX.IO bitcoin exchange's websocket. The websocket connection itself is fine, but encountering an error during the authentication phase: "Timestamp is not in 20sec range." Unsure about the cause of this is ...

Utilize the power of Facebook login in your Parse client side application by integrating it with the user object

Currently, I am in the process of implementing a login system using both the Parse and Facebook Javascript SDK. While I have successfully implemented authentication on the client side, I am now facing the challenge of accessing the user object (generated ...

assign a dynamic expression to a variable

I am currently working on a Vue application where an expression is retrieved from a config file as a variable. My goal is to run this expression (similar to using eval in JavaScript) and apply the result to an attribute. For example: <v-text-field plac ...

An easy way to create an input field after clicking a button

When I try to add a field on Button Click, the default field is not showing and does not get added upon button click I have put in my best effort but I cannot figure out what the problem is. I have added functions and used Math to generate a unique id. Th ...

The onDrop event in javascript seems to be failing to execute

My attempts to get the javascript onDrop event to execute when an object is dropped into a drop zone have been unsuccessful. I've tried rewriting it in various ways without any success or error messages. In my search for potential reasons why the ondr ...

Rotating an object around another object in a 3D matrix axis using Three.js

I currently have the ability to rotate one axis. Here is the code that's working now: https://codepen.io/itzkinnu/full/erwKzY Is there a way to rotate an object on a random axis instead of just one fixed axis? Maybe something similar to this exam ...

Developing a comprehensive Java web service project with AngularJS, test-driven development/behavior-driven development, using Maven and Eclipse

Encountering some challenges while setting up a project to implement the full Java, Angular.js, TDD/BDD stack. While these challenges are not causing any major obstacles as of now, they have the potential to become one. Using Eclipse 4.6.0 Neon with WTP, ...

ability to utilize controller object through service

Here is the code for my controller : constructor(Auth, $http, $rootScope, $log, $scope, $uibModal, loginTemplate, demandCity, signupTemplate, socket) { this.isLoggedIn = Auth.isLoggedIn; this.$http = $http; this.socket = socket; this.awesomeDemand ...

Changing the default view in Ionic

I'm facing a simple issue that I can't seem to solve. I've set up a blank project in Ionic and I want to make a default home screen the first view that loads. However, all I see is a blank screen when I run it in the browser. Here's my ...