Skip to content

Commit

Permalink
Make sure that await interaction works with @storybook/test
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Oct 11, 2024
1 parent 4a7c900 commit ce8985b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rules/await-interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export = createStorybookRule({

const isUserEventFromStorybookImported = (node: TSESTree.ImportDeclaration) => {
return (
node.source.value === '@storybook/testing-library' &&
(node.source.value === '@storybook/testing-library' ||
node.source.value === '@storybook/test') &&
node.specifiers.find(
(spec) =>
isImportSpecifier(spec) &&
Expand All @@ -144,7 +145,7 @@ export = createStorybookRule({

const isExpectFromStorybookImported = (node: TSESTree.ImportDeclaration) => {
return (
node.source.value === '@storybook/jest' &&
(node.source.value === '@storybook/jest' || node.source.value === '@storybook/test') &&
node.specifiers.find(
(spec) => isImportSpecifier(spec) && spec.imported.name === 'expect'
) !== undefined
Expand Down
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 @@ -146,6 +146,61 @@ ruleTester.run('await-interactions', rule, {
},
],
},
{
code: dedent`
import { expect, findByText } from '@storybook/test'
WithModalOpen.play = async ({ args }) => {
// should complain
expect(args.onClick).toHaveBeenCalled()
const element = findByText(canvasElement, 'asdf')
}
`,
output: dedent`
import { expect, findByText } from '@storybook/test'
WithModalOpen.play = async ({ args }) => {
// should complain
await expect(args.onClick).toHaveBeenCalled()
const element = await findByText(canvasElement, 'asdf')
}
`,
only: true,
errors: [
{
messageId: 'interactionShouldBeAwaited',
data: { method: 'toHaveBeenCalled' },
suggestions: [
{
messageId: 'fixSuggestion',
output: dedent`
import { expect, findByText } from '@storybook/test'
WithModalOpen.play = async ({ args }) => {
// should complain
await expect(args.onClick).toHaveBeenCalled()
const element = findByText(canvasElement, 'asdf')
}
`,
},
],
},
{
messageId: 'interactionShouldBeAwaited',
data: { method: 'findByText' },
suggestions: [
{
messageId: 'fixSuggestion',
output: dedent`
import { expect, findByText } from '@storybook/test'
WithModalOpen.play = async ({ args }) => {
// should complain
expect(args.onClick).toHaveBeenCalled()
const element = await findByText(canvasElement, 'asdf')
}
`,
},
],
},
],
},
{
code: dedent`
WithModalOpen.play = ({ canvasElement }) => {
Expand Down

0 comments on commit ce8985b

Please sign in to comment.