Skip to content

Commit

Permalink
fix(rules): use-angular-locators to support 'ng-options'
Browse files Browse the repository at this point in the history
  • Loading branch information
alecxe committed Jun 10, 2016
1 parent 4a91604 commit b376e85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/rules/use-angular-locators.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Recommend using built-in Angular-specific locators

Warn about using attributes like `ng-model`, `ng-bind`, `ng-repeat` inside CSS selectors. Recommend built-in specific locators instead.
Warn about using attributes like `ng-model`, `ng-bind`, `ng-repeat` and `ng-options` inside CSS selectors. Recommend built-in specific locators instead.

## Rule details

Expand All @@ -9,6 +9,7 @@ Here is a mapping between the relevant attribute and a recommended locator:
* `ng-model`/`data-ng-model` -> `by.model()`
* `ng-bind`/`data-ng-bind` -> `by.binding()` or `by.exactBinding()`
* `ng-repeat`/`data-ng-repeat` -> `by.repeater()` or `by.exactRepeater()`
* `ng-options`/`data-ng-options` -> `by.options()`

Any use of the following patterns are considered warnings:

Expand All @@ -19,6 +20,8 @@ $("[ng-bind=test]");
$$("[data-ng-bind=test]");
element(by.id("test")).$('[ng-repeat="item in items"]');
element(by.id("test")).$$('[data-ng-repeat="item in items"]');
$('[ng-options="item in items"]');
$$('[data-ng-options="item in items"]');
```

The following patterns are not warnings:
Expand All @@ -29,4 +32,5 @@ element(by.binding("test"));
element(by.exactBinding("test"));
element.all(by.repeater("item in items"));
element.all(by.exactRepeater("item in items"));
element.all(by.options("item in items"));
```
3 changes: 2 additions & 1 deletion lib/rules/use-angular-locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = function (context) {
var attributeToLocatorMap = {
'ng-model': ['by.model()'],
'ng-bind': ['by.binding()', 'by.exactBinding()'],
'ng-repeat': ['by.repeater()', 'by.exactRepeater()']
'ng-repeat': ['by.repeater()', 'by.exactRepeater()'],
'ng-options': ['by.options()']
}

return {
Expand Down
19 changes: 18 additions & 1 deletion test/rules/use-angular-locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ eslintTester.run('use-angular-locators', rule, {
'element(by.binding("test"));',
'element(by.exactBinding("test"));',
'element.all(by.repeater("item in items"));',
'element.all(by.exactRepeater("item in items"));'
'element.all(by.exactRepeater("item in items"));',
'element.all(by.options("item in items"));'
],

invalid: [
Expand Down Expand Up @@ -62,6 +63,22 @@ eslintTester.run('use-angular-locators', rule, {
message: 'Unexpected "ng-repeat" attribute used inside a CSS selector. Use by.repeater() or by.exactRepeater() locator instead'
}
]
},
{
code: 'element(by.id("test")).$(\'[ng-options="item in items"]\');',
errors: [
{
message: 'Unexpected "ng-options" attribute used inside a CSS selector. Use by.options() locator instead'
}
]
},
{
code: 'element(by.id("test")).$$(\'[data-ng-options="item in items"]\');',
errors: [
{
message: 'Unexpected "ng-options" attribute used inside a CSS selector. Use by.options() locator instead'
}
]
}
]
})

0 comments on commit b376e85

Please sign in to comment.