Skip to content

Commit

Permalink
Merge branch 'next' into tech/refactor-simplified-addons-api
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Apr 1, 2022
2 parents 2832361 + ab1edc8 commit 666f25d
Show file tree
Hide file tree
Showing 44 changed files with 2,142 additions and 175 deletions.
17 changes: 0 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,6 @@ jobs:
name: examples
command: |
yarn run-chromatics
packtracker:
executor:
class: medium
name: sb_node_14_browsers
steps:
- git-shallow-clone/checkout_advanced:
clone_options: '--depth 1 --verbose'
- attach_workspace:
at: .
- run:
name: Report webpack stats for manager of official storybook
command: |
cd examples/official-storybook
yarn packtracker
examples:
executor:
class: medium+
Expand Down Expand Up @@ -439,9 +425,6 @@ workflows:
- smoke-tests:
requires:
- build
- packtracker:
requires:
- build
- unit-tests:
requires:
- build
Expand Down
17 changes: 3 additions & 14 deletions addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@storybook/source-loader": "6.5.0-alpha.52",
"@storybook/store": "6.5.0-alpha.52",
"@storybook/theming": "6.5.0-alpha.52",
"babel-loader": "^8.0.0",
"core-js": "^3.8.2",
"fast-deep-equal": "^3.1.3",
"global": "^4.4.0",
Expand All @@ -86,23 +87,14 @@
"devDependencies": {
"@babel/core": "^7.12.10",
"@storybook/mdx2-csf": "canary",
"@types/util-deprecate": "^1.0.0",
"babel-loader": "^8.0.0",
"webpack": "4"
"@types/util-deprecate": "^1.0.0"
},
"peerDependencies": {
"@storybook/mdx2-csf": "*",
"react": "^16.8.0 || ^17.0.0",
"react-dom": "^16.8.0 || ^17.0.0",
"webpack": "*"
"react-dom": "^16.8.0 || ^17.0.0"
},
"peerDependenciesMeta": {
"@storybook/builder-webpack4": {
"optional": true
},
"@storybook/builder-webpack5": {
"optional": true
},
"@storybook/mdx2-csf": {
"optional": true
},
Expand All @@ -111,9 +103,6 @@
},
"react-dom": {
"optional": true
},
"webpack": {
"optional": true
}
},
"publishConfig": {
Expand Down
13 changes: 2 additions & 11 deletions addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import remarkSlug from 'remark-slug';
import remarkExternalLinks from 'remark-external-links';
import global from 'global';

import type { BuilderConfig, Options } from '@storybook/core-common';
import type { Options } from '@storybook/core-common';
import { logger } from '@storybook/node-logger';

// for frameworks that are not working with react, we need to configure
Expand Down Expand Up @@ -38,16 +38,7 @@ export async function webpack(
typeof createCompiler
>[0] */
) {
const { builder = 'webpack4' } = await options.presets.apply<{
builder: BuilderConfig;
}>('core', {} as any);

const builderName = typeof builder === 'string' ? builder : builder.name;
const resolvedBabelLoader = require.resolve('babel-loader', {
paths: builderName.match(/^webpack(4|5)$/)
? [require.resolve(`@storybook/builder-${builderName}`)]
: [builderName],
});
const resolvedBabelLoader = require.resolve('babel-loader');

const { module = {} } = webpackConfig;

Expand Down
1 change: 1 addition & 0 deletions app/react/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
raw,
forceReRender,
} from './preview';
export * from './testing';

export * from './preview/types-6-3';

Expand Down
129 changes: 129 additions & 0 deletions app/react/src/client/testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
composeStory as originalComposeStory,
composeStories as originalComposeStories,
setProjectAnnotations as originalSetProjectAnnotations,
CSFExports,
ComposedStory,
StoriesWithPartialProps,
} from '@storybook/store';
import { ProjectAnnotations, Args } from '@storybook/csf';
import { once } from '@storybook/client-logger';

import { render } from '../preview/render';
import type { Meta, ReactFramework } from '../preview/types-6-0';

