Skip to content

Commit b1a08b1

Browse files
committed
refactor(no-unnecessary-act): function to find non-testing-library calls
1 parent 0f2eb34 commit b1a08b1

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

lib/rules/no-unnecessary-act.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,26 @@ export default createTestingLibraryRule<[], MessageIds>({
3333

3434
create(context, _, helpers) {
3535
/**
36-
* Determines whether a given list of statements has some call non-related to Testing Library utils.
36+
* Determines whether some call is non Testing Library related for a given list of statements.
3737
*/
38-
function hasNonTestingLibraryCall(
38+
function hasSomeNonTestingLibraryCall(
3939
statements: TSESTree.Statement[]
4040
): boolean {
41-
// TODO: refactor to use Array.every
42-
for (const statement of statements) {
41+
return statements.some((statement) => {
4342
const callExpression = getStatementCallExpression(statement);
4443

4544
if (!callExpression) {
46-
continue;
45+
return false;
4746
}
4847

4948
const identifier = getDeepestIdentifierNode(callExpression);
5049

5150
if (!identifier) {
52-
continue;
51+
return false;
5352
}
5453

55-
if (helpers.isTestingLibraryUtil(identifier)) {
56-
continue;
57-
}
58-
59-
// at this point the statement is a non testing library call
60-
return true;
61-
}
62-
return false;
54+
return !helpers.isTestingLibraryUtil(identifier);
55+
});
6356
}
6457

6558
function checkNoUnnecessaryActFromBlockStatement(
@@ -94,18 +87,12 @@ export default createTestingLibraryRule<[], MessageIds>({
9487
node: callExpressionIdentifier,
9588
messageId: 'noUnnecessaryActEmptyFunction',
9689
});
97-
98-
return;
99-
}
100-
101-
if (hasNonTestingLibraryCall(blockStatementNode.body)) {
102-
return;
90+
} else if (!hasSomeNonTestingLibraryCall(blockStatementNode.body)) {
91+
context.report({
92+
node: callExpressionIdentifier,
93+
messageId: 'noUnnecessaryActTestingLibraryUtil',
94+
});
10395
}
104-
105-
context.report({
106-
node: callExpressionIdentifier,
107-
messageId: 'noUnnecessaryActTestingLibraryUtil',
108-
});
10996
}
11097

11198
function checkNoUnnecessaryActFromImplicitReturn(

tests/lib/rules/no-unnecessary-act.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,6 @@ ruleTester.run(RULE_NAME, rule, {
373373
// TODO case: RTU act wrapping RTL calls - callbacks with return
374374
// TODO case: RTU act wrapping empty callback
375375

376-
// TODO cases like previous ones but with AGR disabled
376+
// TODO case: mixed scenarios - AGR disabled
377377
],
378378
});

0 commit comments

Comments
 (0)