Skip to content

Commit

Permalink
feat(storybook): updated tests and snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jul 12, 2023
1 parent 7740373 commit ab30a1d
Show file tree
Hide file tree
Showing 12 changed files with 734 additions and 166 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,13 @@ describe('react:component-story', () => {
});
});

describe('using eslint', () => {
describe('using eslint - not using interaction tests', () => {
beforeEach(async () => {
appTree = await createTestUILib('test-ui-lib');
await componentStoryGenerator(appTree, {
componentPath: 'lib/test-ui-lib.tsx',
project: 'test-ui-lib',
interactionTests: false,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ export function findPropsAndGenerateFile(

generateFiles(
host,
joinPathFragments(__dirname, `${'./files' + isPlainJs ? '/jsx' : '/tsx'}`),
joinPathFragments(__dirname, `./files${isPlainJs ? '/jsx' : '/tsx'}`),
normalizePath(componentDirectory),
{
tmpl: '',
componentFileName: fromNodeArray
? `${name}--${(cmpDeclaration as any).name.text}`
: name,
Expand All @@ -136,7 +137,10 @@ export async function componentStoryGenerator(
host: Tree,
schema: CreateComponentStoriesFileSchema
) {
createComponentStoriesFile(host, schema);
createComponentStoriesFile(host, {
...schema,
interactionTests: schema.interactionTests ?? true,
});

if (!schema.skipFormat) {
await formatFiles(host);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`react:stories for applications should create the stories with interaction tests 1`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { NxWelcome } from './nx-welcome';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const meta: Meta<typeof NxWelcome> = {
component: NxWelcome,
title: 'NxWelcome',
};
export default meta;
type Story = StoryObj<typeof NxWelcome>;
export const Primary = {
args: {},
};
export const Heading: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText(/Welcome to NxWelcome!/gi)).toBeTruthy();
},
};
"
`;
exports[`react:stories for applications should create the stories with interaction tests 2`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { Test } from './another-cmp';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const meta: Meta<typeof Test> = {
component: Test,
title: 'Test',
};
export default meta;
type Story = StoryObj<typeof Test>;
export const Primary = {
args: {
name: '',
displayAge: false,
},
};
export const Heading: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText(/Welcome to Test!/gi)).toBeTruthy();
},
};
"
`;
exports[`react:stories for applications should create the stories without interaction tests 1`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { NxWelcome } from './nx-welcome';
const meta: Meta<typeof NxWelcome> = {
component: NxWelcome,
title: 'NxWelcome',
};
export default meta;
type Story = StoryObj<typeof NxWelcome>;
export const Primary = {
args: {},
};
"
`;
exports[`react:stories for applications should create the stories without interaction tests 2`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { Test } from './another-cmp';
const meta: Meta<typeof Test> = {
component: Test,
title: 'Test',
};
export default meta;
type Story = StoryObj<typeof Test>;
export const Primary = {
args: {
name: '',
displayAge: false,
},
};
"
`;
exports[`react:stories for applications should not update existing stories 1`] = `
"import { ComponentStory, ComponentMeta } from '@storybook/react';
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`react:stories for libraries should create the stories with interaction tests 1`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { TestUiLib } from './test-ui-lib';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const meta: Meta<typeof TestUiLib> = {
component: TestUiLib,
title: 'TestUiLib',
};
export default meta;
type Story = StoryObj<typeof TestUiLib>;
export const Primary = {
args: {},
};
export const Heading: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText(/Welcome to TestUiLib!/gi)).toBeTruthy();
},
};
"
`;
exports[`react:stories for libraries should create the stories with interaction tests 2`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { Test } from './another-cmp';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
const meta: Meta<typeof Test> = {
component: Test,
title: 'Test',
};
export default meta;
type Story = StoryObj<typeof Test>;
export const Primary = {
args: {
name: '',
displayAge: false,
},
};
export const Heading: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
expect(canvas.getByText(/Welcome to Test!/gi)).toBeTruthy();
},
};
"
`;
exports[`react:stories for libraries should create the stories without interaction tests 1`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { TestUiLib } from './test-ui-lib';
const meta: Meta<typeof TestUiLib> = {
component: TestUiLib,
title: 'TestUiLib',
};
export default meta;
type Story = StoryObj<typeof TestUiLib>;
export const Primary = {
args: {},
};
"
`;
exports[`react:stories for libraries should create the stories without interaction tests 2`] = `
"import type { Meta, StoryObj } from '@storybook/react';
import { Test } from './another-cmp';
const meta: Meta<typeof Test> = {
component: Test,
title: 'Test',
};
export default meta;
type Story = StoryObj<typeof Test>;
export const Primary = {
args: {
name: '',
displayAge: false,
},
};
"
`;
34 changes: 16 additions & 18 deletions packages/react/src/generators/stories/stories.app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,37 @@ describe('react:stories for applications', () => {
);
});

it('should create the stories', async () => {
it('should create the stories with interaction tests', async () => {
await storiesGenerator(appTree, {
project: 'test-ui-app',
});

expect(
appTree.exists('apps/test-ui-app/src/app/nx-welcome.stories.tsx')
).toBeTruthy();
appTree.read('apps/test-ui-app/src/app/nx-welcome.stories.tsx', 'utf-8')
).toMatchSnapshot();
expect(
appTree.exists(
'apps/test-ui-app/src/app/anothercmp/another-cmp.stories.tsx'
appTree.read(
'apps/test-ui-app/src/app/anothercmp/another-cmp.stories.tsx',
'utf-8'
)
).toBeTruthy();
).toMatchSnapshot();
});

it('should generate Cypress specs', async () => {
it('should create the stories without interaction tests', async () => {
await storiesGenerator(appTree, {
project: 'test-ui-app',
interactionTests: false,
});

expect(
appTree.exists('apps/test-ui-app-e2e/src/e2e/app.cy.ts')
).toBeTruthy();
appTree.read('apps/test-ui-app/src/app/nx-welcome.stories.tsx', 'utf-8')
).toMatchSnapshot();
expect(
appTree.exists(
'apps/test-ui-app-e2e/src/e2e/another-cmp/another-cmp.cy.ts'
appTree.read(
'apps/test-ui-app/src/app/anothercmp/another-cmp.stories.tsx',
'utf-8'
)
).toBeTruthy();
).toMatchSnapshot();
});

it('should ignore files that do not contain components', async () => {
Expand All @@ -90,23 +93,18 @@ describe('react:stories for applications', () => {
});

it('should not update existing stories', async () => {
// ARRANGE
appTree.write(
'apps/test-ui-app/src/app/nx-welcome.stories.tsx',
`import { ComponentStory, ComponentMeta } from '@storybook/react'`
);

// ACT
await storiesGenerator(appTree, {
project: 'test-ui-app',
});

// ASSERT
expect(
appTree.read('apps/test-ui-app/src/app/nx-welcome.stories.tsx', 'utf-8')
).toEqual(
`import { ComponentStory, ComponentMeta } from '@storybook/react';\n`
);
).toMatchSnapshot();
});

describe('ignore paths', () => {
Expand Down
32 changes: 15 additions & 17 deletions packages/react/src/generators/stories/stories.lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,38 @@ describe('react:stories for libraries', () => {
);
});

it('should create the stories', async () => {
it('should create the stories with interaction tests', async () => {
await storiesGenerator(appTree, {
project: 'test-ui-lib',
});

expect(
appTree.exists('libs/test-ui-lib/src/lib/test-ui-lib.stories.tsx')
).toBeTruthy();
appTree.read('libs/test-ui-lib/src/lib/test-ui-lib.stories.tsx', 'utf-8')
).toMatchSnapshot();
expect(
appTree.exists(
'libs/test-ui-lib/src/lib/anothercmp/another-cmp.stories.tsx'
appTree.read(
'libs/test-ui-lib/src/lib/anothercmp/another-cmp.stories.tsx',
'utf-8'
)
).toBeTruthy();
).toMatchSnapshot();
});

it('should generate Cypress specs', async () => {
it('should create the stories without interaction tests', async () => {
await storiesGenerator(appTree, {
project: 'test-ui-lib',
interactionTests: false,
});

expect(
appTree.exists(
'apps/test-ui-lib-e2e/src/integration/test-ui-lib/test-ui-lib.spec.ts'
)
).toBeTruthy();
appTree.read('libs/test-ui-lib/src/lib/test-ui-lib.stories.tsx', 'utf-8')
).toMatchSnapshot();
expect(
appTree.exists(
'apps/test-ui-lib-e2e/src/integration/another-cmp/another-cmp.spec.ts'
appTree.read(
'libs/test-ui-lib/src/lib/anothercmp/another-cmp.stories.tsx',
'utf-8'
)
).toBeTruthy();
).toMatchSnapshot();
});

it('should not overwrite existing stories', () => {});

describe('ignore paths', () => {
beforeEach(() => {
appTree.write(
Expand Down
13 changes: 12 additions & 1 deletion packages/react/src/generators/stories/stories.nextjs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('nextjs:stories for applications', () => {
);
});

it('should create the stories', async () => {
it('should create the stories with interaction tests', async () => {
await storiesGenerator(tree, {
project: 'test-ui-app',
});
Expand All @@ -42,6 +42,17 @@ describe('nextjs:stories for applications', () => {
).toBeTruthy();
});

it('should create the stories without interaction tests', async () => {
await storiesGenerator(tree, {
project: 'test-ui-app',
interactionTests: false,
});

expect(
tree.exists('apps/test-ui-app/components/test.stories.tsx')
).toBeTruthy();
});

it('should ignore paths', async () => {
await storiesGenerator(tree, {
project: 'test-ui-app',
Expand Down
Loading

0 comments on commit ab30a1d

Please sign in to comment.