Skip to content

Commit c7aa60f

Browse files
committed
chore(consistent-data-testid): address pr feedback
1 parent d3d38b6 commit c7aa60f

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ To enable this configuration use the `extends` property in your
138138
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
139139
| [prefer-expect-query-by](docs/rules/prefer-expect-query-by.md) | Disallow the use of `expect(getBy*)` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
140140
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
141-
| [data-testid](docs/rules/data-testid.md) | Ensure `data-testid` values match a provided regex. | | |
141+
| [consistent-data-testid](docs/rules/consistent-data-testid.md) | Ensure `data-testid` values match a provided regex. | | |
142142

143143
[build-badge]: https://img.shields.io/travis/Belco90/eslint-plugin-testing-library?style=flat-square
144144
[build-url]: https://travis-ci.org/belco90/eslint-plugin-testing-library

docs/rules/data-testid.md renamed to docs/rules/consistent-data-testid.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Enforces consistent naming for the data-testid attribute (data-testid)
1+
# Enforces consistent naming for the data-testid attribute (consistent-data-testid)
22

33
Ensure `data-testid` values match a provided regex. This rule is un-opinionated, and requires configuration.
44

@@ -18,18 +18,15 @@ Examples of **correct** code for this rule:
1818

1919
```js
2020
const foo = props => <div data-testid="TestId__EXAMPLE">...</div>;
21-
2221
const bar = props => <div data-testid="TestId">...</div>;
23-
2422
const baz = props => <div>...</div>;
2523
```
2624

2725
## Options
2826

29-
| Option | Details | Example |
30-
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |
31-
| testIdPattern | A regex used to validate the format of the `data-testid` value. `{componentName}` can optionally be used as a placeholder and will be substituted with the name of the file OR the name of the files parent directory in the case when the fileName is `index.js` | `'^{componentName}(\_\_([A-Z]+[a-z]_?)+)_\$'` |
32-
| excludePaths | An array of path strings to exclude from the check | `["__tests__"]` |
27+
| Option | Details | Example |
28+
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------- |
29+
| testIdPattern | A regex used to validate the format of the `data-testid` value. `{fileName}` can optionally be used as a placeholder and will be substituted with the name of the file OR the name of the files parent directory in the case when the fileName is `index.js` | `'^{fileName}(\_\_([A-Z]+[a-z]_?)+)_\$'` |
3330

3431
## Example
3532

