Skip to content

Commit

Permalink
Merge pull request #11660 from storybookjs/11571-fix-prefix-redirect-2
Browse files Browse the repository at this point in the history
Core: Fix existing behavior with story prefixes
  • Loading branch information
shilman authored Jul 23, 2020
2 parents 4af0d0d + 07c7e47 commit c70c68e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lib/client-api/src/story_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,17 @@ describe('preview.story_store', () => {
expect(store.getSelection()).toEqual({ storyId: 'g2-a--1', viewMode: 'story' });
});

// Making sure the fix #11571 doesn't break this
it('selects the first story if there are two stories in the group of different lengths', () => {
const store = new StoryStore({ channel });
store.setSelectionSpecifier({ storySpecifier: 'a', viewMode: 'story' });
addStoryToStore(store, 'a', 'long-long-long', () => 0);
addStoryToStore(store, 'a', 'short', () => 0);
store.finishConfiguring();

expect(store.getSelection()).toEqual({ storyId: 'a--long-long-long', viewMode: 'story' });
});

it('selects nothing if the component or group does not exist', () => {
const store = new StoryStore({ channel });
store.setSelectionSpecifier({ storySpecifier: 'c', viewMode: 'story' });
Expand Down
9 changes: 6 additions & 3 deletions lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,12 @@ export default class StoryStore {
// '*' means select the first story. If there is none, we have no selection.
[foundStory] = stories;
} else if (typeof storySpecifier === 'string') {
// Find the story with the shortest id that matches the specifier (see #11571)
const foundStories = Object.values(stories).filter((s) => s.id.startsWith(storySpecifier));
[foundStory] = foundStories.sort((a, b) => a.id.length - b.id.length);
// Find the story with the exact id that matches the specifier (see #11571)
foundStory = Object.values(stories).find((s) => s.id === storySpecifier);
if (!foundStory) {
// Fallback to the first story that starts with the specifier
foundStory = Object.values(stories).find((s) => s.id.startsWith(storySpecifier));
}
} else {
// Try and find a story matching the name/kind, setting no selection if they don't exist.
const { name, kind } = storySpecifier;
Expand Down

0 comments on commit c70c68e

Please sign in to comment.