Wire in initial global Args

This commit is contained in:
Tom Coleman 2020-05-25 19:55:11 +10:00
parent a86c083493
commit 569ec730d8
3 changed files with 22 additions and 2 deletions

View File

@ -112,6 +112,7 @@ export interface SetStoriesPayload {
export interface SetStoriesPayloadV2 extends SetStoriesPayload {
v: 2;
error?: Error;
globalArgs: Args;
globalParameters: Parameters;
kindParameters: {
[kind: string]: Parameters;

View File

@ -1,5 +1,6 @@
import { UPDATE_GLOBAL_ARGS, GLOBAL_ARGS_UPDATED } from '@storybook/core-events';
import { SET_STORIES, UPDATE_GLOBAL_ARGS, GLOBAL_ARGS_UPDATED } from '@storybook/core-events';
import { Args, ModuleFn } from '../index';
import { SetStoriesPayloadV2 } from '../lib/stories';
export interface SubState {
globalArgs: Args;
@ -23,6 +24,9 @@ export const init: ModuleFn = ({ store, fullAPI }) => {
const initModule = () => {
fullAPI.on(GLOBAL_ARGS_UPDATED, (globalArgs: Args) => store.setState({ globalArgs }));
fullAPI.on(SET_STORIES, ({ globalArgs }: SetStoriesPayloadV2) =>
store.setState({ globalArgs })
);
};
return {

View File

@ -1,5 +1,5 @@
import EventEmitter from 'event-emitter';
import { UPDATE_GLOBAL_ARGS, GLOBAL_ARGS_UPDATED } from '@storybook/core-events';
import { SET_STORIES, UPDATE_GLOBAL_ARGS, GLOBAL_ARGS_UPDATED } from '@storybook/core-events';
import { ModuleArgs, API } from '../index';
import { init as initModule, SubAPI } from '../modules/globalArgs';
@ -24,6 +24,21 @@ describe('stories API', () => {
});
});
it('set global args on SET_STORIES', () => {
const api = EventEmitter();
const store = createMockStore();
const { state, init } = initModule(({ store, fullAPI: api } as unknown) as ModuleArgs);
store.setState(state);
init();
api.emit(SET_STORIES, { globalArgs: { a: 'b' } });
expect(store.getState()).toEqual({ globalArgs: { a: 'b' } });
expect(state).toEqual({
globalArgs: {},
});
});
it('updates the state when the preview emits GLOBAL_ARGS_UPDATED', () => {
const api = EventEmitter();
const store = createMockStore();