Skip to content

feat(prefer-in-document): add support for assigments #107

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 14 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
20 changes: 16 additions & 4 deletions docs/rules/prefer-in-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ expect(queryByText("foo")).toBeNull();
expect(queryByText("foo")).not.toBeNull();
expect(queryByText("foo")).toBeDefined();
expect(queryByText("foo")).not.toBeDefined();

const foo = screen.getByText("foo");
expect(foo).toHaveLength(1);

const bar = screen.queryByText("bar");
expect(bar).toHaveLength(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could include some async examples here now that it's properly supported?

```

Examples of **correct** code for this rule:

```js
expect(screen.queryByText("foo")).toBeInTheDocument();
expect(screen.queryByText("foo")).toBeInTheDocument();
expect(queryByText("foo")).toBeInTheDocument()`;
expect(wrapper.queryAllByTestId('foo')).toBeInTheDocument()`;
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2)`;
expect(notAQuery('foo-bar')).toHaveLength(1)`;
expect(queryByText("foo")).toBeInTheDocument();
expect(wrapper.queryAllByTestId("foo")).toBeInTheDocument();
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2);
expect(notAQuery("foo-bar")).toHaveLength(1);

const foo = screen.getAllByText("foo");
expect(foo).toHaveLength(3);

const bar = screen.queryByText("bar");
expect(bar).not.toBeInTheDocument();
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps include some async examples here also, just to be clear.

