Skip to content

Commit

Permalink
feat: Matches toBe(null) and toEqual(null) in addition to toBeNull() …
Browse files Browse the repository at this point in the history
…in prefer-document (#153)

Fixes #152
  • Loading branch information
julienw authored Mar 23, 2021
1 parent cdbc37d commit a6600d3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/rules/prefer-in-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ expect(wrapper.queryByText("foo")).toHaveLength(1);
expect(queryByText("foo")).toHaveLength(0);
expect(queryByText("foo")).toBeNull();
expect(queryByText("foo")).not.toBeNull();
expect(queryByText("foo")).toBe(null);
expect(queryByText("foo")).not.toBe(null);
expect(queryByText("foo")).toEqual(null);
expect(queryByText("foo")).not.toEqual(null);
expect(queryByText("foo")).toBeDefined();
expect(queryByText("foo")).not.toBeDefined();

Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const valid = [
`import {NUM_BUTTONS} from "./foo";
expect(screen.getByText('foo')).toHaveLength(NUM_BUTTONS)`,
`expect(screen.getAllByText("foo")).toHaveLength(getLength())`,
`expect(screen.getAllByText("foo")).toBe(foo)`,
`expect(screen.getAllByText("foo")).toEqual(foo)`,
];
const invalid = [
invalidCase(
Expand Down Expand Up @@ -239,6 +241,22 @@ const invalid = [
`expect(queryByText('foo')) .not .toBeNull()`,
`expect(queryByText('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).toBe(null)`,
`expect(queryByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).not.toBe(null)`,
`expect(queryByText('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).toEqual(null)`,
`expect(queryByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).not.toEqual(null)`,
`expect(queryByText('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).toBeDefined()`,
`expect(queryByText('foo')).toBeInTheDocument()`
Expand Down
17 changes: 16 additions & 1 deletion src/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @author Anton Niklasson
*/

/*eslint complexity: ["error", {"max": 20}]*/

import { queries } from "../queries";
import { getAssignmentForIdentifier } from "../assignment-ast";

Expand All @@ -24,16 +26,22 @@ export const meta = {
function isAntonymMatcher(matcherNode, matcherArguments) {
return (
matcherNode.name === "toBeNull" ||
usesToBeOrToEqualWithNull(matcherNode, matcherArguments) ||
usesToHaveLengthZero(matcherNode, matcherArguments)
);
}

function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
return (matcherNode.name === "toBe" || matcherNode.name === "toEqual") &&
matcherArguments[0].value === null;
}

function usesToHaveLengthZero(matcherNode, matcherArguments) {
return matcherNode.name === "toHaveLength" && matcherArguments[0].value === 0;
}

export const create = (context) => {
const alternativeMatchers = /(toHaveLength|toBeDefined|toBeNull)/;
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual)$/;
function getLengthValue(matcherArguments) {
let lengthValue;

Expand Down Expand Up @@ -79,6 +87,13 @@ export const create = (context) => {
}
}

// toBe() or toEqual() are only invalid with null
if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") {
if (!usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) {
return;
}
}

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

if (queries.includes(query)) {
Expand Down

0 comments on commit a6600d3

Please sign in to comment.