-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
336 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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,44 @@ | ||
# Disallow the use of `container` methods (no-container) | ||
|
||
By using `container` methods like `.querySelector` you may lose a lot of the confidence that the user can really interact with your UI. Also, the test becomes harder to read, and it will break more frequently. | ||
|
||
This applies to Testing Library frameworks built on top of **DOM Testing Library** | ||
|
||
## Rule Details | ||
|
||
This rule aims to disallow the use of `container` methods in your tests. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
const { container } = render(<Example />); | ||
const button = container.querySelector('.btn-primary'); | ||
``` | ||
|
||
```js | ||
const { container: alias } = render(<Example />); | ||
const button = alias.querySelector('.btn-primary'); | ||
``` | ||
|
||
```js | ||
const view = render(<Example />); | ||
const button = view.container.getElementsByClassName('.btn-primary'); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
render(<Example />); | ||
screen.getByRole('button', { name: /click me/i }); | ||
``` | ||
|
||
If you use [custom render functions](https://testing-library.com/docs/example-react-redux) then you can set a config option in your `.eslintrc` to look for these. | ||
|
||
``` | ||
"testing-library/no-container": ["error", {"renderFunctions":["renderWithRedux", "renderWithRouter"]}], | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [about the `container` element](https://testing-library.com/docs/react-testing-library/api#container-1) | ||
- [querying with `screen`](https://testing-library.com/docs/dom-testing-library/api-queries#screen) |
This file contains 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 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 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,120 @@ | ||
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { getDocsUrl } from '../utils'; | ||
import { | ||
isIdentifier, | ||
isMemberExpression, | ||
isObjectPattern, | ||
isProperty, | ||
isRenderVariableDeclarator, | ||
} from '../node-utils'; | ||
|
||
export const RULE_NAME = 'no-container'; | ||
|
||
export default ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow the use of container methods', | ||
category: 'Best Practices', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
noContainer: | ||
'Avoid using container methods. Prefer using the methods from Testing Library, such as "getByRole()"', | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
renderFunctions: { | ||
type: 'array', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [ | ||
{ | ||
renderFunctions: [], | ||
}, | ||
], | ||
|
||
create(context, [options]) { | ||
const { renderFunctions } = options; | ||
const destructuredContainerPropNames: string[] = []; | ||
let renderWrapperName: string = null; | ||
let containerName: string = null; | ||
let containerCallsMethod = false; | ||
|
||
function showErrorIfChainedContainerMethod( | ||
innerNode: TSESTree.MemberExpression | ||
) { | ||
if (isMemberExpression(innerNode)) { | ||
if (isIdentifier(innerNode.object)) { | ||
const isContainerName = innerNode.object.name === containerName; | ||
const isRenderWrapper = innerNode.object.name === renderWrapperName; | ||
|
||
containerCallsMethod = | ||
isIdentifier(innerNode.property) && | ||
innerNode.property.name === 'container' && | ||
isRenderWrapper; | ||
|
||
if (isContainerName || containerCallsMethod) { | ||
context.report({ | ||
node: innerNode, | ||
messageId: 'noContainer', | ||
}); | ||
} | ||
} | ||
showErrorIfChainedContainerMethod( | ||
innerNode.object as TSESTree.MemberExpression | ||
); | ||
} | ||
} | ||
|
||
return { | ||
VariableDeclarator(node) { | ||
if (isRenderVariableDeclarator(node, renderFunctions)) { | ||
if (isObjectPattern(node.id)) { | ||
const containerIndex = node.id.properties.findIndex( | ||
property => | ||
isProperty(property) && | ||
isIdentifier(property.key) && | ||
property.key.name === 'container' | ||
); | ||
const nodeValue = | ||
containerIndex !== -1 && node.id.properties[containerIndex].value; | ||
if (isIdentifier(nodeValue)) { | ||
containerName = nodeValue.name; | ||
} else { | ||
isObjectPattern(nodeValue) && | ||
nodeValue.properties.forEach( | ||
property => | ||
isProperty(property) && | ||
isIdentifier(property.key) && | ||
destructuredContainerPropNames.push(property.key.name) | ||
); | ||
} | ||
} else { | ||
renderWrapperName = isIdentifier(node.id) && node.id.name; | ||
} | ||
} | ||
}, | ||
|
||
CallExpression(node: TSESTree.CallExpression) { | ||
if (isMemberExpression(node.callee)) { | ||
showErrorIfChainedContainerMethod(node.callee); | ||
} else { | ||
isIdentifier(node.callee) && | ||
destructuredContainerPropNames.includes(node.callee.name) && | ||
context.report({ | ||
node, | ||
messageId: 'noContainer', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains 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 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
Oops, something went wrong.