Skip to content

Commit

Permalink
Core: Fix default story sort (#9482)
Browse files Browse the repository at this point in the history
Core: Fix default story sort
  • Loading branch information
shilman committed Jan 16, 2020
2 parents aed216f + 3716f17 commit 3325552
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/client-api/src/story_store.test.ts
Expand Up @@ -127,10 +127,11 @@ describe('preview.story_store', () => {
});
});

describe('dumpStoryBook', () => {
describe('dumpStoryBook/getStoriesForManager', () => {
it('should return nothing when empty', () => {
const store = new StoryStore({ channel });
expect(store.dumpStoryBook()).toEqual([]);
expect(Object.keys(store.getStoriesForManager())).toEqual([]);
});

it('should return storybook with stories', () => {
Expand All @@ -151,6 +152,13 @@ describe('preview.story_store', () => {
stories: ['story-2.1', 'story-2.2'],
},
]);

expect(Object.keys(store.getStoriesForManager())).toEqual([
'kind-1--story-1-1',
'kind-1--story-1-2',
'kind-2--story-2-1',
'kind-2--story-2-2',
]);
});
});

Expand Down
10 changes: 7 additions & 3 deletions lib/client-api/src/story_store.ts
Expand Up @@ -136,8 +136,8 @@ export default class StoryStore extends EventEmitter {
// and thus lose order. However in `_legacydata` they just get zeroed out, meaning
// that the order is preserved. Here we can use this to preserve the load
// order if there is no sort function, although it's a hack.
const kindOrder = Object.keys(this._legacydata).reduce(
(acc: Record<string, number>, kind: string, idx: number) => {
const kindOrder = Object.values(this._legacydata).reduce(
(acc: Record<string, number>, { kind }: any, idx: number) => {
acc[kind] = idx;
return acc;
},
Expand Down Expand Up @@ -261,9 +261,13 @@ export default class StoryStore extends EventEmitter {
this.pushToManager();
}

getStoriesForManager = () => {
return this.extract({ includeDocsOnly: true });
};

pushToManager = debounce(() => {
if (this._channel) {
const stories = this.extract({ includeDocsOnly: true });
const stories = this.getStoriesForManager();

// send to the parent frame.
this._channel.emit(Events.SET_STORIES, { stories });
Expand Down

1 comment on commit 3325552

@vercel
Copy link

@vercel vercel bot commented on 3325552 Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.