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

Fix : Add remove trailing slash in transformStoryIndexToStoriesHash function #26711

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 45 additions & 1 deletion code/lib/manager-api/src/lib/stories.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, it, expect } from 'vitest';
import type { StoryIndexV2, StoryIndexV3 } from '@storybook/types';
import { transformStoryIndexV2toV3, transformStoryIndexV3toV4 } from './stories';
import {
transformStoryIndexV2toV3,
transformStoryIndexV3toV4,
transformStoryIndexToStoriesHash,
} from './stories';

const baseV2: StoryIndexV2['stories'][0] = {
id: '1',
Expand Down Expand Up @@ -151,3 +155,43 @@ describe('transformStoryIndexV3toV4', () => {
`);
});
});

describe('transformStoryIndexToStoriesHash', () => {
it('should correctly remove trailing slash from story titles without throwing errors', () => {
const indexV3: StoryIndexV3 = {
v: 3,
stories: {
'1': {
...baseV3,
id: '1',
kind: 'story',
title: 'scenes/MessageComposition/shared/ComposerImageLibraryPanel/',
parameters: {
docsOnly: true,
},
},
'2': {
...baseV3,
id: '2',
kind: 'story',
title: 'scenes/MessageComposition/shared/ComposerImageLibraryPanel',
parameters: {
docsOnly: true,
},
},
},
};

const options = {
provider: { getConfig: () => ({}), handleAPI: () => {} },
docsOptions: { docsMode: false },
filters: {},
status: {},
};

const result = transformStoryIndexToStoriesHash(indexV3, options);

expect(() => transformStoryIndexToStoriesHash(indexV3, options)).not.toThrow();
expect(result['1']).toBeDefined();
});
});
3 changes: 2 additions & 1 deletion code/lib/manager-api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { type API, combineParameters, type State } from '../index';
import merge from './merge';

const TITLE_PATH_SEPARATOR = /\s*\/\s*/;
const FIND_TRAILING_SLASH = /\/$/;

export const denormalizeStoryParameters = ({
globalParameters,
Expand Down Expand Up @@ -184,7 +185,7 @@ export const transformStoryIndexToStoriesHash = (

// First, split the title into a set of names, separated by '/' and trimmed.
const { title } = item;
const groups = title.trim().split(TITLE_PATH_SEPARATOR);
const groups = title.replace(FIND_TRAILING_SLASH, '').trim().split(TITLE_PATH_SEPARATOR);
const root = (!setShowRoots || showRoots) && groups.length > 1 ? [groups.shift()] : [];
const names = [...root, ...groups];

Expand Down