Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit e86eb72

Browse files
committed
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
1 parent bf26f76 commit e86eb72

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

lib/protractor.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,13 @@ var Protractor = function(webdriverInstance, opt_baseUrl, opt_rootElement) {
841841
*/
842842
this.params = {};
843843

844-
this.moduleNames_ = [];
845-
846-
this.moduleScripts_ = [];
847-
848-
this.moduleArgs_ = [];
844+
/**
845+
* Information about mock modules that will be installed during every
846+
* get().
847+
*
848+
* @type {Array<{name: string, script: function|string, args: Array.<string>}>}
849+
*/
850+
this.mockModules_ = [];
849851
};
850852

851853
/**
@@ -930,30 +932,32 @@ Protractor.prototype.isElementPresent = function(locatorOrElement) {
930932
* the script and may be referenced using the `arguments` object.
931933
*/
932934
Protractor.prototype.addMockModule = function(name, script) {
933-
this.moduleNames_.push(name);
934-
this.moduleScripts_.push(script);
935935
var moduleArgs = Array.prototype.slice.call(arguments, 2);
936-
this.moduleArgs_.push(moduleArgs);
936+
937+
this.mockModules_.push({
938+
name: name,
939+
script: script,
940+
args: moduleArgs
941+
});
937942
};
938943

939944
/**
940945
* Clear the list of registered mock modules.
941946
*/
942947
Protractor.prototype.clearMockModules = function() {
943-
this.moduleNames_ = [];
944-
this.moduleScripts_ = [];
945-
this.moduleArgs_ = [];
948+
this.mockModules_ = [];
946949
};
947950

948951
/**
949952
* Remove a registered mock module.
950953
* @param {!string} name The name of the module to remove.
951954
*/
952955
Protractor.prototype.removeMockModule = function(name) {
953-
var index = this.moduleNames_.indexOf(name);
954-
this.moduleNames_.splice(index, 1);
955-
this.moduleScripts_.splice(index, 1);
956-
this.moduleArgs_.splice(index, 1);
956+
for (var i = 0; i < this.mockModules_.length; ++i) {
957+
if (this.mockModules_[i].name == name) {
958+
this.mockModules_.splice(i, 1);
959+
}
960+
}
957961
};
958962

959963
/**
@@ -1020,20 +1024,22 @@ Protractor.prototype.get = function(destination, opt_timeout) {
10201024

10211025
// At this point, Angular will pause for us, until angular.resumeBootstrap
10221026
// is called.
1023-
for (var i = 0; i < this.moduleScripts_.length; ++i) {
1024-
var name = this.moduleNames_[i];
1025-
var executeScriptArgs = [this.moduleScripts_[i]].
1026-
concat(this.moduleArgs_[i]);
1027+
var moduleNames = [];
1028+
for (var i = 0; i < this.mockModules_.length; ++i) {
1029+
var mockModule = this.mockModules_[i];
1030+
var name = mockModule.name;
1031+
moduleNames.push(name);
1032+
var executeScriptArgs = [mockModule.script].concat(mockModule.args);
10271033
this.driver.executeScript.apply(this, executeScriptArgs).
1028-
then(null, function(err) {
1029-
throw 'Error wile running module script ' + name +
1030-
': ' + err.message;
1031-
});
1034+
then(null, function(err) {
1035+
throw 'Error wile running module script ' + name +
1036+
': ' + err.message;
1037+
});
10321038
}
10331039

10341040
return this.driver.executeScript(
10351041
'angular.resumeBootstrap(arguments[0]);',
1036-
this.moduleNames_);
1042+
moduleNames);
10371043
};
10381044

10391045
/**

spec/basic/mockmodule_spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ describe('mock modules', function() {
4242
expect(element(by.css('[app-version]')).getText()).toEqual('3');
4343
});
4444

45+
it('should use the latest module if two are added with the same name', function() {
46+
browser.addMockModule('moduleA', mockModuleA);
47+
48+
var mockModuleA2 = function() {
49+
var newModule = angular.module('moduleA', []);
50+
newModule.value('version', '3');
51+
};
52+
53+
browser.addMockModule('moduleA', mockModuleA2);
54+
55+
browser.get('index.html');
56+
57+
expect(element(by.css('[app-version]')).getText()).toEqual('3');
58+
});
59+
4560
it('should have the version of the module A after deleting module B', function() {
4661
browser.addMockModule('moduleA', mockModuleA);
4762
browser.addMockModule('moduleB', mockModuleB);
@@ -53,6 +68,15 @@ describe('mock modules', function() {
5368
expect(element(by.css('[app-version]')).getText()).toEqual('2');
5469
});
5570

71+
it('should be a noop to remove a module which does not exist', function() {
72+
browser.addMockModule('moduleA', mockModuleA);
73+
browser.removeMockModule('moduleB');
74+
75+
browser.get('index.html');
76+
77+
expect(element(by.css('[app-version]')).getText()).toEqual('2');
78+
});
79+
5680
it('should have the version provided from parameters through Module C', function() {
5781
browser.addMockModule('moduleC', mockModuleC, '42', 'beta');
5882

0 commit comments

Comments
 (0)