Skip to content

Commit 276b770

Browse files
committed
test: add tests
1 parent 0cbc78e commit 276b770

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

tests/lib/rules/await-async-utils.test.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ ruleTester.run(RULE_NAME, rule, {
416416
data: { name: asyncUtil },
417417
},
418418
],
419+
output: `
420+
import { ${asyncUtil} } from '${testingFramework}';
421+
test('${asyncUtil} util not waited is invalid', async () => {
422+
doSomethingElse();
423+
await ${asyncUtil}(() => getByLabelText('email'));
424+
});
425+
`,
419426
})),
420427
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
421428
code: `
@@ -433,6 +440,13 @@ ruleTester.run(RULE_NAME, rule, {
433440
data: { name: asyncUtil },
434441
},
435442
],
443+
output: `
444+
import { ${asyncUtil} } from '${testingFramework}';
445+
test('${asyncUtil} util not waited is invalid', async () => {
446+
doSomethingElse();
447+
const el = await ${asyncUtil}(() => getByLabelText('email'));
448+
});
449+
`,
436450
})),
437451
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
438452
code: `
@@ -450,6 +464,13 @@ ruleTester.run(RULE_NAME, rule, {
450464
data: { name: asyncUtil },
451465
},
452466
],
467+
output: `
468+
import * as asyncUtil from '${testingFramework}';
469+
test('asyncUtil.${asyncUtil} util not handled is invalid', async () => {
470+
doSomethingElse();
471+
await asyncUtil.${asyncUtil}(() => getByLabelText('email'));
472+
});
473+
`,
453474
})),
454475
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
455476
code: `
@@ -467,6 +488,13 @@ ruleTester.run(RULE_NAME, rule, {
467488
data: { name: asyncUtil },
468489
},
469490
],
491+
output: `
492+
import { ${asyncUtil} } from '${testingFramework}';
493+
test('${asyncUtil} util promise saved not handled is invalid', async () => {
494+
doSomethingElse();
495+
const aPromise = await ${asyncUtil}(() => getByLabelText('email'));
496+
});
497+
`,
470498
})),
471499
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
472500
code: `
@@ -491,6 +519,14 @@ ruleTester.run(RULE_NAME, rule, {
491519
data: { name: asyncUtil },
492520
},
493521
],
522+
output: `
523+
import { ${asyncUtil} } from '${testingFramework}';
524+
test('several ${asyncUtil} utils not handled are invalid', async () => {
525+
const aPromise = ${asyncUtil}(() => getByLabelText('username'));
526+
doSomethingElse(await aPromise);
527+
await ${asyncUtil}(() => getByLabelText('email'));
528+
});
529+
`,
494530
})),
495531
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
496532
code: `
@@ -515,6 +551,14 @@ ruleTester.run(RULE_NAME, rule, {
515551
data: { name: asyncUtil },
516552
},
517553
],
554+
output: `
555+
import { ${asyncUtil} } from '${testingFramework}';
556+
test('unhandled expression that evaluates to promise is invalid', async () => {
557+
const aPromise = ${asyncUtil}(() => getByLabelText('username'));
558+
doSomethingElse(await aPromise);
559+
await ${asyncUtil}(() => getByLabelText('email'));
560+
});
561+
`,
518562
})),
519563
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
520564
code: `
@@ -537,6 +581,18 @@ ruleTester.run(RULE_NAME, rule, {
537581
data: { name: 'waitForSomethingAsync' },
538582
},
539583
],
584+
output: `
585+
import { ${asyncUtil}, render } from '${testingFramework}';
586+
587+
function waitForSomethingAsync() {
588+
return ${asyncUtil}(() => somethingAsync())
589+
}
590+
591+
test('unhandled promise from function wrapping ${asyncUtil} util is invalid', async () => {
592+
render()
593+
await waitForSomethingAsync()
594+
});
595+
`,
540596
})),
541597
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
542598
code: `
@@ -560,6 +616,19 @@ ruleTester.run(RULE_NAME, rule, {
560616
data: { name: 'waitForSomethingAsync' },
561617
},
562618
],
619+
output: `
620+
import { ${asyncUtil}, render } from '${testingFramework}';
621+
622+
function waitForSomethingAsync() {
623+
return ${asyncUtil}(() => somethingAsync())
624+
}
625+
626+
test('unhandled promise in variable declaration from function wrapping ${asyncUtil} util is invalid', async () => {
627+
render()
628+
const result = waitForSomethingAsync()
629+
expect(await result).toBe('foo')
630+
});
631+
`,
563632
})),
564633
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
565634
code: `
@@ -579,6 +648,15 @@ ruleTester.run(RULE_NAME, rule, {
579648
data: { name: asyncUtil },
580649
},
581650
],
651+
output: `
652+
import { ${asyncUtil} } from 'some-other-library'; // rather than ${testingFramework}
653+
test(
654+
'aggressive reporting - util "${asyncUtil}" which is not related to testing library is invalid',
655+
async () => {
656+
doSomethingElse();
657+
await ${asyncUtil}();
658+
});
659+
`,
582660
})),
583661
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
584662
code: `
@@ -601,6 +679,18 @@ ruleTester.run(RULE_NAME, rule, {
601679
data: { name: 'waitForSomethingAsync' },
602680
},
603681
],
682+
output: `
683+
import { ${asyncUtil}, render } from '${testingFramework}';
684+
685+
function waitForSomethingAsync() {
686+
return ${asyncUtil}(() => somethingAsync())
687+
}
688+
689+
test('unhandled promise from function wrapping ${asyncUtil} util is invalid', async () => {
690+
render()
691+
const el = await waitForSomethingAsync()
692+
});
693+
`,
604694
})),
605695

606696
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
@@ -621,6 +711,15 @@ ruleTester.run(RULE_NAME, rule, {
621711
data: { name: asyncUtil },
622712
},
623713
],
714+
output: `
715+
import * as asyncUtils from 'some-other-library'; // rather than ${testingFramework}
716+
test(
717+
'aggressive reporting - util "asyncUtils.${asyncUtil}" which is not related to testing library is invalid',
718+
async () => {
719+
doSomethingElse();
720+
await asyncUtils.${asyncUtil}();
721+
});
722+
`,
624723
})),
625724
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
626725
code: `
@@ -648,6 +747,23 @@ ruleTester.run(RULE_NAME, rule, {
648747
data: { name: 'waitForAsyncUtil' },
649748
},
650749
],
750+
output: `
751+
// implicitly using ${testingFramework}
752+
function setup() {
753+
const utils = render(<MyComponent />);
754+
755+
const waitForAsyncUtil = () => {
756+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
757+
};
758+
759+
return { waitForAsyncUtil, ...utils };
760+
}
761+
762+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
763+
const { user, waitForAsyncUtil } = setup();
764+
await waitForAsyncUtil();
765+
});
766+
`,
651767
})),
652768
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
653769
code: `
@@ -676,6 +792,24 @@ ruleTester.run(RULE_NAME, rule, {
676792
data: { name: 'myAlias' },
677793
},
678794
],
795+
output: `
796+
// implicitly using ${testingFramework}
797+
function setup() {
798+
const utils = render(<MyComponent />);
799+
800+
const waitForAsyncUtil = () => {
801+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
802+
};
803+
804+
return { waitForAsyncUtil, ...utils };
805+
}
806+
807+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
808+
const { user, waitForAsyncUtil } = setup();
809+
const myAlias = waitForAsyncUtil;
810+
await myAlias();
811+
});
812+
`,
679813
})),
680814
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
681815
code: `
@@ -703,6 +837,23 @@ ruleTester.run(RULE_NAME, rule, {
703837
data: { name: 'waitForAsyncUtil' },
704838
},
705839
],
840+
output: `
841+
// implicitly using ${testingFramework}
842+
function setup() {
843+
const utils = render(<MyComponent />);
844+
845+
const waitForAsyncUtil = () => {
846+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
847+
};
848+
849+
return { waitForAsyncUtil, ...utils };
850+
}
851+
852+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
853+
const { ...clone } = setup();
854+
await clone.waitForAsyncUtil();
855+
});
856+
`,
706857
})),
707858
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
708859
code: `
@@ -730,6 +881,23 @@ ruleTester.run(RULE_NAME, rule, {
730881
data: { name: 'myAlias' },
731882
},
732883
],
884+
output: `
885+
// implicitly using ${testingFramework}
886+
function setup() {
887+
const utils = render(<MyComponent />);
888+
889+
const waitForAsyncUtil = () => {
890+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
891+
};
892+
893+
return { waitForAsyncUtil, ...utils };
894+
}
895+
896+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
897+
const { waitForAsyncUtil: myAlias } = setup();
898+
await myAlias();
899+
});
900+
`,
733901
})),
734902
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
735903
code: `
@@ -756,6 +924,22 @@ ruleTester.run(RULE_NAME, rule, {
756924
data: { name: 'waitForAsyncUtil' },
757925
},
758926
],
927+
output: `
928+
// implicitly using ${testingFramework}
929+
function setup() {
930+
const utils = render(<MyComponent />);
931+
932+
const waitForAsyncUtil = () => {
933+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
934+
};
935+
936+
return { waitForAsyncUtil, ...utils };
937+
}
938+
939+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
940+
await setup().waitForAsyncUtil();
941+
});
942+
`,
759943
})),
760944
...ASYNC_UTILS.map<RuleInvalidTestCase>((asyncUtil) => ({
761945
code: `
@@ -783,6 +967,23 @@ ruleTester.run(RULE_NAME, rule, {
783967
data: { name: 'myAlias' },
784968
},
785969
],
970+
output: `
971+
// implicitly using ${testingFramework}
972+
function setup() {
973+
const utils = render(<MyComponent />);
974+
975+
const waitForAsyncUtil = () => {
976+
return ${asyncUtil}(screen.queryByTestId('my-test-id'));
977+
};
978+
979+
return { waitForAsyncUtil, ...utils };
980+
}
981+
982+
test('unhandled promise from destructed property of async function wrapper is invalid', async () => {
983+
const myAlias = setup().waitForAsyncUtil;
984+
await myAlias();
985+
});
986+
`,
786987
})),
787988
]),
788989
});

0 commit comments

Comments
 (0)