dimanche 5 juillet 2015

Testing angular modal's opened promise

There's a lot of questions on SO about testing angular modals, but they all seem to be about mocking the modal instance. I actually want to call through to the implementation, and in particular the opened promise callback. Here's my module:

angular.module('MyApp')
.directive('widgetContainer', function() {
  return {
    templateUrl: '/static/templates/container.html',
    controller: 'ContainerCtrl'
  };
})
.controller('ContainerCtrl', ['$scope', '$modal', function($scope, $modal) {

  function editWidget(widget) {
    var modalInstance = $modal.open({
      templateUrl: '/static/templates//modal.html',
      controller: 'ModalInstanceCtrl',
      scope: $scope,
      size: 'lg',
      backdrop: 'static'
    });
    modalInstance.opened.then(function() {
      $scope.widgetCopy = editWidgetInit(widget);
    });
    modalInstance.result
      .then(function() {
        // some result
      });
    return modalInstance;
  }

  function editWidgetInit(widget) {
    widgetCopy = setSelectedChart(widget);
    // lots of other setup tasks
    return widgetCopy;
  }

}]);

Here's what the test looks like.

describe('on edit widget', function() {
  it('should setup selectedChart from widget', function() {
    var widget = {widget: {indicators: [{name: 'indicator'}]}};
    var modalInstance = scope.editWidget(widget);
    rootScope.$digest();
    expect(scope.selectedChart).toBe('pie');
  });
});

This gets the modal instance to open, but the opened promise is never fulfillled. It should normally trigger when the new modal instance is created.

Can I test this with a real call without mocking it, or is there another, better way to test this?

Aucun commentaire:

Enregistrer un commentaire