angularjs - Testing angular modal's opened promise -
there's lot of questions on testing angular modals, seem mocking modal instance. want call through implementation, , in particular opened promise callback. here's 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() { // result }); return modalinstance; } function editwidgetinit(widget) { widgetcopy = setselectedchart(widget); // lots of other setup tasks return widgetcopy; } }]);
here's test looks like.
describe('on edit widget', function() { it('should setup selectedchart widget', function() { var widget = {widget: {indicators: [{name: 'indicator'}]}}; var modalinstance = scope.editwidget(widget); rootscope.$digest(); expect(scope.selectedchart).tobe('pie'); }); });
this gets modal instance open, opened promise never fulfillled. should trigger when new modal instance created.
can test real call without mocking it, or there another, better way test this?
in test, can resolve opened
promise yourself.
describe('on edit widget', function() { it('should setup selectedchart widget', function() { var widget = {widget: {indicators: [{name: 'indicator'}]}}; var modalinstance = scope.editwidget(widget); modalinstance.opened.resolve(); rootscope.$digest(); expect(scope.selectedchart).tobe('pie'); }); });
Comments
Post a Comment