Skip to content

fix(prefer-screen-queries): take container into account #150

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

Merged
merged 4 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions lib/rules/prefer-screen-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const RULE_NAME = 'prefer-screen-queries';
export type MessageIds = 'preferScreenQueries';
type Options = [];

const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');
const ALL_QUERIES_COMBINATIONS_REGEXP = [...ALL_QUERIES_COMBINATIONS, 'container'].join('|');

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
Expand Down Expand Up @@ -93,7 +93,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
isIdentifier(node) &&
isMemberExpression(node.parent) &&
isCallExpression(node.parent.object) &&
isIdentifier(node.parent.object.callee) &&
isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within'
) {
reportInvalidUsage(node);
Expand Down
26 changes: 14 additions & 12 deletions tests/lib/rules/prefer-screen-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { ALL_QUERIES_COMBINATIONS } from '../../../lib/utils';

const ruleTester = createRuleTester();

const ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER = [...ALL_QUERIES_COMBINATIONS, 'container']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be better to keep ALL_QUERIES_COMBINATIONS and then use container for its specific scenarios. As this would test as valid scenarios that do not match real tests. For instance, in the line 12 it would translate to

screen.container()

which is not RTL valid code. Same below for

within(component).container()


ruleTester.run(RULE_NAME, rule, {
valid: [
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `screen.${queryMethod}()`,
})),
{
Expand All @@ -15,19 +17,19 @@ ruleTester.run(RULE_NAME, rule, {
{
code: `component.otherFunctionShouldNotThrow()`,
},
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `within(component).${queryMethod}()`,
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `within(screen.${queryMethod}()).${queryMethod}()`,
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const { ${queryMethod} } = within(screen.getByText('foo'))
${queryMethod}(baz)
`,
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const myWithinVariable = within(foo)
myWithinVariable.${queryMethod}('baz')
Expand All @@ -36,7 +38,7 @@ ruleTester.run(RULE_NAME, rule, {
],

invalid: [
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `${queryMethod}()`,
errors: [
{
Expand All @@ -48,7 +50,7 @@ ruleTester.run(RULE_NAME, rule, {
],
})),

...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `render().${queryMethod}()`,
errors: [
{
Expand All @@ -60,7 +62,7 @@ ruleTester.run(RULE_NAME, rule, {
],
})),

...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `component.${queryMethod}()`,
errors: [
{
Expand All @@ -71,7 +73,7 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const { ${queryMethod} } = render()
${queryMethod}(baz)
Expand All @@ -85,7 +87,7 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const myRenderVariable = render()
myRenderVariable.${queryMethod}(baz)
Expand All @@ -99,7 +101,7 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const [myVariable] = render()
myVariable.${queryMethod}(baz)
Expand All @@ -113,7 +115,7 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})),
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
code: `
const [myVariable] = within()
myVariable.${queryMethod}(baz)
Expand Down