/** Function that sets the globalConfig of your storybook. The global config is the preview module of your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your stories when using `composeStories` or `composeStory`.
*
* Example:
*```jsx
* // setup.js (for jest)
* import { setProjectAnnotations } from '@storybook/react';
* import * as projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
*```
*
* @param projectAnnotations - e.g. (import * as projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations: ProjectAnnotations<ReactFramework> | ProjectAnnotations<ReactFramework>[]
) {
originalSetProjectAnnotations(projectAnnotations);
}

/** Preserved for users migrating from `@storybook/testing-react`.
*
* @deprecated Use setProjectAnnotations instead
*/
export function setGlobalConfig(
projectAnnotations: ProjectAnnotations<ReactFramework> | ProjectAnnotations<ReactFramework>[]
) {
once.warn(`setGlobalConfig is deprecated. Use setProjectAnnotations instead.`);
setProjectAnnotations(projectAnnotations);
}

// This will not be necessary once we have auto preset loading
const defaultProjectAnnotations: ProjectAnnotations<ReactFramework> = {
render,
};

/**
* Function that will receive a story along with meta (e.g. a default export from a .stories file)
* and optionally projectAnnotations e.g. (import * from '../.storybook/preview)
* and will return a composed component that has all args/parameters/decorators/etc combined and applied to it.
*
*
* It's very useful for reusing a story in scenarios outside of Storybook like unit testing.
*
* Example:
*```jsx
* import { render } from '@testing-library/react';
* import { composeStory } from '@storybook/react';
* import Meta, { Primary as PrimaryStory } from './Button.stories';
*
* const Primary = composeStory(PrimaryStory, Meta);
*
* test('renders primary button with Hello World', () => {
* const { getByText } = render(<Primary>Hello world</Primary>);
* expect(getByText(/Hello world/i)).not.toBeNull();
* });
*```
*
* @param story
* @param componentAnnotations - e.g. (import Meta from './Button.stories')
* @param [projectAnnotations] - e.g. (import * as projectAnnotations from '../.storybook/preview') this can be applied automatically if you use `setProjectAnnotations` in your setup files.
* @param [exportsName] - in case your story does not contain a name and you want it to have a name.
*/
export function composeStory<TArgs = Args>(
story: ComposedStory<ReactFramework, TArgs>,
componentAnnotations: Meta<TArgs | any>,
projectAnnotations?: ProjectAnnotations<ReactFramework>,
exportsName?: string
) {
return originalComposeStory<ReactFramework, TArgs>(
story,
componentAnnotations,
projectAnnotations,
defaultProjectAnnotations,
exportsName
);
}

