Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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