From 01545dd723195e0a1b1a3089fb87a2dc7ae0f5fb Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Sat, 14 Mar 2020 09:16:12 +0800 Subject: [PATCH 1/2] Re-enable failing args tests --- lib/client-api/src/story_store.test.ts | 138 ++++++++++++------------- lib/client-api/src/story_store.ts | 11 +- 2 files changed, 76 insertions(+), 73 deletions(-) diff --git a/lib/client-api/src/story_store.test.ts b/lib/client-api/src/story_store.test.ts index 36396128918d..63a38036ddb8 100644 --- a/lib/client-api/src/story_store.test.ts +++ b/lib/client-api/src/story_store.test.ts @@ -199,75 +199,75 @@ describe('preview.story_store', () => { }); describe('globalArgs', () => { - // it('is initialized to the value stored in parameters.globalArgs on the first story', () => { - // const store = new StoryStore({ channel }); - // addStoryToStore(store, 'a', '1', () => 0, { - // globalArgs: { - // arg1: 'arg1', - // arg2: 2, - // arg3: { complex: { object: ['type'] } }, - // }, - // }); - // store.finishConfiguring(); - // expect(store.getRawStory('a', '1').globalArgs).toEqual({ - // arg1: 'arg1', - // arg2: 2, - // arg3: { complex: { object: ['type'] } }, - // }); - // }); - - // it('is initialized to the default values stored in parameters.globalArgsTypes on the first story', () => { - // const store = new StoryStore({ channel }); - // addStoryToStore(store, 'a', '1', () => 0, { - // globalArgs: { - // arg1: 'arg1', - // arg2: 2, - // }, - // globalArgTypes: { - // arg2: { defaultValue: 'arg2' }, - // arg3: { defaultValue: { complex: { object: ['type'] } } }, - // }, - // }); - // store.finishConfiguring(); - // expect(store.getRawStory('a', '1').globalArgs).toEqual({ - // arg1: 'arg1', - // arg2: 2, - // arg3: { complex: { object: ['type'] } }, - // }); - // }); - - // it('on HMR it sensibly re-initializes with memory', () => { - // const store = new StoryStore({ channel }); - // addons.setChannel(channel); - // addStoryToStore(store, 'a', '1', () => 0, { - // globalArgs: { - // arg1: 'arg1', - // arg2: 2, - // arg3: { complex: { object: ['type'] } }, - // }, - // }); - // store.finishConfiguring(); - - // // HMR - // store.startConfiguring(); - // store.removeStoryKind('a'); - // addStoryToStore(store, 'a', '1', () => 0, { - // globalArgs: { - // arg2: 2, - // // Although we have changed the default there is no way to tell that the user didn't change - // // it themselves - // arg3: { complex: { object: ['changed'] } }, - // arg4: 'new', - // }, - // }); - // store.finishConfiguring(); - - // expect(store.getRawStory('a', '1').globalArgs).toEqual({ - // arg2: 2, - // arg3: { complex: { object: ['type'] } }, - // arg4: 'new', - // }); - // }); + it('is initialized to the value stored in parameters.globalArgs on the first story', () => { + const store = new StoryStore({ channel }); + addStoryToStore(store, 'a', '1', () => 0, { + globalArgs: { + arg1: 'arg1', + arg2: 2, + arg3: { complex: { object: ['type'] } }, + }, + }); + store.finishConfiguring(); + expect(store.getRawStory('a', '1').globalArgs).toEqual({ + arg1: 'arg1', + arg2: 2, + arg3: { complex: { object: ['type'] } }, + }); + }); + + it('is initialized to the default values stored in parameters.globalArgsTypes on the first story', () => { + const store = new StoryStore({ channel }); + addStoryToStore(store, 'a', '1', () => 0, { + globalArgs: { + arg1: 'arg1', + arg2: 2, + }, + globalArgTypes: { + arg2: { defaultValue: 'arg2' }, + arg3: { defaultValue: { complex: { object: ['type'] } } }, + }, + }); + store.finishConfiguring(); + expect(store.getRawStory('a', '1').globalArgs).toEqual({ + arg1: 'arg1', + arg2: 2, + arg3: { complex: { object: ['type'] } }, + }); + }); + + it('on HMR it sensibly re-initializes with memory', () => { + const store = new StoryStore({ channel }); + addons.setChannel(channel); + addStoryToStore(store, 'a', '1', () => 0, { + globalArgs: { + arg1: 'arg1', + arg2: 2, + arg3: { complex: { object: ['type'] } }, + }, + }); + store.finishConfiguring(); + + // HMR + store.startConfiguring(); + store.removeStoryKind('a'); + addStoryToStore(store, 'a', '1', () => 0, { + globalArgs: { + arg2: 2, + // Although we have changed the default there is no way to tell that the user didn't change + // it themselves + arg3: { complex: { object: ['changed'] } }, + arg4: 'new', + }, + }); + store.finishConfiguring(); + + expect(store.getRawStory('a', '1').globalArgs).toEqual({ + arg2: 2, + arg3: { complex: { object: ['type'] } }, + arg4: 'new', + }); + }); it('updateGlobalArgs changes the global args', () => { const store = new StoryStore({ channel }); diff --git a/lib/client-api/src/story_store.ts b/lib/client-api/src/story_store.ts index 9578dea95a65..d2ec19a6e3e0 100644 --- a/lib/client-api/src/story_store.ts +++ b/lib/client-api/src/story_store.ts @@ -53,14 +53,17 @@ const includeStory = (story: StoreItem, options: StoryOptions = { includeDocsOnl return !isStoryDocsOnly(story.parameters); }; +const inJest = () => process.env.JEST_WORKER_ID !== undefined; + const checkGlobalArgs = (parameters: Parameters) => { const { globalArgs, globalArgTypes } = parameters; - if (globalArgs || globalArgTypes) { - throw new Error( - `Global args/argTypes can only be set globally: ${JSON.stringify({ + if ((globalArgs || globalArgTypes) && !inJest()) { + logger.error( + 'Global args/argTypes can only be set globally', + JSON.stringify({ globalArgs, globalArgTypes, - })}` + }) ); } }; From 1f314332112e64e113c253500587bcccb0868296 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Sat, 14 Mar 2020 10:05:08 +0800 Subject: [PATCH 2/2] Global args: Remove problematic object spread --- lib/core/src/server/preview/iframe-webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/src/server/preview/iframe-webpack.config.js b/lib/core/src/server/preview/iframe-webpack.config.js index 5388b155ed06..a511886c300b 100644 --- a/lib/core/src/server/preview/iframe-webpack.config.js +++ b/lib/core/src/server/preview/iframe-webpack.config.js @@ -62,7 +62,7 @@ export default ({ if(args || argTypes) logger.warn('Invalid args/argTypes in config, ignoring.', JSON.stringify({ args, argTypes })); if (decorators) decorators.forEach(decorator => addDecorator(decorator)); - if (parameters || globalArgs || globalArgTypes) addParameters({ ...parameters, globalArgs, globalArgTypes }); + if (parameters || globalArgs || globalArgTypes) addParameters(Object.assign({}, parameters, { globalArgs, globalArgTypes })); if (parameterEnhancers) parameterEnhancers.forEach(enhancer => addParameterEnhancer(enhancer)); `; }