Reputation: 1257
I have been working on adding end to end testing to a current application of mine but I am running into an issue where I am unable to complete a test because of a confirm()
dialogue box that appears in my code (doing a sanity check if they are sure they would like to remove this thing). Problem is that I don't know how to confirm the action in the alert when using Protractor / Jasmine.
//Set up variables for use
var columnNameInput = element(by.model('ColumnNameToOverride'));
var columnDataTypeSelecter = element(by.model('ColumnDataTypeToOverride'));
var addColumnButton = element(by.id('addColumnOverride'));
var columnOverrideRepeater = element.all(by.repeater("i in columnsToOverride"));
//Create first selection
columnNameInput.sendKeys('Protractor Column Name');
columnDataTypeSelecter.sendKeys('double');
addColumnButton.click()
expect(columnOverrideRepeater.count()).toBe(1);
//Create second selection
columnNameInput.sendKeys('Protractor Column Name 2');
columnDataTypeSelecter.sendKeys('double');
addColumnButton.click()
expect(columnOverrideRepeater.count()).toBe(2);
//Attempt to delete second created column
var removeColumnButton1 = element(by.id('removeColumn1'));
removeColumnButton1.click();
//This is where I am having trouble. I can't confirm a confirm() dialogue here <-------
spyOn(window, 'confirm').and.returnValue(true);
expect(columnOverrideRepeater.count()).toBe(1);
var removeColumnButton0 = element(by.id('removeColumn0'));
removeColumnButton0.click();
expect(columnOverrideRepeater.count()).toBe(0);
Any help would be greatly appreciated. Thanks!
Upvotes: 0
Views: 2245
Reputation: 4295
You need to use a mocking framework - something like sinon http://sinonjs.org/. To mock out the confirm function - then you can confirm this function was called in your test.
I would also suggest using $window.confirm rather than just confirm as this makes the mocking process simpler.
Something like this (warning: not actually run, the code is off the top of my head).
(function (angular, sinon) {
'use strict';
describe('namespace.someService', function () {
var sandbox, $rootScope, someService, $window;
beforeEach(module(my.module));
describe('someService', function () {
beforeEach(inject(function ($injector) {
sandbox = sinon.sandbox.create();
$rootScope = $injector.get('$rootScope');
$window = $injector.get('$window');
someService = $injector.get('someService');
}));
afterEach(function () {
sandbox.restore();
});
describe('someMethod', function () {
it('should notify the user via the alert API', function () {
sandbox.stub($window, 'confirm').returns(true);
var result = someService.someMethod();
expect($window.confirm.calledOnce).to.equal(true);
});
});
});
});
}(angular, sinon));
Upvotes: 1