storybook [Bug]:在自动文档页面上显示带有mount-in-play功能的story时,行为不一致,

csbfibhn  于 3个月前  发布在  其他
关注(0)|答案(1)|浏览(23)

描述bug

如果在play函数中使用了mount,自动生成的文档页面将显示第一个故事,并显示以下错误信息:

This story mounts inside of play. Set autoplay to true to view this story.

STORIES部分没有显示任何使用mount函数的故事。
如与@kasperpeulen和@shilman讨论,我们不再希望过滤掉这些故事。

svdrlsy4

svdrlsy41#

为了解决在使用自动文档页面上的mount-in-play功能的故事中出现不一致行为的问题,请按照以下步骤操作:

  • 更新 StoryRender.ts:

    • 修改 render 方法以确保带有mount-in-play功能的故事不会被过滤掉。
    • 确保为这些故事设置了 autoplay 选项为 true
// In StoryRender.ts
async render({
  initial = false,
  forceRemount = false,
}: {
  initial?: boolean;
  forceRemount?: boolean;
} = {}) {
  // Existing code...

  // Ensure autoplay is true for stories with mount-in-play functions
  if (this.story?.usesMount) {
    this.renderOptions.autoplay = true;
  }

  // Existing code...
}
  • 更新 StoryRender.test.ts:

    • 为自动文档页面上带有mount-in-play功能的故事添加测试,以确保它们能够正确渲染。
// In StoryRender.test.ts
it('renders stories with mount-in-play functions correctly', async () => {
  const story = buildStory({
    usesMount: true,
    playFunction: async ({ mount }) => {
      await mount();
    },
  });
  const render = new StoryRender(
    new Channel({}),
    buildStore(),
    vi.fn() as any,
    {} as any,
    entry.id,
    'story',
    { autoplay: true },
    story
  );

  await render.renderToElement({} as any);
  expect(mountSpy).toHaveBeenCalledOnce();
});

这些更改确保了带有mount-in-play功能的故事不会被过滤掉,并且在自动文档页面上能够正确渲染。

参考文献

/code/e2e-tests/addon-docs.spec.ts
/code/core/src/preview-api/modules/preview-web/render/StoryRender.test.ts
/code/core/src/preview-api/modules/preview-web/render/StoryRender.ts

关于Greptile

此响应为您提供了一个研究起点,而不是精确的解决方案。
帮助我们改进!如果这有帮助,请留下一个👍,如果它无关紧要,请留下一个👎。

相关问题