Skip to content

Commit 4d152aa

Browse files
committed
refactor: use isTestingLibraryModule for detection logic
1 parent ca754ce commit 4d152aa

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lib/create-testing-library-rule/detect-testing-library-utils.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import {
2323
ASYNC_UTILS,
2424
DEBUG_UTILS,
2525
PRESENCE_MATCHERS,
26+
USER_EVENT_MODULE,
2627
} from '../utils';
28+
import {
29+
isCustomTestingLibraryModule,
30+
isOfficialTestingLibraryModule,
31+
isTestingLibraryModule,
32+
} from '../utils/is-testing-library-module';
2733

2834
const SETTING_OPTION_OFF = 'off';
2935

@@ -133,7 +139,6 @@ export interface DetectionHelpers {
133139
isNodeComingFromTestingLibrary: IsNodeComingFromTestingLibraryFn;
134140
}
135141

136-
const USER_EVENT_PACKAGE = '@testing-library/user-event';
137142
const REACT_DOM_TEST_UTILS_PACKAGE = 'react-dom/test-utils';
138143
const FIRE_EVENT_NAME = 'fireEvent';
139144
const CREATE_EVENT_NAME = 'createEvent';
@@ -960,12 +965,11 @@ export function detectTestingLibraryUtils<
960965
}
961966

962967
const hasImportElementMatch = hasImportMatch(importNode, identifierName);
963-
const hasImportModuleMatch =
964-
/testing-library/g.test(importDeclarationName) ||
965-
(typeof customModuleSetting === 'string' &&
966-
importDeclarationName.endsWith(customModuleSetting));
967968

968-
return hasImportElementMatch && hasImportModuleMatch;
969+
return (
970+
hasImportElementMatch &&
971+
isTestingLibraryModule(importDeclarationName, customModuleSetting)
972+
);
969973
};
970974

971975
const helpers: DetectionHelpers = {
@@ -1017,17 +1021,16 @@ export function detectTestingLibraryUtils<
10171021
}
10181022
// check only if testing library import not found yet so we avoid
10191023
// to override importedTestingLibraryNodes after it's found
1020-
if (/testing-library/g.test(node.source.value)) {
1024+
if (isOfficialTestingLibraryModule(node.source.value)) {
10211025
importedTestingLibraryNodes.push(node);
10221026
}
10231027

10241028
// check only if custom module import not found yet so we avoid
10251029
// to override importedCustomModuleNode after it's found
10261030
const customModule = getCustomModule();
10271031
if (
1028-
customModule &&
10291032
!importedCustomModuleNode &&
1030-
node.source.value.endsWith(customModule)
1033+
isCustomTestingLibraryModule(node.source.value, customModule)
10311034
) {
10321035
importedCustomModuleNode = node;
10331036
}
@@ -1036,7 +1039,7 @@ export function detectTestingLibraryUtils<
10361039
// to override importedUserEventLibraryNode after it's found
10371040
if (
10381041
!importedUserEventLibraryNode &&
1039-
node.source.value === USER_EVENT_PACKAGE
1042+
node.source.value === USER_EVENT_MODULE
10401043
) {
10411044
importedUserEventLibraryNode = node;
10421045
}
@@ -1063,7 +1066,7 @@ export function detectTestingLibraryUtils<
10631066
(arg) =>
10641067
isLiteral(arg) &&
10651068
typeof arg.value === 'string' &&
1066-
/testing-library/g.test(arg.value)
1069+
isOfficialTestingLibraryModule(arg.value)
10671070
)
10681071
) {
10691072
importedTestingLibraryNodes.push(callExpression);
@@ -1074,10 +1077,9 @@ export function detectTestingLibraryUtils<
10741077
!importedCustomModuleNode &&
10751078
args.some(
10761079
(arg) =>
1077-
customModule &&
10781080
isLiteral(arg) &&
10791081
typeof arg.value === 'string' &&
1080-
arg.value.endsWith(customModule)
1082+
isCustomTestingLibraryModule(arg.value, customModule)
10811083
)
10821084
) {
10831085
importedCustomModuleNode = callExpression;
@@ -1089,7 +1091,7 @@ export function detectTestingLibraryUtils<
10891091
(arg) =>
10901092
isLiteral(arg) &&
10911093
typeof arg.value === 'string' &&
1092-
arg.value === USER_EVENT_PACKAGE
1094+
arg.value === USER_EVENT_MODULE
10931095
)
10941096
) {
10951097
importedUserEventLibraryNode = callExpression;

lib/utils/resolve-to-testing-library-fn.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
isSupportedAccessor,
2525
} from '../node-utils/accessors';
2626

27-
import { LIBRARY_MODULES, USER_EVENT_MODULE } from '.';
27+
import { isTestingLibraryModule } from './is-testing-library-module';
2828

2929
interface ImportDetails {
3030
source: string;
@@ -171,12 +171,8 @@ export const resolveToTestingLibraryFn = <
171171
}
172172

173173
const customModuleSetting = context.settings['testing-library/utils-module'];
174-
const hasImportModuleMatch =
175-
[...LIBRARY_MODULES, USER_EVENT_MODULE].includes(maybeImport.source) ||
176-
(typeof customModuleSetting === 'string' &&
177-
maybeImport.source.endsWith(customModuleSetting));
178174

179-
if (hasImportModuleMatch) {
175+
if (isTestingLibraryModule(maybeImport.source, customModuleSetting)) {
180176
return {
181177
original: maybeImport.imported,
182178
local: maybeImport.local,

0 commit comments

Comments
 (0)