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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF: Make sure loaders/decorators can be used as array #26514

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions code/lib/preview-api/src/modules/store/csf/composeConfigs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ describe('composeConfigs', () => {
});
});

it('allows single array to be written without array', () => {
expect(
composeConfigs([
{
argsEnhancers: ['1', '2'],
argTypesEnhancers: ['1', '2'],
loaders: '1',
},
{
argsEnhancers: '3',
argTypesEnhancers: '3',
loaders: ['2', '3'],
},
])
).toEqual({
parameters: {},
decorators: [],
args: {},
argsEnhancers: ['1', '2', '3'],
argTypes: {},
argTypesEnhancers: ['1', '2', '3'],
globals: {},
globalTypes: {},
loaders: ['1', '2', '3'],
runStep: expect.any(Function),
});
});

it('combines decorators in reverse file order', () => {
expect(
composeConfigs([
Expand Down
9 changes: 5 additions & 4 deletions code/lib/preview-api/src/modules/store/csf/composeConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { global } from '@storybook/global';

import { combineParameters } from '../parameters';
import { composeStepRunners } from './stepRunners';
import { normalizeArrays } from './normalizeArrays';

export function getField<TFieldType = any>(
moduleExportList: ModuleExports[],
Expand All @@ -16,10 +17,10 @@ export function getArrayField<TFieldType = any>(
field: string,
options: { reverseFileOrder?: boolean } = {}
): TFieldType[] {
return getField(moduleExportList, field).reduce(
(a: any, b: any) => (options.reverseFileOrder ? [...b, ...a] : [...a, ...b]),
[]
);
return getField(moduleExportList, field).reduce((prev: any, cur: any) => {
const normalized = normalizeArrays(cur);
return options.reverseFileOrder ? [...normalized, ...prev] : [...prev, ...normalized];
}, []);
}

export function getObjectField<TFieldType = Record<string, any>>(
Expand Down