I have an Angularjs application that uses simple javascript confirm before executing some actions.
function TokenControll
Please consult to this project: https://github.com/katranci/Angular-E2E-Window-Dialog-Commands
If you create a service for the dialog boxes then you can mock that service in your unit test in order to make your code testable:
function TokenController($scope, modalDialog) {
$scope.token = 'sampleToken';
$scope.newToken = function() {
if (modalDialog.confirm("Are you sure you want to change the token?") == true) {
$scope.token = 'modifiedToken';
}
};
}
yourApp.factory('modalDialog', ['$window', function($window) {
return {
confirm: function(message) {
return $window.confirm(message);
}
}
}]);
function modalDialogMock() {
this.confirmResult;
this.confirm = function() {
return this.confirmResult;
}
this.confirmTrue = function() {
this.confirmResult = true;
}
this.confirmFalse = function() {
this.confirmResult = false;
}
}
var scope;
var modalDialog;
beforeEach(module('yourApp'));
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
modalDialog = new modalDialogMock();
var ctrl = $controller('TokenController', {$scope: scope, modalDialog: modalDialog});
}));
it('should be able to generate new token', function () {
modalDialog.confirmTrue();
scope.newToken();
expect(scope.token).toBe('modifiedToken');
});