55// ------------------------------------------------------------------------------
66
77const rule = require ( '../../../lib/rules/await-async-query' ) ;
8+ const {
9+ SYNC_QUERIES_COMBINATIONS ,
10+ ASYNC_QUERIES_COMBINATIONS ,
11+ } = require ( '../../../lib/utils' ) ;
812const RuleTester = require ( 'eslint' ) . RuleTester ;
913
1014// ------------------------------------------------------------------------------
@@ -14,58 +18,79 @@ const RuleTester = require('eslint').RuleTester;
1418const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2018 } } ) ;
1519ruleTester . run ( 'await-async-query' , rule , {
1620 valid : [
17- {
21+ // async queries declaration are valid
22+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
1823 code : `
19- const { findByText } = setUp()
24+ const { ${ query } } = setUp()
2025 ` ,
21- } ,
22- {
26+ } ) ) ,
27+
28+ // async queries with await operator are valid
29+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
2330 code : `async () => {
24- const foo = await findByText('foo')
31+ doSomething()
32+ await ${ query } ('foo')
2533 }
2634 ` ,
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 => ( {
2948 code : `() => {
30- findByText ('foo').then(node => {
49+ ${ query } ('foo').then(() => {
3150 done()
3251 })
3352 }
3453 ` ,
35- } ,
36- {
54+ } ) ) ,
55+
56+ // async queries with promise in variable and then method are valid
57+ ...ASYNC_QUERIES_COMBINATIONS . map ( query => ( {
3758 code : `() => {
38- const promise = findByText ('foo')
39- promise.then(node => done())
59+ const promise = ${ query } ('foo')
60+ promise.then(() => done())
4061 }
4162 ` ,
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
4780 }
4881 ` ,
49- } ,
50- {
51- code : `async () => {
82+ } ) ) ,
83+
84+ // sync queries are valid
85+ ...SYNC_QUERIES_COMBINATIONS . map ( query => ( {
86+ code : `() => {
5287 doSomething()
53- const foo = await findAllByText ('foo')
88+ ${ query } ('foo')
5489 }
5590 ` ,
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
6994 {
7095 code : `async () => {
7196 doSomething()
@@ -75,41 +100,18 @@ ruleTester.run('await-async-query', rule, {
75100 } ,
76101 ] ,
77102
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 => ( {
91106 code : `async () => {
92107 doSomething()
93- const foo = findByText ('foo')
108+ const foo = ${ query } ('foo')
94109 }
95110 ` ,
96111 errors : [
97112 {
98113 messageId : 'awaitAsyncQuery' ,
99114 } ,
100115 ] ,
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+ } ) ) ,
115117} ) ;
0 commit comments