Skip to content

Commit

Permalink
fix(rules): fix handling of multiple arguments in no-repetitive-locators
Browse files Browse the repository at this point in the history
Closes #89
  • Loading branch information
ext committed Nov 19, 2018
1 parent e1db5e8 commit 06cedb0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
18 changes: 14 additions & 4 deletions lib/get-locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
* @fileoverview Utility function to extract the "by" locator values. Also handles the "$" and "$$" shortcuts.
* @author Alexander Afanasyev
*/

function onlyLiteralArguments (node) {
return node.arguments.every(function (arg) {
return arg.type === 'Literal'
})
}

module.exports = function (node) {
var object = node.callee.object
var property = node.callee.property
Expand All @@ -14,20 +21,23 @@ module.exports = function (node) {

// handling by.smth calls
if (insideBy) {
var hasArgument = node.arguments && node.arguments.length && node.arguments[0].hasOwnProperty('value')
if (hasArgument) {
var hasArgument = node.arguments && node.arguments.length
if (hasArgument && onlyLiteralArguments(node)) {
return {
by: property.name,
value: node.arguments[0].value
value: node.arguments.map(function (arg) {
return arg.value
})
}
}
}

// handling $ and $$ calls
if (dollarShortcuts || chainedDollarShortcuts) {
var value = node.arguments[0].value
return {
by: 'css',
value: node.arguments[0].value
value: value ? [value] : undefined
}
}
}
24 changes: 23 additions & 1 deletion lib/rules/no-repetitive-locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@ module.exports = {
// locators collects locators grouped by type, e.g.: {css: [".test", "div:first-of-type"], id: ["myid1", "myid2"]}
var locators = {}

function arrayEquals (a, b) {
if (a.length !== b.length) {
return false
}
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false
}
}
return true
}

function matchExisting (calls, currentArgs) {
if (!calls) {
return false
}

return calls.some(function matchArgs (args) {
return arrayEquals(args, currentArgs)
})
}

return {
CallExpression: function (node) {
var locator = getLocator(node)
if (locator && locator.value) {
// find exact locator duplicates (both by and value were met before)
if (locator.by in locators && locators[locator.by].indexOf(locator.value) >= 0) {
if (matchExisting(locators[locator.by], locator.value)) {
context.report({
node: node,
message: 'Repetitive locator detected'
Expand Down
25 changes: 25 additions & 0 deletions test/rules/no-repetitive-locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ eslintTester.run('no-repetitive-locators', rule, {
' this.firstGrid = this.grids.first();',
'}'
]),
toCode([
'var MyPage = function () {',
' this.dogs = element.all(by.cssContainingText("p", "Dog"));',
' this.cats = element.all(by.cssContainingText("p", "Cat"));',
'}'
]),
toCode([
'var MyPage = function () {',
' this.dogs = element.all(by.cssContainingText("p", "Dog"));',
' this.cats = element.all(by.cssContainingText("i", "Dog"));',
'}'
]),
toCode([
'var MyPage = function () {',
' this.parent = $(".container #parent");',
Expand All @@ -38,6 +50,19 @@ eslintTester.run('no-repetitive-locators', rule, {
],

invalid: [
{
code: toCode([
'var MyPage = function () {',
' this.a = element.all(by.cssContainingText("p", "Dog"));',
' this.b = element.all(by.cssContainingText("p", "Dog"));',
'}'
]),
errors: [
{
message: 'Repetitive locator detected'
}
]
},
{
code: toCode([
'var MyPage = function () {',
Expand Down

0 comments on commit 06cedb0

Please sign in to comment.