Skip to content

Commit 7b6f524

Browse files
authored
feat(prefer-in-document): add support for assigments (#107)
* feat(prefer-in-document): add support for assigments * added coverage * added not use case * spacing * updated docs * fixed spelling mistake * fixed bug with not handling * support awaits * more async support * move check into create * Apply suggestions from code review
1 parent 8bca240 commit 7b6f524

File tree

9 files changed

+316
-84
lines changed

9 files changed

+316
-84
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ learn how: http://kcd.im/pull-request
1818
Relevant code or config
1919

2020
```javascript
21+
2122
```
2223

2324
What you did:

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ module.exports = {
100100
🔧 indicates that a rule is fixable.
101101

102102
<!-- __BEGIN AUTOGENERATED TABLE__ -->
103-
Name | 👍 | 🔧 | Description
104-
----- | ----- | ----- | -----
105-
[prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | 👍 | 🔧 | prefer toBeChecked over checking attributes
106-
[prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | 👍 | 🔧 | Prefer toBeEmpty over checking innerHTML
107-
[prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-enabled-disabled.md) | 👍 | 🔧 | prefer toBeDisabled or toBeEnabled over checking attributes
108-
[prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | 👍 | 🔧 | prefer toHaveFocus over checking document.activeElement
109-
[prefer-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-in-document.md) | | 🔧 | Prefer .toBeInTheDocument() in favor of checking the length of the result using .toHaveLength(1)
110-
[prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties
111-
[prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-attribute.md) | 👍 | 🔧 | prefer toHaveAttribute over checking getAttribute/hasAttribute
112-
[prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style
113-
[prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-text-content.md) | 👍 | 🔧 | Prefer toHaveTextContent over checking element.textContent
103+
104+
| Name | 👍 | 🔧 | Description |
105+
| ---------------------------------------------------------------------------------------------------------------------------------------------- | --- | --- | ------------------------------------------------------------------------------------------------ |
106+
| [prefer-checked](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-checked.md) | 👍 | 🔧 | prefer toBeChecked over checking attributes |
107+
| [prefer-empty](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-empty.md) | 👍 | 🔧 | Prefer toBeEmpty over checking innerHTML |
108+
| [prefer-enabled-disabled](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-enabled-disabled.md) | 👍 | 🔧 | prefer toBeDisabled or toBeEnabled over checking attributes |
109+
| [prefer-focus](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-focus.md) | 👍 | 🔧 | prefer toHaveFocus over checking document.activeElement |
110+
| [prefer-in-document](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-in-document.md) | | 🔧 | Prefer .toBeInTheDocument() in favor of checking the length of the result using .toHaveLength(1) |
111+
| [prefer-required](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-required.md) | 👍 | 🔧 | prefer toBeRequired over checking properties |
112+
| [prefer-to-have-attribute](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-attribute.md) | 👍 | 🔧 | prefer toHaveAttribute over checking getAttribute/hasAttribute |
113+
| [prefer-to-have-style](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-style.md) | 👍 | 🔧 | prefer toHaveStyle over checking element style |
114+
| [prefer-to-have-text-content](https://github.com/testing-library/eslint-plugin-jest-dom/blob/master/docs/rules/prefer-to-have-text-content.md) | 👍 | 🔧 | Prefer toHaveTextContent over checking element.textContent |
115+
114116
<!-- __END AUTOGENERATED TABLE__ -->
115117

116118
## Issues

docs/rules/prefer-in-document.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,32 @@ expect(queryByText("foo")).toBeNull();
1717
expect(queryByText("foo")).not.toBeNull();
1818
expect(queryByText("foo")).toBeDefined();
1919
expect(queryByText("foo")).not.toBeDefined();
20+
21+
const foo = screen.getByText("foo");
22+
expect(foo).toHaveLength(1);
23+
24+
const bar = screen.queryByText("bar");
25+
expect(bar).toHaveLength(0);
2026
```
2127

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

2430
```js
2531
expect(screen.queryByText("foo")).toBeInTheDocument();
26-
expect(screen.queryByText("foo")).toBeInTheDocument();
27-
expect(queryByText("foo")).toBeInTheDocument()`;
28-
expect(wrapper.queryAllByTestId('foo')).toBeInTheDocument()`;
29-
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2)`;
30-
expect(notAQuery('foo-bar')).toHaveLength(1)`;
32+
expect(await screen.findByText("foo")).toBeInTheDocument();
33+
expect(queryByText("foo")).toBeInTheDocument();
34+
expect(wrapper.queryAllByTestId("foo")).toBeInTheDocument();
35+
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2);
36+
expect(notAQuery("foo-bar")).toHaveLength(1);
37+
38+
const foo = screen.getAllByText("foo");
39+
expect(foo).toHaveLength(3);
40+
41+
const bar = screen.queryByText("bar");
42+
expect(bar).not.toBeDefined();
43+
44+
const baz = await screen.findByText("baz");
45+
expect(baz).toBeDefined()
3146
```
3247

3348
## When Not To Use It

other/MAINTAINING.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@ necessary by the git commit messages. With this in mind, **please brush up on
6666

6767
Thank you so much for helping to maintain this project!
6868

69-
[commit]:
70-
https://github.com/conventional-changelog-archived-repos/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md
69+
[commit]: https://github.com/conventional-changelog-archived-repos/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"rules": {
6060
"babel/quotes": "off",
6161
"max-lines-per-function": "off",
62-
"testing-library/no-dom-import": "off"
62+
"testing-library/no-dom-import": "off",
63+
"consistent-return": "off"
6364
}
6465
},
6566
"eslintIgnore": [

src/__tests__/lib/rules/prefer-in-document.js

Lines changed: 149 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//------------------------------------------------------------------------------
99

1010
import { RuleTester } from "eslint";
11-
import { queries, queriesByVariant } from "../../../queries";
1211
import * as rule from "../../../rules/prefer-in-document";
1312

1413
//------------------------------------------------------------------------------
@@ -28,17 +27,29 @@ function invalidCase(code, output) {
2827
}
2928

3029
const valid = [
31-
...queries.map((q) => [
30+
...["getByText", "getByRole"].map((q) => [
3231
`expect(screen.${q}('foo')).toBeInTheDocument()`,
3332
`expect(${q}('foo')).toBeInTheDocument()`,
3433
`expect(wrapper.${q}('foo')).toBeInTheDocument()`,
34+
`let foo;
35+
foo = screen.${q}('foo');
36+
foo = somethingElse;
37+
expect(foo).toHaveLength(1);`,
3538
]),
39+
`let foo;
40+
foo = "bar";
41+
expect(foo).toHaveLength(1);`,
42+
`let foo;
43+
foo = "bar";
44+
expect(foo).toHaveLength(0);`,
45+
`let foo;
46+
expect(foo).toHaveLength(1);`,
3647
`expect(screen.notAQuery('foo-bar')).toHaveLength(1)`,
37-
`expect(screen.getByText('foo-bar')).toHaveLength(2)`,
48+
`expect(screen.getAllByText('foo-bar')).toHaveLength(2)`,
3849
];
3950
const invalid = [
4051
// Invalid cases that applies to all variants
41-
...queries.map((q) => [
52+
...["getByText", "getAllByRole"].map((q) => [
4253
invalidCase(
4354
`expect(screen.${q}('foo')).toHaveLength(1)`,
4455
`expect(screen.${q}('foo')).toBeInTheDocument()`
@@ -51,9 +62,37 @@ const invalid = [
5162
`expect(wrapper.${q}('foo')).toHaveLength(1)`,
5263
`expect(wrapper.${q}('foo')).toBeInTheDocument()`
5364
),
65+
invalidCase(
66+
`const foo = screen.${q}('foo');
67+
expect(foo).toHaveLength(1);`,
68+
`const foo = screen.${q}('foo');
69+
expect(foo).toBeInTheDocument();`
70+
),
71+
invalidCase(
72+
`const foo = ${q}('foo');
73+
expect(foo).toHaveLength(1);`,
74+
`const foo = ${q}('foo');
75+
expect(foo).toBeInTheDocument();`
76+
),
77+
invalidCase(
78+
`let foo;
79+
foo = ${q}('foo');
80+
expect(foo).toHaveLength(1);`,
81+
`let foo;
82+
foo = ${q}('foo');
83+
expect(foo).toBeInTheDocument();`
84+
),
85+
invalidCase(
86+
`let foo;
87+
foo = screen.${q}('foo');
88+
expect(foo).toHaveLength(1);`,
89+
`let foo;
90+
foo = screen.${q}('foo');
91+
expect(foo).toBeInTheDocument();`
92+
),
5493
]),
5594
// Invalid cases that applies to queryBy* and queryAllBy*
56-
...queriesByVariant.query.map((q) => [
95+
...["queryByText", "queryAllByText"].map((q) => [
5796
invalidCase(
5897
`expect(${q}('foo')).toHaveLength(0)`,
5998
`expect(${q}('foo')).not.toBeInTheDocument()`
@@ -66,18 +105,120 @@ const invalid = [
66105
`expect(${q}('foo')).not.toBeNull()`,
67106
`expect(${q}('foo')).toBeInTheDocument()`
68107
),
108+
invalidCase(
109+
`expect(${q}('foo')) .not .toBeNull()`,
110+
`expect(${q}('foo')).toBeInTheDocument()`
111+
),
69112
invalidCase(
70113
`expect(${q}('foo')).toBeDefined()`,
71114
`expect(${q}('foo')).toBeInTheDocument()`
72115
),
73116
invalidCase(
74-
`expect(${q}('foo')).not.toBeDefined()`,
75-
`expect(${q}('foo')).not.toBeInTheDocument()`
117+
`expect(${q}('foo')) .not .toBeDefined()`,
118+
`expect(${q}('foo')) .not .toBeInTheDocument()`
119+
),
120+
invalidCase(
121+
`let foo;
122+
foo = screen.${q}('foo');
123+
expect(foo).toHaveLength(0);`,
124+
`let foo;
125+
foo = screen.${q}('foo');
126+
expect(foo).not.toBeInTheDocument();`
127+
),
128+
invalidCase(
129+
`let foo;
130+
foo = screen.${q}('foo');
131+
expect(foo) .not.toBeNull();`,
132+
`let foo;
133+
foo = screen.${q}('foo');
134+
expect(foo).toBeInTheDocument();`
135+
),
136+
invalidCase(
137+
`let foo = screen.${q}('foo');
138+
expect(foo).not.toBeNull();`,
139+
`let foo = screen.${q}('foo');
140+
expect(foo).toBeInTheDocument();`
76141
),
77142
]),
143+
invalidCase(
144+
`it("foo", async () => {
145+
expect(await findByRole("button")).toBeDefined();
146+
})`,
147+
`it("foo", async () => {
148+
expect(await findByRole("button")).toBeInTheDocument();
149+
})`
150+
),
151+
invalidCase(
152+
`it("foo", async () => {
153+
expect(await findByRole("button")).not.toBeNull();
154+
})`,
155+
`it("foo", async () => {
156+
expect(await findByRole("button")).toBeInTheDocument();
157+
})`
158+
),
159+
invalidCase(
160+
`it("foo", async () => {
161+
expect(await screen.findByText(/Compressing video/)).toBeDefined();
162+
})`,
163+
`it("foo", async () => {
164+
expect(await screen.findByText(/Compressing video/)).toBeInTheDocument();
165+
})`
166+
),
167+
invalidCase(
168+
`it("foo", async () => {
169+
expect(await screen.findByText(/Compressing video/)).not.toBeDefined();
170+
})`,
171+
`it("foo", async () => {
172+
expect(await screen.findByText(/Compressing video/)).not.toBeInTheDocument();
173+
})`
174+
),
175+
invalidCase(
176+
`it("foo", async () => {
177+
const compressingFeedback = await screen.findByText(/Compressing video/);
178+
expect(compressingFeedback).toBeDefined();
179+
});`,
180+
`it("foo", async () => {
181+
const compressingFeedback = await screen.findByText(/Compressing video/);
182+
expect(compressingFeedback).toBeInTheDocument();
183+
});`
184+
),
185+
invalidCase(
186+
`it("foo", async () => {
187+
const compressingFeedback = await screen.findByText(/Compressing video/);
188+
expect(compressingFeedback).not.toBeNull();
189+
});`,
190+
`it("foo", async () => {
191+
const compressingFeedback = await screen.findByText(/Compressing video/);
192+
expect(compressingFeedback).toBeInTheDocument();
193+
});`
194+
),
195+
invalidCase(
196+
`it("foo", async () => {
197+
let compressingFeedback;
198+
compressingFeedback = await screen.findByText(/Compressing video/);
199+
expect(compressingFeedback).toBeDefined();
200+
});`,
201+
`it("foo", async () => {
202+
let compressingFeedback;
203+
compressingFeedback = await screen.findByText(/Compressing video/);
204+
expect(compressingFeedback).toBeInTheDocument();
205+
});`
206+
),
207+
invalidCase(
208+
`it("foo", async () => {
209+
let compressingFeedback;
210+
compressingFeedback = await screen.findByText(/Compressing video/);
211+
expect(compressingFeedback).not.toBeDefined();
212+
});`,
213+
`it("foo", async () => {
214+
let compressingFeedback;
215+
compressingFeedback = await screen.findByText(/Compressing video/);
216+
expect(compressingFeedback).not.toBeInTheDocument();
217+
});`
218+
),
78219
];
79220

80-
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
221+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017 } });
81222
ruleTester.run("prefer-in-document", rule, {
82223
valid: [].concat(...valid),
83224
invalid: [].concat(...invalid),

src/__tests__/lib/rules/prefer-to-have-style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ruleTester.run("prefer-to-have-style", rule, {
2020
invalid: [
2121
{
2222
code: `expect(a.style).toHaveProperty('transform')`,
23-
errors
23+
errors,
2424
},
2525
{
2626
code: `expect(el.style.foo).toBe("bar")`,

src/queries.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
import { queries as allQueries } from "@testing-library/dom";
22

33
export const queries = Object.keys(allQueries);
4-
5-
export const queriesByVariant = {
6-
query: queries.filter((q) => q.startsWith("query")),
7-
get: queries.filter((q) => q.startsWith("get")),
8-
find: queries.filter((q) => q.startsWith("find")),
9-
};

0 commit comments

Comments
 (0)