Change story update functions to be updateX

Thanks @shilman!

Co-Authored-By: Michael Shilman <shilman@users.noreply.github.com>
This commit is contained in:
Tom Coleman 2020-03-04 17:23:04 +11:00 committed by GitHub
parent 3aaba64e1e
commit 533b1fc2e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 16 deletions

View File

@ -428,12 +428,12 @@ export function useStoryState<S>(defaultState?: S) {
return useSharedState<S>(`story-state-${storyId}`, defaultState);
}
export function useArgs() {
export function useArgs(): [Args, (newArgs: Args) => void] {
const {
api: { getCurrentStoryData, setStoryArgs },
api: { getCurrentStoryData, updateStoryArgs },
} = useStorybookApi();
const { id, args } = getCurrentStoryData();
return [args, (newArgs: Args) => setStoryArgs(id, newArgs)];
return [args, (newArgs: Args) => updateStoryArgs(id, newArgs)];
}

View File

@ -244,7 +244,7 @@ const initStoriesApi = ({
store.setState({ storiesHash });
};
const setStoryArgs = (id: StoryId, newArgs: Args) => {
const updateStoryArgs = (id: StoryId, newArgs: Args) => {
if (!fullApi) throw new Error('Cannot set story args until api has been initialized');
fullApi.emit(CHANGE_STORY_ARGS, id, newArgs);

View File

@ -558,12 +558,12 @@ describe('stories API', () => {
});
});
it('setStoryArgs emits CHANGE_STORY_ARGS and does not change anything', () => {
it('updateStoryArgs emits CHANGE_STORY_ARGS and does not change anything', () => {
const navigate = jest.fn();
const store = createMockStore();
const {
api: { setStories, setStoryArgs },
api: { setStories, updateStoryArgs },
init,
} = initStories({ store, navigate, provider });
@ -575,7 +575,7 @@ describe('stories API', () => {
const emit = jest.fn();
init({ api: { emit, on: jest.fn() } });
setStoryArgs('a--1', { foo: 'bar' });
updateStoryArgs('a--1', { foo: 'bar' });
expect(emit).toHaveBeenCalledWith(CHANGE_STORY_ARGS, 'a--1', { foo: 'bar' });
const { storiesHash: changedStoriesHash } = store.getState();

View File

@ -111,15 +111,15 @@ describe('preview.story_store', () => {
});
});
it('setStoryArgs changes the args of a story, per-key', () => {
it('updateStoryArgs changes the args of a story, per-key', () => {
const store = new StoryStore({ channel });
addStoryToStore(store, 'a', '1', () => 0);
expect(store.getRawStory('a', '1').args).toEqual({});
store.setStoryArgs('a--1', { foo: 'bar' });
store.updateStoryArgs('a--1', { foo: 'bar' });
expect(store.getRawStory('a', '1').args).toEqual({ foo: 'bar' });
store.setStoryArgs('a--1', { baz: 'bing' });
store.updateStoryArgs('a--1', { baz: 'bing' });
expect(store.getRawStory('a', '1').args).toEqual({ foo: 'bar', baz: 'bing' });
});
@ -127,7 +127,7 @@ describe('preview.story_store', () => {
const storyFn = jest.fn();
const store = new StoryStore({ channel });
addStoryToStore(store, 'a', '1', storyFn);
store.setStoryArgs('a--1', { foo: 'bar' });
store.updateStoryArgs('a--1', { foo: 'bar' });
store.getRawStory('a', '1').storyFn();
expect(storyFn).toHaveBeenCalledWith(
@ -137,7 +137,7 @@ describe('preview.story_store', () => {
);
});
it('setStoryArgs emits STORY_ARGS_CHANGED', () => {
it('updateStoryArgs emits STORY_ARGS_CHANGED', () => {
const onArgsChangedChannel = jest.fn();
const testChannel = mockChannel();
testChannel.on(Events.STORY_ARGS_CHANGED, onArgsChangedChannel);
@ -145,10 +145,10 @@ describe('preview.story_store', () => {
const store = new StoryStore({ channel: testChannel });
addStoryToStore(store, 'a', '1', () => 0);
store.setStoryArgs('a--1', { foo: 'bar' });
store.updateStoryArgs('a--1', { foo: 'bar' });
expect(onArgsChangedChannel).toHaveBeenCalledWith('a--1', { foo: 'bar' });
store.setStoryArgs('a--1', { baz: 'bing' });
store.updateStoryArgs('a--1', { baz: 'bing' });
expect(onArgsChangedChannel).toHaveBeenCalledWith('a--1', { foo: 'bar', baz: 'bing' });
});

View File

@ -113,7 +113,7 @@ export default class StoryStore {
);
this._channel.on(Events.CHANGE_STORY_ARGS, (id: string, newArgs: Args) =>
this.setStoryArgs(id, newArgs)
this.updateStoryArgs(id, newArgs)
);
}
@ -434,7 +434,7 @@ export default class StoryStore {
this.getStoriesForKind(kind).map(story => this.cleanHooks(story.id));
}
setStoryArgs(id: string, newArgs: Args) {
updateStoryArgs(id: string, newArgs: Args) {
if (!this._stories[id]) throw new Error(`No story for id ${id}`);
const { args } = this._stories[id];
this._stories[id].args = { ...args, ...newArgs };