Skip to content

Commit

Permalink
Accept router params via storybook parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Jan 23, 2025
1 parent ffbee54 commit 049f402
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
37 changes: 24 additions & 13 deletions packages/profile/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import React from "react";
import type { Preview, ReactRenderer } from "@storybook/react";
import type { Decorator, Preview, ReactRenderer } from "@storybook/react";
import { withThemeByClassName } from "@storybook/addon-themes";
import { SonnerToaster } from "@cartridge/ui-next";
import { Provider } from "../src/components/provider";

import "../src/index.css";
import { MemoryRouter, Route, Routes } from "react-router-dom";

const routerDecorator: Decorator = (Story, { parameters: { router } }) => {
const params = router?.params || {}
const path = Object.keys(params).length
? `/:${Object.keys(params).join("/:")}`
: "/"
const url = `/${Object.values(params).join("/")}`

return (
<MemoryRouter initialEntries={[url]}>
<Provider>
<Routes>
<Route
path={path}
element={<Story />}
/>
</Routes>
<SonnerToaster />
</Provider>
</MemoryRouter>
);
};

const preview: Preview = {
parameters: {
controls: {
Expand Down Expand Up @@ -39,18 +61,7 @@ const preview: Preview = {
},
defaultTheme: "dark",
}),
(Story,
// { parameters }
) => (
<MemoryRouter initialEntries={["/project-1"]}>
<Provider>
<Routes>
<Route path="/:project" element={<Story />} />
</Routes>
<SonnerToaster />
</Provider>
</MemoryRouter>
),
routerDecorator,
],
};

Expand Down
7 changes: 7 additions & 0 deletions packages/profile/src/components/inventory/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { decorator } from "@/hooks/account.mock";

const meta = {
component: Inventory,
parameters: {
router: {
params: {
project: "test-0",
},
},
},
} satisfies Meta<typeof Inventory>;

export default meta;
Expand Down
13 changes: 13 additions & 0 deletions packages/profile/src/components/inventory/token/token.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Meta, StoryObj } from "@storybook/react";

import { Token } from "./token";

const meta = {
component: Token,
} satisfies Meta<typeof Token>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {};

0 comments on commit 049f402

Please sign in to comment.