- 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 23 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
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,100 @@ | ||
| /** | ||
| * @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks | ||
| * @author Ben Monro | ||
| */ | ||
| 'use strict'; | ||
| | ||
| 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: function(context) { | ||
| function getCorrectFunctionFor(node, negated = false) { | ||
| return (node.arguments.length === 1 || | ||
| node.arguments[1].value === true || | ||
| node.arguments[1].value === '') && | ||
| !negated | ||
| ? 'toBeDisabled()' | ||
| : 'toBeEnabled()'; | ||
| } | ||
| return { | ||
| "CallExpression[callee.property.name=/toBeEnabled|toBeDisabled/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"( | ||
| node | ||
| ) { | ||
| const incorrectFunction = node.callee.property.name; | ||
| | ||
| const correctFunction = | ||
| incorrectFunction === 'toBeDisabled' ? 'toBeEnabled' : 'toBeDisabled'; | ||
| 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 isDisabledArg = node.arguments[0].value === 'disabled'; | ||
| | ||
| if (isDisabledArg) { | ||
| const correctFunction = getCorrectFunctionFor(node, true); | ||
| | ||
| const incorrectFunction = node.callee.property.name; | ||
| context.report({ | ||
| message: `Use ${correctFunction} instead of not.${incorrectFunction}('disabled')`, | ||
| node, | ||
| fix(fixer) { | ||
| return fixer.replaceTextRange( | ||
benmonro marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| [node.callee.object.property.start, node.end], | ||
| correctFunction | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| "CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]"( | ||
| node | ||
| ) { | ||
| const isDisabledArg = node.arguments[0].value === 'disabled'; | ||
| | ||
| if (isDisabledArg) { | ||
| const correctFunction = getCorrectFunctionFor(node); | ||
| | ||
| const incorrectFunction = node.callee.property.name; | ||
| | ||
| const message = | ||
| node.arguments.length === 1 | ||
| ? `Use toBeDisabled() instead of ${incorrectFunction}('disabled')` | ||
| : `Use ${correctFunction} instead of ${incorrectFunction}(${node.arguments[0].raw}, ${node.arguments[1].raw})`; | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * @fileoverview prefer toBeDisabled or toBeEnabled over attribute checks | ||
| * @author Ben Monro | ||
| */ | ||
| 'use strict'; | ||
| const rule = require('../../../lib/rules/jest-dom-prefer-enabled-disabled'); | ||
| | ||
| const RuleTester = require('eslint').RuleTester; | ||
| | ||
| const ruleTester = new RuleTester({ | ||
| parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
| }); | ||
| ruleTester.run('jest-dom-prefer-enabled-disabled', rule, { | ||
| valid: [ | ||
| `expect(element).toBeDisabled()`, | ||
| `expect(element).toHaveProperty('checked', true)`, | ||
benmonro marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ], | ||
| | ||
| invalid: [ | ||
benmonro marked this conversation as resolved. Outdated Show resolved Hide resolved benmonro marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| { | ||
| code: "expect(element).toHaveProperty('disabled', true)", | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeDisabled() instead of toHaveProperty('disabled', true)", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).toHaveProperty('disabled', false)", | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeEnabled() instead of toHaveProperty('disabled', false)", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeEnabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).toHaveAttribute('disabled', false)", | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeEnabled() instead of toHaveAttribute('disabled', false)", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeEnabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).toHaveProperty('disabled')", | ||
| errors: [ | ||
| { | ||
| message: "Use toBeDisabled() instead of toHaveProperty('disabled')", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).toHaveAttribute('disabled')", | ||
| errors: [ | ||
| { | ||
| message: "Use toBeDisabled() instead of toHaveAttribute('disabled')", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).not.toHaveAttribute('disabled')", | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeEnabled() instead of not.toHaveAttribute('disabled')", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeEnabled()', | ||
| }, | ||
| { | ||
| code: "expect(element).not.toHaveProperty('disabled')", | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeEnabled() instead of not.toHaveProperty('disabled')", | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeEnabled()', | ||
| }, | ||
| { | ||
| code: 'expect(element).not.toBeEnabled()', | ||
| errors: [ | ||
| { | ||
| message: 'Use toBeDisabled() instead of not.toBeEnabled()', | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: 'expect(element).toHaveAttribute("disabled", "")', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Use toBeDisabled() instead of toHaveAttribute("disabled", "")', | ||
| }, | ||
| ], | ||
| output: 'expect(element).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: 'expect(getByText("foo")).toHaveAttribute("disabled", "")', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Use toBeDisabled() instead of toHaveAttribute("disabled", "")', | ||
| }, | ||
| ], | ||
| output: 'expect(getByText("foo")).toBeDisabled()', | ||
| }, | ||
| { | ||
| code: 'expect(getByText("foo")).not.toHaveProperty("disabled")', | ||
| errors: [ | ||
| { | ||
| message: | ||
| "Use toBeEnabled() instead of not.toHaveProperty('disabled')", | ||
| }, | ||
| ], | ||
| output: 'expect(getByText("foo")).toBeEnabled()', | ||
| }, | ||
| ], | ||
| }); | ||
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 🙂