From ff0453400bf86f58278475a05c99dd75e8990182 Mon Sep 17 00:00:00 2001 From: Jimmy Somsanith Date: Mon, 17 Jun 2019 23:51:22 +0200 Subject: [PATCH] Fix addon api --- lib/api/src/modules/addons.ts | 26 +++++++++++++++++-------- lib/api/src/tests/addons.test.js | 33 ++++++++++++++++---------------- lib/ui/src/containers/panel.js | 2 +- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/api/src/modules/addons.ts b/lib/api/src/modules/addons.ts index 35db70f04758..ce95b9765bde 100644 --- a/lib/api/src/modules/addons.ts +++ b/lib/api/src/modules/addons.ts @@ -43,10 +43,16 @@ interface Panels { type StateMerger = (input: S) => S; +interface StoryInput { + parameters: { + [parameterName: string]: any; + }; +} + export interface SubAPI { getElements: (type: Types) => Collection; getPanels: () => Collection; - getStoryPanels: (storyId: string) => Collection; + getStoryPanels: () => Collection; getSelectedPanel: () => string; setSelectedPanel: (panelName: string) => void; setAddonState( @@ -74,17 +80,21 @@ export default ({ provider, store }: Module) => { const api: SubAPI = { getElements: type => provider.getElements(type), getPanels: () => api.getElements(types.PANEL), - getStoryPanels: storyId => { - const storyParameters = provider.getParameters(storyId); - const panels = api.getPanels(); - if (!panels || !storyParameters) { - return panels; + getStoryPanels: () => { + const allPanels = api.getPanels(); + const { storyId, storiesHash } = store.getState(); + const storyInput = storyId && (storiesHash[storyId] as StoryInput); + + if (!allPanels || !storyInput) { + return allPanels; } + const { parameters } = storyInput; + const filteredPanels: Collection = {}; - Object.entries(panels).forEach(([id, panel]) => { + Object.entries(allPanels).forEach(([id, panel]) => { const { paramKey } = panel; - if (paramKey && storyParameters[paramKey] && storyParameters[paramKey].disabled) { + if (paramKey && parameters[paramKey] && parameters[paramKey].disabled) { return; } filteredPanels[id] = panel; diff --git a/lib/api/src/tests/addons.test.js b/lib/api/src/tests/addons.test.js index 95a016a71508..c17c485518ec 100644 --- a/lib/api/src/tests/addons.test.js +++ b/lib/api/src/tests/addons.test.js @@ -61,11 +61,7 @@ describe('Addons API', () => { describe('#getStoryPanels', () => { it('should return all panels by default', () => { // given - const providerWithStoryParameters = { - ...provider, - getParameters: () => {}, - }; - const { api } = initAddons({ provider: providerWithStoryParameters, store }); + const { api } = initAddons({ provider, store }); // when const filteredPanels = api.getStoryPanels(); @@ -77,21 +73,24 @@ describe('Addons API', () => { it('should filter disabled addons', () => { // given const storyId = 'story 1'; - const providerWithStoryParameters = { - ...provider, - getParameters: id => { - if (id === storyId) { - return { - a11y: { disabled: true }, - }; - } - return null; - }, + const storeWithStory = { + getState: () => ({ + storyId, + storiesHash: { + [storyId]: { + parameters: { + a11y: { disabled: true }, + }, + }, + }, + }), + setState: jest.fn(), }; - const { api } = initAddons({ provider: providerWithStoryParameters, store }); + + const { api } = initAddons({ provider, store: storeWithStory }); // when - const filteredPanels = api.getStoryPanels(storyId); + const filteredPanels = api.getStoryPanels(); // then expect(filteredPanels).toEqual({ diff --git a/lib/ui/src/containers/panel.js b/lib/ui/src/containers/panel.js index fcf2e7eca281..88821840822d 100644 --- a/lib/ui/src/containers/panel.js +++ b/lib/ui/src/containers/panel.js @@ -11,7 +11,7 @@ const createPanelActions = memoize(1)(api => ({ })); const mapper = ({ state, api }) => ({ - panels: api.getStoryPanels(state.storyId), + panels: api.getStoryPanels(), selectedPanel: api.getSelectedPanel(), panelPosition: state.layout.panelPosition, actions: createPanelActions(api),