Skip to content

Commit

Permalink
converted project to standardjs
Browse files Browse the repository at this point in the history
  • Loading branch information
dereke committed Apr 5, 2017
1 parent 89d37d3 commit 45b23a9
Show file tree
Hide file tree
Showing 47 changed files with 2,685 additions and 1,933 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test:
karma start

test-all:
standard
mocha
karma start --single-run --browsers browserstack-osx-chrome
karma start --single-run --browsers browserstack-osx-firefox
Expand Down
227 changes: 114 additions & 113 deletions actions.js
Original file line number Diff line number Diff line change
@@ -1,181 +1,182 @@
var debug = require('debug')('browser-monkey');
var sendkeys = require('./sendkeys');
var errorHandler = require('./errorHandler');
function notSillyBlankIEObject(element){
return Object.keys(element).length > 0;
var debug = require('debug')('browser-monkey')
var sendkeys = require('./sendkeys')
var errorHandler = require('./errorHandler')
function notSillyBlankIEObject (element) {
return Object.keys(element).length > 0
}
module.exports = {
focus: function(element, options) {
var focus = typeof options == 'object' && options.hasOwnProperty('focus')? options.focus: true;
focus: function (element, options) {
var focus = typeof options === 'object' && options.hasOwnProperty('focus') ? options.focus : true

if (focus) {
var $ = this.get('$');
var document = this.get('document');
var $ = this.get('$')
var document = this.get('document')
if (element && element.length > 0) {
element = element[0];
element = element[0]
}

var activeElement = document.activeElement;
var activeElement = document.activeElement
if (activeElement && !$(activeElement).is(':focus') && notSillyBlankIEObject(activeElement)) {
$(activeElement).trigger('blur');
$(activeElement).trigger('blur')
}
if (['[object Document]', '[object HTMLDocument]'].indexOf(document.toString()) === -1){
document.activeElement = element;
if (['[object Document]', '[object HTMLDocument]'].indexOf(document.toString()) === -1) {
document.activeElement = element
}
$(element).focus();
$(element).focus()
}
},

click: function(options) {
var self = this;
click: function (options) {
var self = this

if (typeof options === 'string') {
self = this.linkOrButton(options);
self = this.linkOrButton(options)
}

return self.enabled().element(options).then(function(element) {
debug('click', element);
self.handleEvent({type: 'click', element: element});
self.focus(element, options);
element.trigger('mousedown');
element.trigger('mouseup');
element.trigger('click');
}).catch(errorHandler(new Error()));
return self.enabled().element(options).then(function (element) {
debug('click', element)
self.handleEvent({type: 'click', element: element})
self.focus(element, options)
element.trigger('mousedown')
element.trigger('mouseup')
element.trigger('click')
}).catch(errorHandler(new Error()))
},

select: function(options) {
if (typeof options == 'string') {
var o = arguments[1] || {};
o.text = options;
return this.select(o);
select: function (options) {
if (typeof options === 'string') {
var o = arguments[1] || {}
o.text = options
return this.select(o)
}
var $ = this.get('$');
var self = this;

return this.is('select').find('option', options).elements(options).then(function(optionElements) {
var optionElement = $(optionElements[0]);
var selectElement = optionElement.parent();
self.focus(selectElement, options);
optionElement.prop('selected', true);
optionElement.attr('selected', 'selected');
selectElement.val(optionElement.val());

debug('select', selectElement);
var $ = this.get('$')
var self = this

return this.is('select').find('option', options).elements(options).then(function (optionElements) {
var optionElement = $(optionElements[0])
var selectElement = optionElement.parent()
self.focus(selectElement, options)
optionElement.prop('selected', true)
optionElement.attr('selected', 'selected')
selectElement.val(optionElement.val())

debug('select', selectElement)
self.handleEvent({
type: 'select option',
value: selectElement.val(),
element: selectElement,
optionElement: optionElement
});
})

selectElement.trigger('change');
}).catch(errorHandler(new Error()));
selectElement.trigger('change')
}).catch(errorHandler(new Error()))
},

typeIn: function(text, options) {
if (typeof text === 'object'){
text = text.text;
typeIn: function (text, options) {
if (typeof text === 'object') {
text = text.text
}
var self = this;

return this.element(options).then(function(element) {
debug('typeIn', element, text);
assertCanTypeIntoElement(element);
self.focus(element, options);
self.handleEvent({type: 'typing', text: text, element: element});
return sendkeys(element, text);
}).catch(errorHandler(new Error()));
var self = this

return this.element(options).then(function (element) {
debug('typeIn', element, text)
assertCanTypeIntoElement(element)
self.focus(element, options)
self.handleEvent({type: 'typing', text: text, element: element})
return sendkeys(element, text)
}).catch(errorHandler(new Error()))
},

submit: function(options) {
var self = this;
submit: function (options) {
var self = this

return this.element(options).then(function(element) {
debug('submit', element);
self.focus(element, options);
self.handleEvent({type: 'submit', element: element});
return element.trigger('submit');
}).catch(errorHandler(new Error()));
return this.element(options).then(function (element) {
debug('submit', element)
self.focus(element, options)
self.handleEvent({type: 'submit', element: element})
return element.trigger('submit')
}).catch(errorHandler(new Error()))
},

typeInHtml: function(html, options) {
var self = this;
typeInHtml: function (html, options) {
var self = this

return this.element(options).then(function(element) {
self.focus(element, options);
debug('typeInHtml', element, html);
self.handleEvent({type: 'typing html', html: html, element: element});
return sendkeys.html(element, html);
}).catch(errorHandler(new Error()));
return this.element(options).then(function (element) {
self.focus(element, options)
debug('typeInHtml', element, html)
self.handleEvent({type: 'typing html', html: html, element: element})
return sendkeys.html(element, html)
}).catch(errorHandler(new Error()))
},


fill: function(field){
var isArray = Object.prototype.toString.call(field) === '[object Array]';
var component = this;
fill: function (field) {
var isArray = Object.prototype.toString.call(field) === '[object Array]'
var component = this
var Promise = this.promise()
return new Promise(function(success, failure){
return new Promise(function (resolve, reject) {
if (isArray) {
var fields = field;
function fillField(){
var field = fields.shift();
if (field) {
return component.fill(field).then(fillField).catch(failure);
} else {
success();
}
}

fillField();
fillField(component, field)
.then(resolve)
.catch(reject)
} else {
if (!field.name) {
try {
field = inferField(component, field);
} catch(e) {
failure(e.message);
return;
field = inferField(component, field)
} catch (e) {
reject(e)
return
}
}

if (typeof component[field.name] === 'function') {
var finder = component[field.name]()
success(component[field.name]()[field.action](field.options));
resolve(component[field.name]()[field.action](field.options))
} else {
failure("No field '"+field.name+"' exists on this component");
reject(new Error("No field '" + field.name + "' exists on this component"))
}
}
});
})
}
};
}

function inferField(component, field){
var ignoreActions = {constructor: true, _options: true};
function fillField (component, fields) {
var field = fields.shift()
if (field) {
return component.fill(field).then(function () {
return fillField(component, fields)
})
} else {
return Promise.resolve()
}
}

function inferField (component, field) {
var ignoreActions = {constructor: true, _options: true}
for (var action in component) {
if (field[action] && !ignoreActions[action]){
if (field[action] && !ignoreActions[action]) {
var newField = {
name: field[action],
action: action,
options: field
};
delete field[action];
}
delete field[action]

if (field.options) {
newField.options = field.options;
newField.options = field.options
}

if (typeof component[newField.name] !== 'function'){
throw new Error("Field '"+newField.name+"' does not exist");
if (typeof component[newField.name] !== 'function') {
throw new Error("Field '" + newField.name + "' does not exist")
}

return newField;
return newField
}
};
if (!field.name) {
throw new Error('No action found for field: '+JSON.stringify(field));
throw new Error('No action found for field: ' + JSON.stringify(field))
}
}

function canTypeIntoElement(element) {
function canTypeIntoElement (element) {
return element.is('input:not([type]), ' +
'input[type=text], ' +
'input[type=email], ' +
Expand All @@ -184,11 +185,11 @@ function canTypeIntoElement(element) {
'input[type=tel], ' +
'input[type=url], ' +
'input[type=number],' +
'textarea');
'textarea')
}

function assertCanTypeIntoElement(element) {
function assertCanTypeIntoElement (element) {
if (!canTypeIntoElement(element)) {
throw new Error('Cannot type into ' + element.prop('tagName'));
throw new Error('Cannot type into ' + element.prop('tagName'))
}
}
23 changes: 12 additions & 11 deletions angular.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
var debug = require('debug')('browser-monkey:angular')
var Mount = require('./mount');
var createMonkey = require('./create');
var createTestDiv = require('./createTestDiv');
var Mount = require('./mount')
var createMonkey = require('./create')
var createTestDiv = require('./createTestDiv')
var angular = require('angular')

module.exports = function(app) {
module.exports = function (app) {
return new Mount(app, {
stopApp: function(){},
startApp: function(){
stopApp: function () {},
startApp: function () {
debug('Mounting angular app ' + app.moduleName)
var div = createTestDiv();
div.setAttribute(app.directiveName, '');
angular.bootstrap(div, [app.moduleName]);
var div = createTestDiv()
div.setAttribute(app.directiveName, '')
angular.bootstrap(div, [app.moduleName])

return createMonkey(document.body);
return createMonkey(document.body)
}
}).start();
}).start()
}
Loading

0 comments on commit 45b23a9

Please sign in to comment.