Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple test attributes #19

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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