From 3716f17413cf7a8de4bcedc71d8c01451e5a2d99 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 16 Jan 2020 11:54:02 +0800 Subject: [PATCH] Core: Fix default story sort --- lib/client-api/src/story_store.test.ts | 10 +++++++++- lib/client-api/src/story_store.ts | 10 +++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/client-api/src/story_store.test.ts b/lib/client-api/src/story_store.test.ts index 20c61f2893f4..f2dbf0a20c4f 100644 --- a/lib/client-api/src/story_store.test.ts +++ b/lib/client-api/src/story_store.test.ts @@ -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', () => { @@ -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', + ]); }); }); diff --git a/lib/client-api/src/story_store.ts b/lib/client-api/src/story_store.ts index fee7b26d093e..2982aeafa1c4 100644 --- a/lib/client-api/src/story_store.ts +++ b/lib/client-api/src/story_store.ts @@ -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, kind: string, idx: number) => { + const kindOrder = Object.values(this._legacydata).reduce( + (acc: Record, { kind }: any, idx: number) => { acc[kind] = idx; return acc; }, @@ -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 });