How to unit test Ionic popup close function

Hey Ionites,

I have a popup in my controller

   $ionicPopup.alert({
     title    : $translate.instant('miscelleanous.congratulation')
   })
   .then(function() {
      // Lot of thing come here
   });

In my unit Test (Karma + Jasmine), I don’t know how to enter in the ‘then’ function of my popup

  describe('When registration failed', function() {
    var vm;

    beforeEach(function() {
      vm = createController();
    });

    it('Should display error message', function() {
       var popup = spyOn($ionicPopup, 'alert').and.callThrough();
      expect(popup).toHaveBeenCalled();
    });
  });

So Ok my popup is correctly call but I don’t know how to test the promise that return the popup ?

1 Like

Got it !

  describe('When registration succeed', function() {
    var vm;

    beforeEach(function() {
      vm = createController();
    });

    it('should display success message if registration succeed', inject(function($q) {
      var deferred = $q.defer();
      deferred.resolve('somevalue');

      var popup = spyOn($ionicPopup, 'alert').and.callThrough().and.returnValue(deferred.promise);
    
      vm.register();

      expect(popup).toHaveBeenCalled();
      expect(true).toBe(true);
    }));
  });
1 Like

Hey!
Im trying to test the same but with cofirm ionicPopup…im getting this:

TypeError: undefined is not a constructor (evaluating '$ionicPopup.confirm({ …

Do you any clue??

Thanks :wink:

1 Like