Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate prefer-find-by to v4 #270

Merged
merged 2 commits into from
Dec 13, 2020
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
11 changes: 4 additions & 7 deletions lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ESLintUtils,
TSESTree,
ASTUtils,
} from '@typescript-eslint/experimental-utils';
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
import {
ReportFixFunction,
RuleFix,
Expand All @@ -15,7 +11,8 @@ import {
isObjectPattern,
isProperty,
} from '../node-utils';
import { getDocsUrl, SYNC_QUERIES_COMBINATIONS } from '../utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { SYNC_QUERIES_COMBINATIONS } from '../utils';

export const RULE_NAME = 'prefer-find-by';
export type MessageIds = 'preferFindBy';
Expand Down Expand Up @@ -51,7 +48,7 @@ function findRenderDefinitionDeclaration(
return findRenderDefinitionDeclaration(scope.upper, query);
}

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
Expand Down
137 changes: 100 additions & 37 deletions tests/lib/rules/prefer-find-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,60 +38,110 @@ ruleTester.run(RULE_NAME, rule, {
valid: [
...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
const { ${queryMethod} } = setup()
const submitButton = await ${queryMethod}('foo')
it('tests', async () => {
const { ${queryMethod} } = setup()
const submitButton = await ${queryMethod}('foo')
})
`,
})),
...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `const submitButton = await screen.${queryMethod}('foo')`,
code: `
import {screen} from '@testing-library/foo';
it('tests', async () => {
const submitButton = await screen.${queryMethod}('foo')
})
`,
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `await waitForElementToBeRemoved(() => ${queryMethod}(baz))`,
code: `
import {waitForElementToBeRemoved} from '@testing-library/foo';
it('tests', async () => {
await waitForElementToBeRemoved(() => ${queryMethod}(baz))
})
`,
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `await waitFor(function() {
return ${queryMethod}('baz', { name: 'foo' })
})`,
code: `
import {waitFor} from '@testing-library/foo';

it('tests', async () => {
await waitFor(function() {
return ${queryMethod}('baz', { name: 'foo' })
})
})
`,
})),
{
code: `await waitFor(() => myCustomFunction())`,
code: `
import {waitFor} from '@testing-library/foo';

it('tests', async () => {
await waitFor(() => myCustomFunction())
})
`,
},
{
code: `await waitFor(customFunctionReference)`,
code: `
import {waitFor} from '@testing-library/foo';
it('tests', async () => {
await waitFor(customFunctionReference)
})
`,
},
{
code: `await waitForElementToBeRemoved(document.querySelector('foo'))`,
code: `
import {waitForElementToBeRemoved} from '@testing-library/foo';
it('tests', async () => {
const { container } = render()
await waitForElementToBeRemoved(container.querySelector('foo'))
})
`,
},
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => {
foo()
return ${queryMethod}()
import {waitFor} from '@testing-library/foo';
it('tests', async () => {
await waitFor(() => {
foo()
return ${queryMethod}()
})
})
`,
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => expect(screen.${queryMethod}('baz')).toBeDisabled());
import {screen, waitFor} from '@testing-library/foo';
it('tests', async () => {
await waitFor(() => expect(screen.${queryMethod}('baz')).toBeDisabled());
})
`,
})),
...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
code: `
await waitFor(() => expect(${queryMethod}('baz')).toBeInTheDocument());
import {waitFor} from '@testing-library/foo';
it('tests', async () => {
await waitFor(() => expect(${queryMethod}('baz')).toBeInTheDocument());
})
`,
})),
{
code: `
await waitFor();
await wait();
import {waitFor} from '@testing-library/foo';
it('tests', async () => {
await waitFor();
await wait();
})
`,
},
],
invalid: [
...createScenario((waitMethod: string, queryMethod: string) => ({
code: `
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))
import {${waitMethod}} from '@testing-library/foo';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))
})
`,
errors: [
{
Expand All @@ -104,14 +154,22 @@ ruleTester.run(RULE_NAME, rule, {
},
],
output: `
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
queryMethod
)}('foo', { name: 'baz' })
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: `const submitButton = await ${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))`,
code: `
import {${waitMethod}, screen} from '@testing-library/foo';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))
})
`,
errors: [
{
messageId: 'preferFindBy',
Expand All @@ -122,15 +180,21 @@ ruleTester.run(RULE_NAME, rule, {
},
},
],
output: `const submitButton = await screen.${buildFindByMethod(
queryMethod
)}('foo', { name: 'baz' })`,
output: `
import {${waitMethod}, screen} from '@testing-library/foo';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
queryMethod
)}('foo', { name: 'baz' })
})
`,
})),
// // this scenario verifies it works when the render function is defined in another scope
...WAIT_METHODS.map((waitMethod: string) => ({
code: `
import {${waitMethod}} from '@testing-library/foo';
const { getByText, queryByLabelText, findAllByRole } = customRender()
it('foo', async () => {
it('tests', async () => {
const submitButton = await ${waitMethod}(() => getByText('baz', { name: 'button' }))
})
`,
Expand All @@ -145,20 +209,20 @@ ruleTester.run(RULE_NAME, rule, {
},
],
output: `
import {${waitMethod}} from '@testing-library/foo';
const { getByText, queryByLabelText, findAllByRole, findByText } = customRender()
it('foo', async () => {
it('tests', async () => {
const submitButton = await findByText('baz', { name: 'button' })
})
`,
})),
// // this scenario verifies when findBy* were already defined (because it was used elsewhere)
...WAIT_METHODS.map((waitMethod: string) => ({
code: `
import {${waitMethod}} from '@testing-library/foo';
const { getAllByRole, findAllByRole } = customRender()
describe('some scenario', () => {
it('foo', async () => {
const submitButton = await ${waitMethod}(() => getAllByRole('baz', { name: 'button' }))
})
it('tests', async () => {
const submitButton = await ${waitMethod}(() => getAllByRole('baz', { name: 'button' }))
})
`,
errors: [
Expand All @@ -172,11 +236,10 @@ ruleTester.run(RULE_NAME, rule, {
},
],
output: `
import {${waitMethod}} from '@testing-library/foo';
const { getAllByRole, findAllByRole } = customRender()
describe('some scenario', () => {
it('foo', async () => {
const submitButton = await findAllByRole('baz', { name: 'button' })
})
it('tests', async () => {
const submitButton = await findAllByRole('baz', { name: 'button' })
})
`,
})),
Expand Down