Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 42a08e8

Browse files
committed
deprecate setGlobalConfig in favor of setProjectAnnotations
1 parent 82c6acb commit 42a08e8

File tree

6 files changed

+35
-29
lines changed

6 files changed

+35
-29
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ If you have global decorators/parameters/etc and want them applied to your stori
5959

6060
```tsx
6161
// setupFile.js <-- this will run before the tests in jest.
62-
import { setGlobalConfig } from '@storybook/testing-react';
62+
import { setProjectAnnotations } from '@storybook/testing-react';
6363
import * as globalStorybookConfig from './.storybook/preview'; // path of your preview.js file
6464

65-
setGlobalConfig(globalStorybookConfig);
65+
setProjectAnnotations(globalStorybookConfig);
6666
```
6767

6868
For the setup file to be picked up, you need to pass it as an option to jest in your test command:

example/.storybook/preview.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
import type { DecoratorFn } from '@storybook/react';
2+
import type { Decorator } from '@storybook/react';
33
import { ThemeProvider, convert, themes } from '@storybook/theming';
44

55
export const parameters = {
66
actions: { argTypesRegex: '^on[A-Z].*' }
77
}
88

9-
export const decorators: DecoratorFn[] = [
9+
export const decorators: Decorator[] = [
1010
(StoryFn, { globals: { locale } }) => (
1111
<>
1212
<div>Locale: {locale}</div>

example/setup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setGlobalConfig } from '../dist/index';
1+
import { setProjectAnnotations } from '../dist/index';
22
import * as globalStorybookConfig from './.storybook/preview';
33

4-
setGlobalConfig(globalStorybookConfig);
4+
setProjectAnnotations(globalStorybookConfig);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@storybook/preview-web": "next",
8383
"@storybook/preview-api": "next",
8484
"@storybook/react": "next",
85+
"@storybook/client-logger": "next",
8586
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
8687
},
8788
"publishConfig": {

src/index.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { defaultDecorateStory, combineParameters, addons, applyHooks, HooksContext, mockChannel } from '@storybook/preview-api';
1+
import { defaultDecorateStory, combineParameters, addons, applyHooks, HooksContext, mockChannel, composeConfigs } from '@storybook/preview-api';
22
import type { ReactRenderer, Args } from '@storybook/react';
3-
import type { ComponentAnnotations, Store_CSFExports, StoryContext } from '@storybook/types';
3+
import type { ComponentAnnotations, ProjectAnnotations, Store_CSFExports, StoryContext } from '@storybook/types';
44
import { isExportStory } from '@storybook/csf';
5+
import { deprecate } from '@storybook/client-logger';
56

6-
import type { GlobalConfig, StoriesWithPartialProps, TestingStory, TestingStoryPlayContext } from './types';
7+
import type { StoriesWithPartialProps, TestingStory, TestingStoryPlayContext } from './types';
78
import { getStoryName, globalRender, isInvalidStory, objectEntries } from './utils';
89

910
// Some addons use the channel api to communicate between manager/preview, and this is a client only feature, therefore we must mock it.
1011
addons.setChannel(mockChannel());
1112

12-
let globalStorybookConfig = {};
13+
let GLOBAL_STORYBOOK_PROJECT_ANNOTATIONS = {};
1314

1415
const decorateStory = applyHooks(defaultDecorateStory);
1516

@@ -23,16 +24,29 @@ const isValidStoryExport = (storyName: string, nonStoryExportsConfig = {}) =>
2324
* Example:
2425
*```jsx
2526
* // setup.js (for jest)
26-
* import { setGlobalConfig } from '@storybook/testing-react';
27-
* import * as globalStorybookConfig from './.storybook/preview';
27+
* import { setProjectAnnotations } from '@storybook/testing-react';
28+
* import * as projectAnnotations from './.storybook/preview';
2829
*
29-
* setGlobalConfig(globalStorybookConfig);
30+
* setProjectAnnotations(projectAnnotations);
3031
*```
3132
*
32-
* @param config - e.g. (import * as globalConfig from '../.storybook/preview')
33+
* @param projectAnnotations - e.g. (import * as projectAnnotations from '../.storybook/preview')
3334
*/
34-
export function setGlobalConfig(config: GlobalConfig) {
35-
globalStorybookConfig = config;
35+
export function setProjectAnnotations(
36+
projectAnnotations: ProjectAnnotations<ReactRenderer> | ProjectAnnotations<ReactRenderer>[]
37+
) {
38+
const annotations = Array.isArray(projectAnnotations) ? projectAnnotations : [projectAnnotations];
39+
GLOBAL_STORYBOOK_PROJECT_ANNOTATIONS = composeConfigs(annotations);
40+
}
41+
42+
/**
43+
* @deprecated Use setProjectAnnotations instead
44+
*/
45+
export function setGlobalConfig(
46+
projectAnnotations: ProjectAnnotations<ReactRenderer> | ProjectAnnotations<ReactRenderer>[]
47+
) {
48+
deprecate(`[@storybook/testing-react] setGlobalConfig is deprecated. Use setProjectAnnotations instead.`);
49+
setProjectAnnotations(projectAnnotations);
3650
}
3751

3852
/**
@@ -64,7 +78,7 @@ export function setGlobalConfig(config: GlobalConfig) {
6478
export function composeStory<GenericArgs extends Args>(
6579
story: TestingStory<GenericArgs>,
6680
meta: ComponentAnnotations<ReactRenderer>,
67-
globalConfig: GlobalConfig = globalStorybookConfig
81+
globalConfig: ProjectAnnotations<ReactRenderer> = GLOBAL_STORYBOOK_PROJECT_ANNOTATIONS
6882
) {
6983

7084
if (isInvalidStory(story)) {
@@ -190,7 +204,7 @@ export function composeStory<GenericArgs extends Args>(
190204
* @param storiesImport - e.g. (import * as stories from './Button.stories')
191205
* @param [globalConfig] - e.g. (import * as globalConfig from '../.storybook/preview') this can be applied automatically if you use `setGlobalConfig` in your setup files.
192206
*/
193-
export function composeStories<TModule extends Store_CSFExports<ReactRenderer>>(storiesImport: TModule, globalConfig?: GlobalConfig) {
207+
export function composeStories<TModule extends Store_CSFExports<ReactRenderer>>(storiesImport: TModule, globalConfig?: ProjectAnnotations<ReactRenderer>) {
194208
const { default: meta, __esModule, __namedExportsOrder, ...stories } = storiesImport;
195209

196210
// This function should take this as input:

src/types.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@ import type {
22
AnnotatedStoryFn,
33
Args,
44
PlayFunction, PlayFunctionContext,
5-
ProjectAnnotations,
65
StoryAnnotations,
76
} from '@storybook/types';
87
import type { ReactRenderer } from '@storybook/react';
98

10-
/**
11-
* Object representing the preview.ts module
12-
*
13-
* Used in storybook testing utilities.
14-
* @see [Unit testing with Storybook](https://storybook.js.org/docs/react/workflows/unit-testing)
15-
*/
16-
export type GlobalConfig = ProjectAnnotations<ReactRenderer>;
17-
18-
export type TestingStory<T = Args> = StoryAnnotations<ReactRenderer, T>;
9+
export type TestingStory<TArgs = Args> = StoryAnnotations<ReactRenderer, TArgs>;
1910

20-
export type TestingStoryPlayContext<T = Args> = Partial<PlayFunctionContext<ReactRenderer, T>> & Pick<PlayFunctionContext, 'canvasElement'>
11+
export type TestingStoryPlayContext<TArgs = Args> = Partial<PlayFunctionContext<ReactRenderer, TArgs>> & Pick<PlayFunctionContext, 'canvasElement'>
2112

2213
export type StoryFn<TArgs = Args> = AnnotatedStoryFn<ReactRenderer, TArgs> & { play: PlayFunction<ReactRenderer, TArgs> }
2314

0 commit comments

Comments
 (0)