- Notifications
You must be signed in to change notification settings - Fork 153
feat: add jest-dom-prefer-enabled-disabled, jest-dom-prefer-required, jest-dom-prefer-checked rules #31
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
Closed
benmonro wants to merge 25 commits into testing-library:master from benmonro:feature/prefer-enabled-disabled
Closed
feat: add jest-dom-prefer-enabled-disabled, jest-dom-prefer-required, jest-dom-prefer-checked rules #31
Changes from all commits
Commits
Show all changes
25 commits Select commit Hold shift + click to select a range
4ec21b7
feat: added prefer enabled disabled
fd86cdb
fix: unit test and updated docs
f089edc
feat: added toHaveAttribute
717040f
feat: support for not
68e0daf
feat: don't use not on toBeDisabled/Enabled
59992f9
fix: little refactoring
e02f9f2
chore: addressed PR feedback
fcd267a
fix: added url
3488c5d
docs: added to home page
c8bdc40
docs: changed prefer enabled disabled to recommended
4db94eb
docs: added to recommended
db6cef3
docs: fixed typo in readme
a9a2563
docs: more docs
aff8bc6
Merge branch 'master' into feature/prefer-enabled-disabled
benmonro 86f4972
chore: removed comments
cd83ab4
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro 5e6ca6c
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro 9c72cd3
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro 9ae4277
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro 2a86e3a
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro 4cceff7
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro d0fc8e6
Update docs/rules/jest-dom-prefer-enabled-disabled.md
benmonro ca1fb14
chore: addressed PR feedback
139f88f
fix: added another valid test
ca796a8
feat: added rules for checked & required
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# prefer toBeDisabled() or toBeEnabled() over toHaveProperty('disabled', true|false) (jest-dom-prefer-enabled-disabled) | ||
| ||
## Rule Details | ||
| ||
This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `.toBeEnabled()` or `toBeDisabled()` as appropriate. | ||
| ||
In addition, to avoid double negatives and confusing syntax, `expect(element).not.toBeDisabled()` is also reported and auto-fixed to `expect(element).toBeEnabled()` and vice versa. | ||
| ||
### False positives | ||
| ||
Consider these 2 snippets: | ||
| ||
```js | ||
const { getByRole } = render(<input type="checkbox" disabled />); | ||
const element = getByRole('checkbox'); | ||
expect(element).toHaveProperty('disabled'); // passes | ||
| ||
const { getByRole } = render(<input type="checkbox" />); | ||
const element = getByRole('checkbox'); | ||
expect(element).toHaveProperty('disabled'); // also passes 😱 | ||
``` | ||
| ||
### Readability | ||
| ||
Consider the following snippets: | ||
| ||
```js | ||
const { getByRole } = render(<input type="checkbox" />); | ||
const element = getByRole('checkbox'); | ||
| ||
expect(element).toHaveAttribute('disabled', false); // fails | ||
expect(element).toHaveAttribute('disabled', ''); // fails | ||
expect(element).not.toHaveAttribute('disabled', ''); // passes | ||
| ||
expect(element).not.toHaveAttribute('disabled', true); // passes. | ||
expect(element).not.toHaveAttribute('disabled', false); // also passes. | ||
``` | ||
| ||
As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests. | ||
| ||
Examples of **incorrect** code for this rule: | ||
| ||
```js | ||
expect(element).toHaveProperty('disabled', true); | ||
expect(element).toHaveAttribute('disabled', false); | ||
| ||
expect(element).toHaveAttribute('disabled'); | ||
expect(element).not.toHaveProperty('disabled'); | ||
| ||
expect(element).not.toBeDisabled(); | ||
expect(element).not.toBeEnabled(); | ||
``` | ||
| ||
Examples of **correct** code for this rule: | ||
| ||
```js | ||
expect(element).toBeEnabled(); | ||
| ||
expect(element).toBeDisabled(); | ||
| ||
expect(element).toHaveProperty('checked', true); | ||
| ||
expect(element).toHaveAttribute('checked'); | ||
``` | ||
| ||
## When Not To Use It | ||
| ||
Don't use this rule if you: | ||
| ||
- don't use `jest-dom` | ||
- want to allow `.toHaveProperty('disabled', true|false);` | ||
| ||
## Further reading | ||
| ||
- [toBeDisabled](https://github.com/testing-library/jest-dom#tobedisabled) | ||
- [toBeEnabled](https://github.com/testing-library/jest-dom#tobeenabled) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
module.exports = { | ||
testMatch: ['**/tests/**/*.js'], | ||
collectCoverage: true, | ||
coverageThreshold: { | ||
global: { | ||
branches: 96.55, | ||
functions: 100, | ||
lines: 98.97, | ||
statements: 0, | ||
}, | ||
}, | ||
testPathIgnorePatterns: ['<rootDir>/tests/fixtures/'], | ||
collectCoverageFrom: ['lib/**/*.js', '!**/node_modules/**'], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module.exports = ({ preferred, negatedPreferred, attributes }) => context => { | ||
function getCorrectFunctionFor(node, negated = false) { | ||
return (node.arguments.length === 1 || | ||
node.arguments[1].value === true || | ||
node.arguments[1].value === '') && | ||
!negated | ||
? preferred | ||
: negatedPreferred; | ||
} | ||
| ||
const isBannedArg = node => | ||
attributes.some(attr => attr === node.arguments[0].value); | ||
| ||
return { | ||
[`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`]( | ||
node | ||
) { | ||
if (negatedPreferred.startsWith('toBe')) { | ||
const incorrectFunction = node.callee.property.name; | ||
| ||
const correctFunction = | ||
incorrectFunction === preferred ? negatedPreferred : preferred; | ||
context.report({ | ||
message: `Use ${correctFunction}() instead of not.${incorrectFunction}()`, | ||
node, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[node.callee.object.property.start, node.end], | ||
`${correctFunction}()` | ||
); | ||
}, | ||
}); | ||
} | ||
}, | ||
"CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"( | ||
node | ||
) { | ||
const arg = node.arguments[0].value; | ||
if (isBannedArg(node)) { | ||
const correctFunction = getCorrectFunctionFor(node, true); | ||
| ||
const incorrectFunction = node.callee.property.name; | ||
context.report({ | ||
message: `Use ${correctFunction}() instead of not.${incorrectFunction}('${arg}')`, | ||
node, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[node.callee.object.property.start, node.end], | ||
`${correctFunction}()` | ||
); | ||
}, | ||
}); | ||
} | ||
}, | ||
"CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]"( | ||
node | ||
) { | ||
if (isBannedArg(node)) { | ||
const correctFunction = getCorrectFunctionFor(node); | ||
| ||
const incorrectFunction = node.callee.property.name; | ||
| ||
const message = `Use ${correctFunction}() instead of ${incorrectFunction}(${node.arguments | ||
.map(({ raw }) => raw) | ||
.join(', ')})`; | ||
context.report({ | ||
node: node.callee.property, | ||
message, | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceTextRange( | ||
[node.callee.property.start, node.end], | ||
`${correctFunction}()` | ||
), | ||
]; | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks | ||
* @author Ben Monro | ||
*/ | ||
'use strict'; | ||
| ||
const createBannedAttributeRule = require('../createBannedAttributeRule'); | ||
const { getDocsUrl } = require('../utils'); | ||
| ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'prefer toBeDisabled or toBeEnabled over checking properties', | ||
category: 'jest-dom', | ||
recommended: false, | ||
url: getDocsUrl('jest-dom-prefer-checked'), | ||
}, | ||
fixable: 'code', | ||
}, | ||
| ||
create: createBannedAttributeRule({ | ||
preferred: 'toBeChecked', | ||
negatedPreferred: 'not.toBeChecked', | ||
attributes: ['checked', 'aria-checked'], | ||
}), | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks | ||
* @author Ben Monro | ||
*/ | ||
'use strict'; | ||
| ||
const createBannedAttributeRule = require('../createBannedAttributeRule'); | ||
const { getDocsUrl } = require('../utils'); | ||
| ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'prefer toBeDisabled or toBeEnabled over checking properties', | ||
category: 'jest-dom', | ||
recommended: false, | ||
url: getDocsUrl('jest-dom-prefer-enabled-disabled'), | ||
}, | ||
fixable: 'code', | ||
}, | ||
| ||
create: createBannedAttributeRule({ | ||
preferred: 'toBeDisabled', | ||
negatedPreferred: 'toBeEnabled', | ||
attributes: ['disabled'], | ||
}), | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks | ||
* @author Ben Monro | ||
*/ | ||
'use strict'; | ||
| ||
const createBannedAttributeRule = require('../createBannedAttributeRule'); | ||
const { getDocsUrl } = require('../utils'); | ||
| ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'prefer toBeDisabled or toBeEnabled over checking properties', | ||
category: 'jest-dom', | ||
recommended: false, | ||
url: getDocsUrl('jest-dom-prefer-required'), | ||
}, | ||
fixable: 'code', | ||
}, | ||
| ||
create: createBannedAttributeRule({ | ||
preferred: 'toBeRequired', | ||
negatedPreferred: 'not.toBeRequired', | ||
attributes: ['required', 'aria-required'], | ||
}), | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a purpose to include these JSDocs? The filename seems enough to me 🙂