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

Fix await interactions use cases #51

Merged
merged 6 commits into from
Nov 19, 2021
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
26 changes: 25 additions & 1 deletion lib/rules/await-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import type { CallExpression, Identifier, Node } from '@typescript-eslint/types/

import { createStorybookRule } from '../utils/create-storybook-rule'
import { CategoryId } from '../utils/constants'
import { isMemberExpression, isIdentifier, isAwaitExpression, isCallExpression } from '../utils/ast'
import {
isMemberExpression,
isIdentifier,
isAwaitExpression,
isCallExpression,
isArrowFunctionExpression,
isReturnStatement,
isTSNonNullExpression,
} from '../utils/ast'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -47,12 +55,19 @@ export = createStorybookRule({
'waitForElement',
'waitForDomChange',
'userEvent',
'play',
]

const getMethodThatShouldBeAwaited = (expr: CallExpression) => {
const shouldAwait = (name: any) => {
return FUNCTIONS_TO_BE_AWAITED.includes(name) || name.startsWith('findBy')
}

// When an expression is a return value it doesn't need to be awaited
if (isArrowFunctionExpression(expr.parent) || isReturnStatement(expr.parent)) {
return null
}

if (
isMemberExpression(expr.callee) &&
isIdentifier(expr.callee.object) &&
Expand All @@ -61,6 +76,15 @@ export = createStorybookRule({
return expr.callee.object
}

if (
isTSNonNullExpression(expr.callee) &&
isMemberExpression(expr.callee.expression) &&
isIdentifier(expr.callee.expression.property) &&
shouldAwait(expr.callee.expression.property.name)
) {
return expr.callee.expression.property
}

if (
isMemberExpression(expr.callee) &&
isIdentifier(expr.callee.property) &&
Expand Down
1 change: 1 addition & 0 deletions lib/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export const isProgram = isNodeOfType(AST_NODE_TYPES.Program)
export const isTSTypeAliasDeclaration = isNodeOfType(AST_NODE_TYPES.TSTypeAliasDeclaration)
export const isTSInterfaceDeclaration = isNodeOfType(AST_NODE_TYPES.TSInterfaceDeclaration)
export const isTSAsExpression = isNodeOfType(AST_NODE_TYPES.TSAsExpression)
export const isTSNonNullExpression = isNodeOfType(AST_NODE_TYPES.TSNonNullExpression)
55 changes: 55 additions & 0 deletions tests/lib/rules/await-interactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ ruleTester.run('await-interactions', rule, {
}
`,
'await expect(foo).toBe(bar)',
dedent`
Basic.play = async () => {
await waitForElementToBeRemoved(() => canvas.findByText('Loading...'))
await waitForElementToBeRemoved(() => userEvent.hover(canvas.getByTestId('password-error-info')))
}
`,
// // @TODO: https://github.com/storybookjs/eslint-plugin-storybook/issues/47
// dedent`
// import { userEvent } from '../utils'
// Basic.play = async () => {
// userEvent.click(canvas.getByRole('button'))
// }
// `,
// // @TODO: https://github.com/storybookjs/eslint-plugin-storybook/issues/28
// dedent`
// Block.parameters = {
// async puppeteerTest(page) {
// const element = await page.$('[data-test-block]');
// await element.hover();
// const textContent = await element.getProperty('textContent');
// const text = await textContent.jsonValue();
// expect(text).toBe('I am hovered');
// },
// };
// `,
'Basic.play = async () => userEvent.click(button)',
'Basic.play = async () => { return userEvent.click(button) }',
],
invalid: [
{
Expand Down Expand Up @@ -236,5 +263,33 @@ ruleTester.run('await-interactions', rule, {
},
],
},
{
code: dedent`
export const ThirdStory = {
play: async (context) => {
FirstStory.play(context)
SecondStory.play!(context)
}
}
`,
output: dedent`
export const ThirdStory = {
play: async (context) => {
await FirstStory.play(context)
await SecondStory.play!(context)
}
}
`,
errors: [
{
messageId: 'interactionShouldBeAwaited',
data: { method: 'play' },
},
{
messageId: 'interactionShouldBeAwaited',
data: { method: 'play' },
},
],
},
],
})