Skip to content

Commit 9624a44

Browse files
dyatkoBelco90
andauthored
fix(prefer-find-by): handle different spacing correctly for autofix (#472)
* fix(prefer-find-by): stop prefer-find-by fixer to break code when no spaces before bracket * fix(prefer-find-by): use regex to add findByMethod Co-authored-by: Marat Dyatko <[email protected]> Co-authored-by: Mario Beltrán Alarcón <[email protected]>
1 parent 08e40fc commit 9624a44

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Diff for: lib/rules/prefer-find-by.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
464464
const textDestructuring = sourceCode.getText(
465465
allVariableDeclarations
466466
);
467-
const text = `${textDestructuring.substring(
468-
0,
469-
textDestructuring.length - 2
470-
)}, ${findByMethod} }`;
467+
const text = textDestructuring.replace(
468+
/(\s*})$/,
469+
`, ${findByMethod}$1`
470+
);
471471
allFixes.push(fixer.replaceText(allVariableDeclarations, text));
472472
}
473473

Diff for: tests/lib/rules/prefer-find-by.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,35 @@ ruleTester.run(RULE_NAME, rule, {
457457
})
458458
`,
459459
})),
460+
...createScenario((waitMethod: string, queryMethod: string) => ({
461+
code: `
462+
import {${waitMethod}} from '@testing-library/foo';
463+
it('tests', async () => {
464+
const {${queryMethod}} = render()
465+
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull())
466+
})
467+
`,
468+
errors: [
469+
{
470+
messageId: 'preferFindBy',
471+
data: {
472+
queryVariant: getFindByQueryVariant(queryMethod),
473+
queryMethod: queryMethod.split('By')[1],
474+
prevQuery: queryMethod,
475+
waitForMethodName: waitMethod,
476+
},
477+
},
478+
],
479+
output: `
480+
import {${waitMethod}} from '@testing-library/foo';
481+
it('tests', async () => {
482+
const {${queryMethod}, ${buildFindByMethod(queryMethod)}} = render()
483+
const submitButton = await ${buildFindByMethod(
484+
queryMethod
485+
)}('foo', { name: 'baz' })
486+
})
487+
`,
488+
})),
460489
...createScenario((waitMethod: string, queryMethod: string) => ({
461490
code: `
462491
import {${waitMethod}} from '@testing-library/foo';

0 commit comments

Comments
 (0)