/**
* Function that will receive a stories import (e.g. `import * as stories from './Button.stories'`)
* and optionally projectAnnotations (e.g. `import * from '../.storybook/preview`)
* and will return an object containing all the stories passed, but now as a composed component that has all args/parameters/decorators/etc combined and applied to it.
*
*
* It's very useful for reusing stories in scenarios outside of Storybook like unit testing.
*
* Example:
*```jsx
* import { render } from '@testing-library/react';
* import { composeStories } from '@storybook/react';
* import * as stories from './Button.stories';
*
* const { Primary, Secondary } = composeStories(stories);
*
* test('renders primary button with Hello World', () => {
* const { getByText } = render(<Primary>Hello world</Primary>);
* expect(getByText(/Hello world/i)).not.toBeNull();
* });
*```
*
* @param csfExports - e.g. (import * as stories from './Button.stories')
* @param [projectAnnotations] - e.g. (import * as projectAnnotations from '../.storybook/preview') this can be applied automatically if you use `setProjectAnnotations` in your setup files.
*/
export function composeStories<TModule extends CSFExports<ReactFramework>>(
csfExports: TModule,
projectAnnotations?: ProjectAnnotations<ReactFramework>
) {
const composedStories = originalComposeStories(csfExports, projectAnnotations, composeStory);

return composedStories as unknown as Omit<
StoriesWithPartialProps<ReactFramework, TModule>,
keyof CSFExports
>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { StorybookConfig } from '@storybook/react/types';

const path = require('path');

module.exports = {
const mainConfig: StorybookConfig = {
stories: ['../src/**/*.stories.@(tsx|mdx)'],
addons: [
'@storybook/preset-create-react-app',
Expand All @@ -13,9 +15,9 @@ module.exports = {
},
],
logLevel: 'debug',
webpackFinal: (config) => {
webpackFinal: async (config) => {
// add monorepo root as a valid directory to import modules from
config.resolve.plugins.forEach((p) => {
config.resolve?.plugins?.forEach((p: any) => {
if (Array.isArray(p.appSrcs)) {
p.appSrcs.push(path.join(__dirname, '..', '..', '..'));
}
Expand All @@ -30,3 +32,5 @@ module.exports = {
buildStoriesJson: true,
},
};

module.exports = mainConfig;
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import React from 'react';
import type { DecoratorFn } from '@storybook/react';
import { ThemeProvider, convert, themes } from '@storybook/theming';

export const decorators = [
(StoryFn, { globals: { locale = 'en' } }) => (
export const decorators: DecoratorFn[] = [
(StoryFn, { globals: { locale } }) => (
<>
<div>{locale}</div>
<div>Locale: {locale}</div>
<StoryFn />
</>
),
(StoryFn) => (
<ThemeProvider theme={convert(themes.light)}>
<StoryFn />
</ThemeProvider>
),
];

export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
};

export const globalTypes = {
locale: {
name: 'Locale',
Expand Down
8 changes: 6 additions & 2 deletions examples/cra-ts-essentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"eject": "react-scripts eject",
"start": "react-scripts start",
"storybook": "start-storybook -p 9009 --no-manager-cache",
"test": "react-scripts test"
"test": "SKIP_PREFLIGHT_CHECK=true react-scripts test"
},
"browserslist": {
"production": [
Expand All @@ -23,10 +23,13 @@
]
},
"dependencies": {
"@storybook/components": "6.5.0-alpha.51",
"@storybook/theming": "6.5.0-alpha.51",
"@types/jest": "^26.0.16",
"@types/node": "^14.14.20 || ^16.0.0",
"@types/react": "^16.14.23",
"@types/react-dom": "^16.9.14",
"@types/react-dom": "16.9.14",
"formik": "2.2.9",
"global": "^4.4.0",
"react": "16.14.0",
"react-dom": "16.14.0",
Expand All @@ -40,6 +43,7 @@
"@storybook/builder-webpack4": "6.5.0-alpha.52",
"@storybook/preset-create-react-app": "^3.1.6",
"@storybook/react": "6.5.0-alpha.52",
"@storybook/testing-library": "^0.0.9",
"webpack": "4"
},
"storybook": {
Expand Down
4 changes: 4 additions & 0 deletions examples/cra-ts-essentials/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setProjectAnnotations } from '@storybook/react';
import * as projectAnnotations from '../.storybook/preview';

setProjectAnnotations(projectAnnotations);
7 changes: 5 additions & 2 deletions examples/cra-ts-essentials/src/stories/0-Welcome.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';
import { linkTo } from '@storybook/addon-links';
import { Welcome } from '@storybook/react/demo';
import type { ComponentMeta, ComponentStory } from '@storybook/react';

export default {
title: 'Welcome',
component: Welcome,
};
} as ComponentMeta<typeof Welcome>;

export const ToStorybook = () => <Welcome showApp={linkTo('Button')} />;
export const ToStorybook: ComponentStory<typeof Welcome> = () => (
<Welcome showApp={linkTo('Button')} />
);

ToStorybook.storyName = 'to Storybook';
5 changes: 3 additions & 2 deletions examples/cra-ts-essentials/src/stories/1-Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import { Button } from '@storybook/react/demo';
import type { ComponentStory, ComponentMeta } from '@storybook/react';

export default {
title: 'Button',
component: Button,
argTypes: { onClick: { action: 'clicked' } },
};
} as ComponentMeta<typeof Button>;

const Template = (args: any) => <Button {...args} />;
const Template: ComponentStory<typeof Button> = (args) => <Button {...args} />;

export const Text = Template.bind({});
Text.args = {
Expand Down

0 comments on commit 666f25d

Please sign in to comment.