Skip to content

Commit 5ea05bb

Browse files
committed
Update doc for rules
1 parent ab3d081 commit 5ea05bb

File tree

5 files changed

+89
-26
lines changed

5 files changed

+89
-26
lines changed

docs/rules/await-async-query.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,60 @@
11
# Enforce async queries to have proper `await` (await-async-query)
22

3-
TODO: Please describe the origin of the rule here.
3+
Ensure that promises returned by async queries are handled properly.
44

55
## Rule Details
66

7-
This rule aims to...
7+
Some of the queries variants that Testing Library provides are
8+
asynchronous as they return a promise which resolves when elements are
9+
found. Those queries variants are:
10+
11+
- `findBy*`
12+
- `findByAll*`
13+
14+
This rule aims to prevent users from forgetting to await the returned
15+
promise from those async queries to be fulfilled, which could lead to
16+
errors in the tests. The promises can be handled by using either `await`
17+
operator or `then` method.
818

919
Examples of **incorrect** code for this rule:
1020

1121
```js
12-
// TODO: fill me in
22+
const foo = () => {
23+
// ...
24+
const rows = findAllByRole('row');
25+
// ...
26+
};
27+
28+
const bar = () => {
29+
// ...
30+
findByText('submit');
31+
// ...
32+
};
1333
```
1434

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

1737
```js
18-
// TODO: fill me in
38+
// `await` operator is correct
39+
const foo = async () => {
40+
// ...
41+
const rows = await findAllByRole('row');
42+
// ...
43+
};
44+
45+
// `then` method is correct
46+
const bar = () => {
47+
// ...
48+
findByText('submit').then(() => {
49+
// ...
50+
});
51+
};
52+
53+
// return the promise within a function is correct too!
54+
const findMyButton = () => findByText('my button');
1955
```
2056

21-
### Options
22-
23-
TODO: If there are any options, describe them here. Otherwise, delete this section.
24-
25-
## When Not To Use It
26-
27-
TODO: Give a short description of when it would be appropriate to turn off this rule.
28-
2957
## Further Reading
3058

59+
- [Async queries variants](https://testing-library.com/docs/dom-testing-library/api-queries#findby)
3160
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)

docs/rules/no-await-sync-query.md

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
# Disallow unnecessary `await` for sync queries (no-await-sync-query)
22

3-
TODO: Please describe the origin of the rule here.
3+
Ensure sync queries
44

55
## Rule Details
66

7-
This rule aims to...
7+
Usual queries variants that Testing Library provides are synchronous and
8+
don't need to wait for any element. Those queries are:
9+
10+
- `getBy*`
11+
- `getByAll*`
12+
- `queryBy*`
13+
- `queryAllBy*`
14+
15+
This rule aims to prevent users from waiting for synchronous queries.
816

917
Examples of **incorrect** code for this rule:
1018

1119
```js
12-
// TODO: fill me in
20+
const foo = async () => {
21+
// ...
22+
const rows = await queryAllByRole('row');
23+
// ...
24+
};
25+
26+
const bar = () => {
27+
// ...
28+
getByText('submit').then(() => {
29+
// ...
30+
});
31+
};
1332
```
1433

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

1736
```js
18-
// TODO: fill me in
37+
const foo = () => {
38+
// ...
39+
const rows = queryAllByRole('row');
40+
// ...
41+
};
42+
43+
const bar = () => {
44+
// ...
45+
const button = getByText('submit');
46+
// ...
47+
};
1948
```
2049

21-
### Options
22-
23-
TODO: If there are any options, describe them here. Otherwise, delete this section.
24-
25-
## When Not To Use It
26-
27-
TODO: Give a short description of when it would be appropriate to turn off this rule.
28-
2950
## Further Reading
3051

52+
- [Sync queries variants](https://testing-library.com/docs/dom-testing-library/api-queries#variants)
3153
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)

lib/rules/await-async-query.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { getDocsUrl } = require('../utils');
4+
35
const VALID_PARENTS = [
46
'AwaitExpression',
57
'ArrowFunctionExpression',
@@ -15,7 +17,7 @@ module.exports = {
1517
description: 'Enforce async queries to have proper `await`',
1618
category: 'Best Practices',
1719
recommended: true,
18-
url: 'TODO',
20+
url: getDocsUrl('await-async-query'),
1921
},
2022
messages: {
2123
awaitAsyncQuery: '`{{ name }}` must have `await` operator',

lib/rules/no-await-sync-query.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { getDocsUrl } = require('../utils');
4+
35
const SYNC_QUERIES_REGEXP = /^(get|query)(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
46

57
module.exports = {
@@ -9,7 +11,7 @@ module.exports = {
911
description: 'Disallow unnecessary `await` for sync queries',
1012
category: 'Best Practices',
1113
recommended: true,
12-
url: 'TODO',
14+
url: getDocsUrl('no-await-sync-query'),
1315
},
1416
messages: {
1517
noAwaitSyncQuery: '`{{ name }}` does not need `await` operator',

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const getDocsUrl = ruleName =>
4+
`https://github.com/Belco90/eslint-plugin-testing-library/tree/master/docs/rules/${ruleName}.md`;
5+
6+
module.exports = {
7+
getDocsUrl,
8+
};

0 commit comments

Comments
 (0)