5
5
// ------------------------------------------------------------------------------
6
6
7
7
const rule = require ( '../../../lib/rules/await-async-query' ) ;
8
+ const {
9
+ SYNC_QUERIES_COMBINATIONS ,
10
+ ASYNC_QUERIES_COMBINATIONS ,
11
+ } = require ( '../../../lib/utils' ) ;
8
12
const RuleTester = require ( 'eslint' ) . RuleTester ;
9
13
10
14
// ------------------------------------------------------------------------------
@@ -14,58 +18,79 @@ const RuleTester = require('eslint').RuleTester;
14
18
const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2018 } } ) ;
15
19
ruleTester . run ( 'await-async-query' , rule , {
16
20
valid : [
17
- {
21
+ // async queries declaration are valid
22
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
18
23
code : `
19
- const { findByText } = setUp()
24
+ const { ${ query } } = setUp()
20
25
` ,
21
- } ,
22
- {
26
+ } ) ) ,
27
+
28
+ // async queries with await operator are valid
29
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
23
30
code : `async () => {
24
- const foo = await findByText('foo')
31
+ doSomething()
32
+ await ${ query } ('foo')
25
33
}
26
34
` ,
27
- } ,
28
- {
35
+ } ) ) ,
36
+
37
+ // async queries with promise in variable and await operator are valid
38
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
39
+ code : `async () => {
40
+ const promise = ${ query } ('foo')
41
+ await promise
42
+ }
43
+ ` ,
44
+ } ) ) ,
45
+
46
+ // async queries with then method are valid
47
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
29
48
code : `() => {
30
- findByText ('foo').then(node => {
49
+ ${ query } ('foo').then(() => {
31
50
done()
32
51
})
33
52
}
34
53
` ,
35
- } ,
36
- {
54
+ } ) ) ,
55
+
56
+ // async queries with promise in variable and then method are valid
57
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
37
58
code : `() => {
38
- const promise = findByText ('foo')
39
- promise.then(node => done())
59
+ const promise = ${ query } ('foo')
60
+ promise.then(() => done())
40
61
}
41
62
` ,
42
- } ,
43
- {
44
- code : `async () => {
45
- doSomething()
46
- const foo = await findByText('foo')
63
+ } ) ) ,
64
+
65
+ // async queries with promise returned in arrow function definition are valid
66
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
67
+ code : `anArrowFunction = () => ${ query } ('foo')` ,
68
+ } ) ) ,
69
+
70
+ // async queries with promise returned in regular function definition are valid
71
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
72
+ code : `function foo() { return ${ query } ('foo') }` ,
73
+ } ) ) ,
74
+
75
+ // async queries with promise in variable and returned in regular function definition are valid
76
+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
77
+ code : `function foo() {
78
+ const promise = ${ query } ('foo')
79
+ return promise
47
80
}
48
81
` ,
49
- } ,
50
- {
51
- code : `async () => {
82
+ } ) ) ,
83
+
84
+ // sync queries are valid
85
+ ...SYNC_QUERIES_COMBINATIONS . map ( query => ( {
86
+ code : `() => {
52
87
doSomething()
53
- const foo = await findAllByText ('foo')
88
+ ${ query } ('foo')
54
89
}
55
90
` ,
56
- } ,
57
- {
58
- code : `anArrowFunction = () => findByText('foo')` ,
59
- } ,
60
- {
61
- code : `function foo() {return findByText('foo')}` ,
62
- } ,
63
- {
64
- code : `function foo() {
65
- const promise = findByText('foo')
66
- return promise
67
- }` ,
68
- } ,
91
+ } ) ) ,
92
+
93
+ // non-existing queries are valid
69
94
{
70
95
code : `async () => {
71
96
doSomething()
@@ -75,41 +100,18 @@ ruleTester.run('await-async-query', rule, {
75
100
} ,
76
101
] ,
77
102
78
- invalid : [
79
- {
80
- code : `async () => {
81
- const foo = findByText('foo')
82
- }
83
- ` ,
84
- errors : [
85
- {
86
- messageId : 'awaitAsyncQuery' ,
87
- } ,
88
- ] ,
89
- } ,
90
- {
103
+ invalid :
104
+ // async queries without await operator or then method are not valid
105
+ ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
91
106
code : `async () => {
92
107
doSomething()
93
- const foo = findByText ('foo')
108
+ const foo = ${ query } ('foo')
94
109
}
95
110
` ,
96
111
errors : [
97
112
{
98
113
messageId : 'awaitAsyncQuery' ,
99
114
} ,
100
115
] ,
101
- } ,
102
- {
103
- code : `async () => {
104
- doSomething()
105
- const foo = findAllByText('foo')
106
- }
107
- ` ,
108
- errors : [
109
- {
110
- messageId : 'awaitAsyncQuery' ,
111
- } ,
112
- ] ,
113
- } ,
114
- ] ,
116
+ } ) ) ,
115
117
} ) ;
0 commit comments