From e5947043f686026b2260e794ad2cb2ba92c03821 Mon Sep 17 00:00:00 2001 From: fredkingham Date: Fri, 17 Feb 2017 16:36:39 +0000 Subject: [PATCH] if we cancel don't call the callback --- .../controllers/modal_pathway_maker.js | 19 +++++++++---- .../modal_pathway_maker.controller.test.js | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/pathway/static/js/pathway/controllers/modal_pathway_maker.js b/pathway/static/js/pathway/controllers/modal_pathway_maker.js index cc4a20e..7cdd38f 100644 --- a/pathway/static/js/pathway/controllers/modal_pathway_maker.js +++ b/pathway/static/js/pathway/controllers/modal_pathway_maker.js @@ -15,15 +15,22 @@ angular.module('opal.controllers').controller('ModalPathwayMaker', function( var result = new pathwayService(pathwayDefinition, episode).open(); result.then(function(response){ - var resolved = pathwayCallback(response); - if(resolved && resolved.then){ - resolved.then(function(callBackResult){ - $modalInstance.close(callBackResult); - }); + // if there is a response then this was saved, otherwise it was cancelled + if(response){ + var resolved = pathwayCallback(response); + if(resolved && resolved.then){ + resolved.then(function(callBackResult){ + $modalInstance.close(callBackResult); + }); + } + else{ + $modalInstance.close(resolved); + } } else{ - $modalInstance.close(resolved); + $modalInstance.close(); } + }, function(error){ $window.alert("unable to save patient"); }); diff --git a/pathway/static/js/pathwaytest/modal_pathway_maker.controller.test.js b/pathway/static/js/pathwaytest/modal_pathway_maker.controller.test.js index 75db3bc..959dbe2 100644 --- a/pathway/static/js/pathwaytest/modal_pathway_maker.controller.test.js +++ b/pathway/static/js/pathwaytest/modal_pathway_maker.controller.test.js @@ -37,6 +37,34 @@ describe("ModalPathwayMaker", function(){ }); }); + it("should not call the callback if there is no response", function(){ + var mockPathwayService = function(pathwaySlug, episode, isModal){ + this.open = function(){ + return { + then: function(fn){ + fn(); + } + }; + }; + }; + + mockInjector = { + get: function(something){ return mockPathwayService; } + }; + + $controller("ModalPathwayMaker", { + $scope: $scope, + $modalInstance: $modalInstance, + $injector: mockInjector, + pathwaySlug: pathwaySlug, + pathwayLoader: pathwayLoader, + episode: episode, + pathwayCallback: callBack + }); + expect(callBack).not.toHaveBeenCalled(); + expect($modalInstance.close).toHaveBeenCalledWith(); + }); + it("should flow through on success", function(){ var mockPathwayService = function(pathwaySlug, episode, isModal){ this.open = function(){