Currently, I have created a directive attached to the form. Anytime a form is dirty, I have a confirmation of the modality of window.onbeforeunload when you try to leave. Right now I'm trying to write a jasmine test to make sure window.onbeforeunload gets a call when updating / changing the URL.
var app = angular.directive('app'); app.directive('dialog',[$window, function() { return { restrict: 'A', require: 'form', link: function(scope, element, attrs, formCtrl) { $window.onbeforeunload = function () { if(formCtrl.$dirty) { return 'Are you sure you want to leave this form'; } }; } }; }]);
Part of the Jasmine specification
beforeEach(inject(function(_$rootScope_,_$state_,_$window_) { $rootScope = _$rootScope_; $state = _$state_; $window = _$window_; spyOn($window, 'onbeforeunload') })); describe('Listen for window prompt', function () { it('should be called on url/refresh change', function () { window.location.reload(); expect($window.onbeforeunload).toHaveBeenCalled(); }); });
source share