```

## When Not To Use It
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"rules": {
"babel/quotes": "off",
"max-lines-per-function": "off",
"testing-library/no-dom-import": "off"
"testing-library/no-dom-import": "off",
"consistent-return": "off"
}
},
"eslintIgnore": [
Expand Down
72 changes: 69 additions & 3 deletions src/__tests__/lib/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,21 @@ const valid = [
`expect(screen.${q}('foo')).toBeInTheDocument()`,
`expect(${q}('foo')).toBeInTheDocument()`,
`expect(wrapper.${q}('foo')).toBeInTheDocument()`,
`let foo;
foo = screen.${q}('foo');
foo = somethingElse;
expect(foo).toHaveLength(1);`,
]),
`let foo;
foo = "bar";
expect(foo).toHaveLength(1);`,
`let foo;
foo = "bar";
expect(foo).toHaveLength(0);`,
`let foo;
expect(foo).toHaveLength(1);`,
`expect(screen.notAQuery('foo-bar')).toHaveLength(1)`,
`expect(screen.getByText('foo-bar')).toHaveLength(2)`,
`expect(screen.getAllByText('foo-bar')).toHaveLength(2)`,
];
const invalid = [
// Invalid cases that applies to all variants
Expand All @@ -51,6 +63,34 @@ const invalid = [
`expect(wrapper.${q}('foo')).toHaveLength(1)`,
`expect(wrapper.${q}('foo')).toBeInTheDocument()`
),
invalidCase(
`const foo = screen.${q}('foo');
expect(foo).toHaveLength(1);`,
`const foo = screen.${q}('foo');
expect(foo).toBeInTheDocument();`
),
invalidCase(
`const foo = ${q}('foo');
expect(foo).toHaveLength(1);`,
`const foo = ${q}('foo');
expect(foo).toBeInTheDocument();`
),
invalidCase(
`let foo;
foo = ${q}('foo');
expect(foo).toHaveLength(1);`,
`let foo;
foo = ${q}('foo');
expect(foo).toBeInTheDocument();`
),
invalidCase(
`let foo;
foo = screen.${q}('foo');
expect(foo).toHaveLength(1);`,
`let foo;
foo = screen.${q}('foo');
expect(foo).toBeInTheDocument();`
),
]),
// Invalid cases that applies to queryBy* and queryAllBy*
...queriesByVariant.query.map((q) => [
Expand All @@ -66,13 +106,39 @@ const invalid = [
`expect(${q}('foo')).not.toBeNull()`,
`expect(${q}('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(${q}('foo')) .not .toBeNull()`,
`expect(${q}('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(${q}('foo')).toBeDefined()`,
`expect(${q}('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(${q}('foo')).not.toBeDefined()`,
`expect(${q}('foo')).not.toBeInTheDocument()`
`expect(${q}('foo')) .not .toBeDefined()`,
`expect(${q}('foo')) .not .toBeInTheDocument()`
),
invalidCase(
`let foo;
foo = screen.${q}('foo');
expect(foo).toHaveLength(0);`,
`let foo;
foo = screen.${q}('foo');
expect(foo).not.toBeInTheDocument();`
),
invalidCase(
`let foo;
foo = screen.${q}('foo');
expect(foo) .not.toBeNull();`,
`let foo;
foo = screen.${q}('foo');
expect(foo).toBeInTheDocument();`
),
invalidCase(
`let foo = screen.${q}('foo');
expect(foo).not.toBeNull();`,
`let foo = screen.${q}('foo');
expect(foo).toBeInTheDocument();`
),
]),
];
Expand Down
96 changes: 81 additions & 15 deletions src/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ function isAntonymMatcher(matcherNode, matcherArguments) {

function check(
context,
{ queryNode, matcherNode, matcherArguments, negatedMatcher }
{ queryNode, matcherNode, matcherArguments, negatedMatcher, expect }
) {
const query = queryNode.name || queryNode.property.name;

if (!queryNode || (!queryNode.name && !queryNode.property)) return;
// toHaveLength() is only invalid with 0 or 1
if (matcherNode.name === "toHaveLength" && matcherArguments[0].value > 1) {
return;
}

const query = queryNode.name || queryNode.property.name;

if (queries.includes(query)) {
context.report({
node: matcherNode,
Expand All @@ -46,15 +47,21 @@ function check(
fix(fixer) {
const operations = [];

// Flip the .not if neccessary
// Remove any arguments in the matcher
for (const argument of Array.from(matcherArguments)) {
operations.push(fixer.remove(argument));
}
// Flip the .not if necessary
if (isAntonymMatcher(matcherNode, matcherArguments)) {
if (negatedMatcher) {
operations.push(
fixer.removeRange([
matcherNode.range[0] - 5,
matcherNode.range[0] - 1,
])
fixer.replaceTextRange(
[expect.range[1], matcherNode.range[1]],
".toBeInTheDocument"
)
);

return operations;
} else {
operations.push(fixer.insertTextBefore(matcherNode, "not."));
}
Expand All @@ -63,11 +70,6 @@ function check(
// Replace the actual matcher
operations.push(fixer.replaceText(matcherNode, "toBeInTheDocument"));

// Remove any arguments in the matcher
for (const argument of matcherArguments) {
operations.push(fixer.remove(argument));
}

return operations;
},
});
Expand All @@ -76,25 +78,89 @@ function check(

export const create = (context) => {
const alternativeMatchers = /(toHaveLength|toBeDefined|toBeNull)/;
function getQueryNodeFromAssignment(identifierName) {
const variable = context.getScope().set.get(identifierName);
const init = variable.defs[0].node.init;

let queryNode;
if (init) {
// let foo = screen.<query>();
queryNode = init.callee.property || init.callee;
} else {
// let foo;
// foo = screen.<query>();
const assignmentRef = variable.references
.reverse()
.find((ref) => !!ref.writeExpr);
if (!assignmentRef) {
return;
}
queryNode =
assignmentRef.writeExpr.type === "CallExpression"
? assignmentRef.writeExpr.callee
: assignmentRef.writeExpr;
}
return queryNode;
}
return {
// Grabbing expect(<query>).not.<matcher>
// expect(<query>).not.<matcher>
[`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}]`](
node
) {
const queryNode = node.callee.object.object.arguments[0].callee;
const matcherNode = node.callee.property;
const matcherArguments = node.arguments;

const expect = node.callee.object.object;
check(context, {
negatedMatcher: true,
queryNode,
matcherNode,
matcherArguments,
expect,
});
},

// // const foo = <query> expect(foo).not.<matcher>
[`MemberExpression[object.object.callee.name=expect][object.property.name=not][property.name=${alternativeMatchers}][object.object.arguments.0.type=Identifier]`](
node
) {
const queryNode = getQueryNodeFromAssignment(
node.object.object.arguments[0].name
);
const matcherNode = node.property;

const matcherArguments = node.parent.arguments;

const expect = node.object.object;

check(context, {
negatedMatcher: true,
queryNode,
matcherNode,
matcherArguments,
expect,
});
},
// const foo = <query> expect(foo).<matcher>
[`MemberExpression[object.callee.name=expect][property.name=${alternativeMatchers}][object.arguments.0.type=Identifier]`](
node
) {
const queryNode = getQueryNodeFromAssignment(
node.object.arguments[0].name
);
const matcherNode = node.property;

const matcherArguments = node.parent.arguments;

// Grabbing expect(<query>).<matcher>
check(context, {
negatedMatcher: false,
queryNode,
matcherNode,
matcherArguments,
});
},
// expect(<query>).<matcher>
[`CallExpression[callee.object.callee.name='expect'][callee.property.name=${alternativeMatchers}]`](
node
) {
Expand Down