Storybook 覆盖导入方法
我的组件“ReportWrapper”如下所示,它导入“getReportData”,而“getReportData”又返回异步数据。
import { getReportData } from "../dataFecther";
export const ReportWrapper = ({ query }) => {
const { data, loading, error } = getReportData({ type: "1", query });
return (
<ReportTable
reportData={data}
error={error}
/>
);
它获取数据的方式可能不适合编写 Storybook 故事。 有没有办法将“getReportData”的导入覆盖为故事中的模拟导入之类的内容。
示例故事
export default {
title: "Storybook",
component: ReportWrapper,
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes
};
const Template = ({ args }) => {
return (
<ReportWrapper {...args} />
);
};
export const First = Template.bind({});
First.args = {
storyName: "First One",
};
My component 'ReportWrapper' is something like below where it import 'getReportData' which in turn return data async.
import { getReportData } from "../dataFecther";
export const ReportWrapper = ({ query }) => {
const { data, loading, error } = getReportData({ type: "1", query });
return (
<ReportTable
reportData={data}
error={error}
/>
);
The way it fetch data may not be suitable for writing Storybook stories.
Is there a way to override this import of 'getReportData' to something like a mock import in stories.
Sample story
export default {
title: "Storybook",
component: ReportWrapper,
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes
};
const Template = ({ args }) => {
return (
<ReportWrapper {...args} />
);
};
export const First = Template.bind({});
First.args = {
storyName: "First One",
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想在故事中使用不同的
getReportData
。我将其视为嘲笑故事中的模块。我设法使用 Storybook 的
webpackFinal
配置来做到这一点并添加一个 webpack 插件 - webpack 的NormalModuleReplacementPlugin
。基本上,您可以使用这种方法替换故事中的模块。
你可以在你的故事书配置中尝试这个:
If I understand correctly you want to use a different
getReportData
in stories. I read this as mocking a module in stories.I managed to do this using Storybook's
webpackFinal
config and to add a webpack plugin - webpack'sNormalModuleReplacementPlugin
.Basically you can replace a module in stories using this approach.
You could try this in your storybook config:
如果我理解正确的话,您想在组件内部运行一个方法,该方法来自另一个组件。
第一个组件想要从外部获取方法
,第二个组件想要使用第一个组件
If I understand correctly, you want to run a method inside of a component which that method comes from another component.
the first component that wants to take the method from outside
and the second component that to use the first component