-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: remove hooks from the withRoundTrip * feat: support vue3
- Loading branch information
Showing
7 changed files
with
62 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,58 @@ | ||
import { useEffect } from 'react'; | ||
import { FORCE_RE_RENDER } from '@storybook/core-events'; | ||
import { useChannel, useParameter } from '@storybook/addons'; | ||
import { FORCE_RE_RENDER, STORY_CHANGED } from '@storybook/core-events'; | ||
import addons from '@storybook/addons'; | ||
import { EVENTS, PARAM_KEY, GLOBAL_PARAM_KEY } from './utils/constants'; | ||
import faker from './utils/faker'; | ||
|
||
export const withRoundTrip = (storyFn) => { | ||
const paramData = useParameter(PARAM_KEY, []); | ||
const mockAddonConfigs = useParameter(GLOBAL_PARAM_KEY, { | ||
const getParameter = (parameters, key, defaultValue) => { | ||
return parameters[key] || defaultValue; | ||
}; | ||
|
||
let INITIAL_MOUNT_STATE = true; | ||
let STORY_CHANGED_STATE = false; | ||
|
||
const channel = addons.getChannel(); | ||
|
||
export const withRoundTrip = (storyFn, context) => { | ||
const { parameters } = context; | ||
const paramData = getParameter(parameters, PARAM_KEY, []); | ||
const mockAddonConfigs = getParameter(parameters, GLOBAL_PARAM_KEY, { | ||
refreshStoryOnUpdate: false, | ||
globalMockData: [], | ||
}); | ||
const { globalMockData, refreshStoryOnUpdate } = mockAddonConfigs; | ||
const data = [...globalMockData, ...paramData]; | ||
|
||
/** | ||
* Initiate event listener for story change and update. | ||
* This state executes once to setup. | ||
*/ | ||
if (INITIAL_MOUNT_STATE) { | ||
faker.makeInitialRequestMap(data); | ||
|
||
channel.emit(EVENTS.SEND, faker.getRequests()); | ||
|
||
const emit = useChannel({ | ||
[EVENTS.UPDATE]: (item, name, value) => { | ||
faker.update(item, name, value); | ||
channel.on(STORY_CHANGED, () => { | ||
STORY_CHANGED_STATE = true; | ||
}); | ||
|
||
const { refreshStoryOnUpdate } = mockAddonConfigs; | ||
channel.on(EVENTS.UPDATE, ({ item, key, value }) => { | ||
faker.update(item, key, value); | ||
const req = faker.getRequests(); | ||
emit(EVENTS.SEND, req); | ||
refreshStoryOnUpdate && emit(FORCE_RE_RENDER); | ||
}, | ||
}); | ||
channel.emit(EVENTS.SEND, req); | ||
refreshStoryOnUpdate && channel.emit(FORCE_RE_RENDER); | ||
}); | ||
|
||
useEffect(() => { | ||
const { globalMockData } = mockAddonConfigs; | ||
const data = [...globalMockData, ...paramData]; | ||
faker.makeInitialRequestMap(data); | ||
emit(EVENTS.SEND, faker.getRequests()); | ||
}, []); | ||
INITIAL_MOUNT_STATE = false; | ||
} | ||
|
||
return storyFn(); | ||
/** | ||
* This state executes when a story change. So that it can | ||
* take the new parameters to setup the faker requests. | ||
*/ | ||
if (STORY_CHANGED_STATE) { | ||
faker.makeInitialRequestMap(data); | ||
channel.emit(EVENTS.SEND, faker.getRequests()); | ||
STORY_CHANGED_STATE = false; | ||
} | ||
return storyFn(context); | ||
}; |
83e5955
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
storybook-addon-mock – ./
storybook-addon-mock-git-master-nutboltu.vercel.app
storybook-addon-mock-nutboltu.vercel.app
storybook-addon-mock.vercel.app