Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
const textDestructuring = sourceCode.getText(
allVariableDeclarations
);
const hasSpaceBeforeBracket = textDestructuring.endsWith(' }');
const symbolsToRemove = hasSpaceBeforeBracket ? 2 : 1;
const text = `${textDestructuring.substring(
0,
textDestructuring.length - 2
)}, ${findByMethod} }`;
textDestructuring.length - symbolsToRemove
)}, ${findByMethod}${hasSpaceBeforeBracket ? ' ' : ''}}`;
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