Skip to content

Commit 95cf425

Browse files
author
Ben Monro
committed
docs: added 2 missing docs
1 parent cfc5556 commit 95cf425

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

docs/rules/prefer-checked.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# prefer not.toBeChecked() or not.toBeChecked() over toHaveProperty('checked', true|false) (prefer-enabled-checked)
2+
3+
## Rule Details
4+
5+
This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `.toBeChecked()` or `not.toBeChecked()` as appropriate.
6+
7+
### False positives
8+
9+
Consider these 2 snippets:
10+
11+
```js
12+
const { getByRole } = render(<input type="checkbox" che />);
13+
const element = getByRole('checkbox');
14+
expect(element).toHaveProperty('checked'); // passes
15+
16+
const { getByRole } = render(<input type="checkbox" />);
17+
const element = getByRole('checkbox');
18+
expect(element).toHaveProperty('checked'); // also passes 😱
19+
```
20+
21+
### Readability
22+
23+
Consider the following snippets:
24+
25+
```js
26+
const { getByRole } = render(<input type="checkbox" />);
27+
const element = getByRole('checkbox');
28+
29+
expect(element).toHaveAttribute('checked', false); // fails
30+
expect(element).toHaveAttribute('checked', ''); // fails
31+
expect(element).not.toHaveAttribute('checked', ''); // passes
32+
33+
expect(element).not.toHaveAttribute('checked', true); // passes.
34+
expect(element).not.toHaveAttribute('checked', false); // also passes.
35+
```
36+
37+
As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests.
38+
39+
Examples of **incorrect** code for this rule:
40+
41+
```js
42+
expect(element).toHaveProperty('checked', true);
43+
expect(element).toHaveAttribute('checked', false);
44+
45+
expect(element).toHaveAttribute('checked');
46+
expect(element).not.toHaveProperty('checked');
47+
48+
expect(element).not.not.toBeChecked();
49+
```
50+
51+
Examples of **correct** code for this rule:
52+
53+
```js
54+
expect(element).not.toBeChecked();
55+
56+
expect(element).toBeChecked();
57+
58+
expect(element).toHaveProperty('value', 'foo');
59+
60+
expect(element).toHaveAttribute('aria-label');
61+
```
62+
63+
## When Not To Use It
64+
65+
Don't use this rule if you:
66+
67+
- don't use `jest-dom`
68+
- want to allow `.toHaveProperty('checked', true|false);`
69+
70+
## Further reading
71+
72+
- [not.toBeChecked](https://github.com/testing-library/jest-dom#not.toBeChecked)
73+
- [not.toBeChecked](https://github.com/testing-library/jest-dom#not.toBeChecked)

docs/rules/prefer-required.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# prefer toBeRequired() or not.toBeRequired() over toHaveProperty('required', true|false) (prefer-enabled-required)
2+
3+
## Rule Details
4+
5+
This rule aims to prevent false positives and improve readability and should only be used with the `@testing-library/jest-dom` package. See below for examples of those potential issues and why this rule is recommended. The rule is autofixable and will replace any instances of `.toHaveProperty()` or `.toHaveAttribute()` with `toBeRequired()` or `not.toBeRequired` as appropriate.
6+
7+
### False positives
8+
9+
Consider these 2 snippets:
10+
11+
```js
12+
const { getByRole } = render(<input type="checkbox" required />);
13+
const element = getByRole('checkbox');
14+
expect(element).toHaveProperty('required'); // passes
15+
16+
const { getByRole } = render(<input type="checkbox" />);
17+
const element = getByRole('checkbox');
18+
expect(element).toHaveProperty('required'); // also passes 😱
19+
```
20+
21+
### Readability
22+
23+
Consider the following snippets:
24+
25+
```js
26+
const { getByRole } = render(<input type="checkbox" />);
27+
const element = getByRole('checkbox');
28+
29+
expect(element).toHaveAttribute('required', false); // fails
30+
expect(element).toHaveAttribute('required', ''); // fails
31+
expect(element).not.toHaveAttribute('required', ''); // passes
32+
33+
expect(element).not.toHaveAttribute('required', true); // passes.
34+
expect(element).not.toHaveAttribute('required', false); // also passes.
35+
```
36+
37+
As you can see, using `toHaveAttribute` in this case is confusing, unintuitive and can even lead to false positive tests.
38+
39+
Examples of **incorrect** code for this rule:
40+
41+
```js
42+
expect(element).toHaveProperty('required', true);
43+
expect(element).toHaveAttribute('required', false);
44+
45+
expect(element).toHaveAttribute('required');
46+
expect(element).not.toHaveProperty('required');
47+
48+
expect(element).not.toBeRequired();
49+
expect(element).not.not.toBeRequired();
50+
```
51+
52+
Examples of **correct** code for this rule:
53+
54+
```js
55+
expect(element).not.toBeRequired();
56+
57+
expect(element).toBeRequired();
58+
59+
expect(element).toHaveProperty('aria-label', 'foo');
60+
61+
expect(element).toHaveAttribute('alt');
62+
```
63+
64+
## When Not To Use It
65+
66+
Don't use this rule if you:
67+
68+
- don't use `jest-dom`
69+
- want to allow `.toHaveProperty('required', true|false);`
70+
71+
## Further reading
72+
73+
- [toBeRequired](https://github.com/testing-library/jest-dom#toBeRequired)
74+
- [not.toBeRequired](https://github.com/testing-library/jest-dom#not.toBeRequired)

0 commit comments

Comments
 (0)