Attualmente, ho creato una direttiva allegata a un modulo. Ogni volta che il modulo è sporco, ho una modal di conferma da window.onbeforeunload pop up quando si tenta di lasciare. In questo momento sto provando a scrivere un test sui gelsomini per assicurarmi che window.onbeforeunload venga richiamato dopo l'aggiornamento/modifica dell'URL.Come attivare window.onbeforeunload in jasmine test
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';
}
};
}
};
}]);
Parte del gelsomino Spec
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();
});
});
Quando è scattato l'evento onbeforeunload, si spezzerebbe le prove di gelsomino e fare un aggiornamento completo sulla pagina web. –