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

add support for CSF3 notation in use-storybook-expect #16

Merged
merged 4 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 23 additions & 11 deletions lib/rules/use-storybook-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import {
isCallExpression,
isMemberExpression,
isIdentifier,
isBlockStatement,
isProperty,
isVariableDeclaration,
isObjectExpression,
} from '../utils/ast'

import { createStorybookRule } from '../utils/create-storybook-rule'
import { isVariableDeclarator } from '@typescript-eslint/experimental-utils/dist/ast-utils'

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -67,6 +72,20 @@ export = createStorybookRule({
node.specifiers.find((spec: any) => spec.imported.name === 'expect')
)
}

const checkExpectInvocations = (blockStatement) => {
if (!isBlockStatement(blockStatement)) {
return
}

const expressionBody = blockStatement.body || []
const expressionStatements = getExpressionStatements(expressionBody)
expressionStatements.forEach(({ expression }) => {
if (isExpect(expression)) {
expectInvocations.push(expression)
}
})
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand All @@ -80,20 +99,13 @@ export = createStorybookRule({
isImportingFromStorybookExpect = true
}
},
AssignmentExpression(node: any) {
if (!isExpressionStatement(node.parent)) {
CallExpression(node) {
if (!isIdentifier(node.callee)) {
return null
}

if (isPlayFunction(node)) {
const { right } = node
const expressionBody = (right.body && right.body.body) || []
const expressionStatements = getExpressionStatements(expressionBody)
expressionStatements.forEach(({ expression }) => {
if (isExpect(expression)) {
expectInvocations.push(expression)
}
})
if (node.callee.name === 'expect') {
expectInvocations.push(node.callee)
}
},
'Program:exit': function () {
Expand Down
100 changes: 91 additions & 9 deletions tests/lib/rules/use-storybook-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,123 @@ import { AST_NODE_TYPES } from '@typescript-eslint/types'

ruleTester.run('use-storybook-expect', rule, {
valid: [
dedent(`
dedent`
import { expect } from '@storybook/jest';

Default.play = () => {
expect(123).toEqual(123);
}
`),
`,
dedent`
import { expect } from '@storybook/jest';

export const Basic = {
...Default,
play: async (context) => {
expect(123).toEqual(123);
},
};
`,
],

invalid: [
{
code: dedent(`
code: dedent`
Default.play = () => {
expect(123).toEqual(123);
}
`),
output: dedent(`
`,
output: dedent`
import { expect } from '@storybook/jest';
Default.play = () => {
expect(123).toEqual(123);
}
`),
`,
errors: [
{
messageId: 'useExpectFromStorybook',
type: AST_NODE_TYPES.CallExpression,
type: AST_NODE_TYPES.Identifier,
suggestions: [
{
messageId: 'updateImports',
output: dedent(`
output: dedent`
import { expect } from '@storybook/jest';
Default.play = () => {
expect(123).toEqual(123);
}
`),
`,
},
],
},
],
},
{
code: dedent`
const someInteraction = () => {
expect(123).toEqual(123);
}
Default.play = someInteraction
`,
output: dedent`
import { expect } from '@storybook/jest';
const someInteraction = () => {
expect(123).toEqual(123);
}
Default.play = someInteraction
`,
errors: [
{
messageId: 'useExpectFromStorybook',
type: AST_NODE_TYPES.Identifier,
suggestions: [
{
messageId: 'updateImports',
output: dedent`
import { expect } from '@storybook/jest';
const someInteraction = () => {
expect(123).toEqual(123);
}
Default.play = someInteraction
`,
},
],
},
],
},
{
code: dedent`
export const Basic = {
...Default,
play: async (context) => {
expect(123).toEqual(123);
},
};
`,
output: dedent`
import { expect } from '@storybook/jest';
export const Basic = {
...Default,
play: async (context) => {
expect(123).toEqual(123);
},
};
`,
errors: [
{
messageId: 'useExpectFromStorybook',
type: AST_NODE_TYPES.Identifier,
suggestions: [
{
messageId: 'updateImports',
output: dedent`
import { expect } from '@storybook/jest';
export const Basic = {
...Default,
play: async (context) => {
expect(123).toEqual(123);
},
};
`,
},
],
},
Expand Down