Skip to content

Commit

Permalink
fix(prefer-find-by): handle different spacing correctly for autofix (#…
Browse files Browse the repository at this point in the history
…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 <maratd@spotify.com>
Co-authored-by: Mario Beltrán Alarcón <belco90@gmail.com>
  • Loading branch information
3 people committed Sep 9, 2021
1 parent 08e40fc commit 9624a44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
const textDestructuring = sourceCode.getText(
allVariableDeclarations
);
const text = `${textDestructuring.substring(
0,
textDestructuring.length - 2
)}, ${findByMethod} }`;
const text = textDestructuring.replace(
/(\s*})$/,
`, ${findByMethod}$1`
);
allFixes.push(fixer.replaceText(allVariableDeclarations, text));
}

Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/prefer-find-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,35 @@ ruleTester.run(RULE_NAME, rule, {
})
`,
})),
...createScenario((waitMethod: string, queryMethod: string) => ({
code: `
import {${waitMethod}} from '@testing-library/foo';
it('tests', async () => {
const {${queryMethod}} = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull())
})
`,
errors: [
{
messageId: 'preferFindBy',
data: {
queryVariant: getFindByQueryVariant(queryMethod),
queryMethod: queryMethod.split('By')[1],
prevQuery: queryMethod,
waitForMethodName: waitMethod,
},
},
],
output: `
import {${waitMethod}} from '@testing-library/foo';
it('tests', async () => {
const {${queryMethod}, ${buildFindByMethod(queryMethod)}} = render()
const submitButton = await ${buildFindByMethod(
queryMethod
)}('foo', { name: 'baz' })
})
`,
})),
...createScenario((waitMethod: string, queryMethod: string) => ({
code: `
import {${waitMethod}} from '@testing-library/foo';
Expand Down

0 comments on commit 9624a44

Please sign in to comment.