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

Commit

Permalink
fix(protractor): removing a mock module that was never added now is a…
Browse files Browse the repository at this point in the history
… noop

It used to remove the last module - now is a noop.

Closes #764
  • Loading branch information
juliemr committed Jun 24, 2014
1 parent bf26f76 commit e86eb72
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
54 changes: 30 additions & 24 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>}>}
*/
this.mockModules_ = [];
};

/**
Expand Down Expand Up @@ -930,30 +932,32 @@ 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_ = [];
};

/**
* Remove a registered mock module.
* @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);
}
}
};

/**
Expand Down Expand Up @@ -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);
};

/**
Expand Down
24 changes: 24 additions & 0 deletions spec/basic/mockmodule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');

Expand Down

0 comments on commit e86eb72

Please sign in to comment.