@@ -38,8 +35,7 @@ const baz = props => <div>...</div>;
3835
"testing-library/data-testid": [
3936
2,
4037
{
41-
"testIdPattern": "^TestId(__[A-Z]*)?$",
42-
"excludePaths": ["__tests__"]
38+
"testIdPattern": "^TestId(__[A-Z]*)?$"
4339
}
4440
]
4541
}

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const rules = {
44
'await-async-query': require('./rules/await-async-query'),
55
'await-fire-event': require('./rules/await-fire-event'),
6-
'data-testid': require('./rules/data-testid'),
6+
'consistent-data-testid': require('./rules/consistent-data-testid'),
77
'no-await-sync-query': require('./rules/no-await-sync-query'),
88
'no-debug': require('./rules/no-debug'),
99
'no-dom-import': require('./rules/no-dom-import'),

lib/rules/data-testid.js renamed to lib/rules/consistent-data-testid.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ module.exports = {
2020
type: 'string',
2121
default: '',
2222
},
23-
excludePaths: {
24-
type: 'array',
25-
items: { type: 'string' },
26-
default: [],
27-
},
2823
},
2924
},
3025
],
@@ -35,37 +30,30 @@ module.exports = {
3530
const defaultOptions = { testIdPattern: '', excludePaths: [] };
3631
const ruleOptions = options.length ? options[0] : defaultOptions;
3732

38-
function getComponentData() {
33+
function getFileNameData() {
3934
const splitPath = getFilename().split('/');
40-
const exclude = ruleOptions.excludePaths.some(path =>
41-
splitPath.includes(path)
42-
);
4335
const fileNameWithExtension = splitPath.pop();
4436
const parent = splitPath.pop();
4537
const fileName = fileNameWithExtension.split('.').shift();
4638

4739
return {
48-
componentDescriptor: fileName === 'index' ? parent : fileName,
49-
exclude,
40+
fileName: fileName === 'index' ? parent : fileName,
5041
};
5142
}
5243

53-
function getTestIdValidator({ componentName }) {
44+
function getTestIdValidator({ fileName }) {
5445
return new RegExp(
55-
ruleOptions.testIdPattern.replace('{componentName}', componentName)
46+
ruleOptions.testIdPattern.replace('{fileName}', fileName)
5647
);
5748
}
5849

5950
return {
6051
'JSXIdentifier[name=data-testid]': node => {
6152
const { value } = (node && node.parent && node.parent.value) || {};
62-
const {
63-
componentDescriptor: componentName,
64-
exclude,
65-
} = getComponentData();
66-
const regex = getTestIdValidator({ componentName });
53+
const { fileName } = getFileNameData();
54+
const regex = getTestIdValidator({ fileName });
6755

68-
if (!exclude && value && !regex.test(value)) {
56+
if (value && !regex.test(value)) {
6957
context.report({
7058
node,
7159
messageId: 'invalidTestId',

tests/lib/rules/data-testid.js renamed to tests/lib/rules/consistent-data-testid.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Requirements
55
// ------------------------------------------------------------------------------
66

7-
const rule = require('../../../lib/rules/data-testid');
7+
const rule = require('../../../lib/rules/consistent-data-testid');
88
const RuleTester = require('eslint').RuleTester;
99

1010
// ------------------------------------------------------------------------------
@@ -20,7 +20,7 @@ const parserOptions = {
2020
};
2121

2222
const ruleTester = new RuleTester({ parserOptions });
23-
ruleTester.run('data-testid', rule, {
23+
ruleTester.run('consistent-data-testid', rule, {
2424
valid: [
2525
{
2626
code: `
@@ -78,7 +78,7 @@ ruleTester.run('data-testid', rule, {
7878
`,
7979
options: [
8080
{
81-
testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
81+
testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
8282
},
8383
],
8484
filename: '/my/cool/file/path/Awesome.js',
@@ -97,7 +97,7 @@ ruleTester.run('data-testid', rule, {
9797
`,
9898
options: [
9999
{
100-
testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
100+
testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
101101
},
102102
],
103103
filename: '/my/cool/file/path/Awesome.js',
@@ -116,7 +116,7 @@ ruleTester.run('data-testid', rule, {
116116
`,
117117
options: [
118118
{
119-
testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
119+
testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
120120
},
121121
],
122122
filename: '/my/cool/file/Parent/index.js',
@@ -135,8 +135,26 @@ ruleTester.run('data-testid', rule, {
135135
`,
136136
options: [
137137
{
138-
testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
139-
excludePaths: ['__tests__'],
138+
testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
139+
},
140+
],
141+
filename: '/my/cool/__tests__/Parent/index.js',
142+
},
143+
{
144+
code: `
145+
import React from 'react';
146+
147+
const TestComponent = props => {
148+
return (
149+
<div data-testid="Parent">
150+
Hello
151+
</div>
152+
)
153+
};
154+
`,
155+
options: [
156+
{
157+
testIdPattern: '{fileName}',
140158
},
141159
],
142160
filename: '/my/cool/__tests__/Parent/index.js',
@@ -177,7 +195,6 @@ ruleTester.run('data-testid', rule, {
177195
options: [
178196
{
179197
testIdPattern: 'matchMe',
180-
excludePaths: ['__mocks__'],
181198
},
182199
],
183200
filename: '/my/cool/__tests__/Parent/index.js',
@@ -201,8 +218,7 @@ ruleTester.run('data-testid', rule, {
201218
`,
202219
options: [
203220
{
204-
testIdPattern: '^{componentName}(__([A-Z]+[a-z]*?)+)*$',
205-
excludePaths: ['__mocks__'],
221+
testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
206222
},
207223
],
208224
filename: '/my/cool/__tests__/Parent/index.js',

0 commit comments

Comments
 (0)