From e86eb726ad20737d463341afdb4c56b4d19ef414 Mon Sep 17 00:00:00 2001 From: Julie Date: Mon, 23 Jun 2014 18:24:59 -0700 Subject: [PATCH] fix(protractor): removing a mock module that was never added now is a noop It used to remove the last module - now is a noop. Closes #764 --- lib/protractor.js | 54 +++++++++++++++++++---------------- spec/basic/mockmodule_spec.js | 24 ++++++++++++++++ 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/lib/protractor.js b/lib/protractor.js index 5a211df2e..192f1aafa 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -841,11 +841,13 @@ var Protractor = function(webdriverInstance, opt_baseUrl, opt_rootElement) { */ this.params = {}; - this.moduleNames_ = []; - - this.moduleScripts_ = []; - - this.moduleArgs_ = []; + /** + * Information about mock modules that will be installed during every + * get(). + * + * @type {Array<{name: string, script: function|string, args: Array.}>} + */ + this.mockModules_ = []; }; /** @@ -930,19 +932,20 @@ Protractor.prototype.isElementPresent = function(locatorOrElement) { * the script and may be referenced using the `arguments` object. */ Protractor.prototype.addMockModule = function(name, script) { - this.moduleNames_.push(name); - this.moduleScripts_.push(script); var moduleArgs = Array.prototype.slice.call(arguments, 2); - this.moduleArgs_.push(moduleArgs); + + this.mockModules_.push({ + name: name, + script: script, + args: moduleArgs + }); }; /** * Clear the list of registered mock modules. */ Protractor.prototype.clearMockModules = function() { - this.moduleNames_ = []; - this.moduleScripts_ = []; - this.moduleArgs_ = []; + this.mockModules_ = []; }; /** @@ -950,10 +953,11 @@ Protractor.prototype.clearMockModules = function() { * @param {!string} name The name of the module to remove. */ Protractor.prototype.removeMockModule = function(name) { - var index = this.moduleNames_.indexOf(name); - this.moduleNames_.splice(index, 1); - this.moduleScripts_.splice(index, 1); - this.moduleArgs_.splice(index, 1); + for (var i = 0; i < this.mockModules_.length; ++i) { + if (this.mockModules_[i].name == name) { + this.mockModules_.splice(i, 1); + } + } }; /** @@ -1020,20 +1024,22 @@ Protractor.prototype.get = function(destination, opt_timeout) { // At this point, Angular will pause for us, until angular.resumeBootstrap // is called. - for (var i = 0; i < this.moduleScripts_.length; ++i) { - var name = this.moduleNames_[i]; - var executeScriptArgs = [this.moduleScripts_[i]]. - concat(this.moduleArgs_[i]); + var moduleNames = []; + for (var i = 0; i < this.mockModules_.length; ++i) { + var mockModule = this.mockModules_[i]; + var name = mockModule.name; + moduleNames.push(name); + var executeScriptArgs = [mockModule.script].concat(mockModule.args); this.driver.executeScript.apply(this, executeScriptArgs). - then(null, function(err) { - throw 'Error wile running module script ' + name + - ': ' + err.message; - }); + then(null, function(err) { + throw 'Error wile running module script ' + name + + ': ' + err.message; + }); } return this.driver.executeScript( 'angular.resumeBootstrap(arguments[0]);', - this.moduleNames_); + moduleNames); }; /** diff --git a/spec/basic/mockmodule_spec.js b/spec/basic/mockmodule_spec.js index 027ac4aa9..0680e94eb 100644 --- a/spec/basic/mockmodule_spec.js +++ b/spec/basic/mockmodule_spec.js @@ -42,6 +42,21 @@ describe('mock modules', function() { expect(element(by.css('[app-version]')).getText()).toEqual('3'); }); + it('should use the latest module if two are added with the same name', function() { + browser.addMockModule('moduleA', mockModuleA); + + var mockModuleA2 = function() { + var newModule = angular.module('moduleA', []); + newModule.value('version', '3'); + }; + + browser.addMockModule('moduleA', mockModuleA2); + + browser.get('index.html'); + + expect(element(by.css('[app-version]')).getText()).toEqual('3'); + }); + it('should have the version of the module A after deleting module B', function() { browser.addMockModule('moduleA', mockModuleA); browser.addMockModule('moduleB', mockModuleB); @@ -53,6 +68,15 @@ describe('mock modules', function() { expect(element(by.css('[app-version]')).getText()).toEqual('2'); }); + it('should be a noop to remove a module which does not exist', function() { + browser.addMockModule('moduleA', mockModuleA); + browser.removeMockModule('moduleB'); + + browser.get('index.html'); + + expect(element(by.css('[app-version]')).getText()).toEqual('2'); + }); + it('should have the version provided from parameters through Module C', function() { browser.addMockModule('moduleC', mockModuleC, '42', 'beta');