Skip to content

Commit

Permalink
Support multiple test attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledMohamedP committed Aug 4, 2023
1 parent 9ff3d4a commit 350d135
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
14 changes: 12 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ module.exports = {
"type": "object",
"properties": {
"testAttribute": {
"type": "string"
},
"oneOf": [
{
"type": "string",
},
{
"type": "array",
"items": {
"type": "string",
},
},
],
},
"ignoreDisabled": {
"type": "boolean"
},
Expand Down
15 changes: 12 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ const {
// regex.
// https://stackoverflow.com/a/37217166/214017
const fillTemplate = (str, vars) => {
return new Function(`return \`${ str }\`;`).call(vars);
vars.attribute = Array.isArray(vars.attribute)
? vars.attribute.join(", or ")
: vars.attribute;

return new Function(`return \`${str}\`;`).call(vars);
};

/* Determines the value of an HTML Node prop/attribute. */
const getValue = (node, prop) => getPropValue(getProp(node.attributes, prop));

/* Determines if a node's attribute passes a filter test. */
const attributeTest = (node, { attribute, test }) => {
const attributeValue = getValue(node, attribute);
const attrs = Array.isArray(attribute) ? attribute : [attribute];
const type = elementType(node);
return test({ attributeValue, elementType: type });

// One of the attributes must be present on the node
return attrs.some((attr) => {
const attributeValue = getValue(node, attr);
return test({ attributeValue, elementType: type });
});
};

module.exports = {
Expand Down
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ The default test attribute expected is `data-test-id`, but you can override it w
}
```

Note: You can also pass multiple attributes

```json
{
"rules": {
"test-selectors/onChange": [
"warn",
"always",
{ "testAttribute": ["data-testid", "testId"] }
]
}
}
```


### ignoreDisabled

By default all elements with the `disabled` attribute are ignored, e.g. `<input disabled />`. If you don't want to ignore this attribute, set `ignoreDisabled` to `false`:
Expand Down Expand Up @@ -148,6 +163,8 @@ Only supported on `button` rule, this option will exempt React components called
- `test-selectors/onKeyUp`
- `test-selectors/onSubmit`



## Further Reading

If you don't want these test attributes added in production, you can use something like [babel-plugin-jsx-remove-data-test-id](https://github.com/coderas/babel-plugin-jsx-remove-data-test-id)
Expand Down

0 comments on commit 350d135

Please sign in to comment.