Skip to content

Commit

Permalink
Add withRouter parameter to Storybook to optionally configure a rou…
Browse files Browse the repository at this point in the history
…ter per-story

Fix the ChangePassword stories by adding a memory router
  • Loading branch information
nickgros committed Jan 3, 2025
1 parent e88e7b4 commit fae84ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const meta: Meta<typeof ChangePassword> = {
component: ChangePassword,
parameters: {
stack: 'mock',
withRouter: true,
},
decorators: [
Story => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import dayjs from 'dayjs'
import { ReactNode, Suspense, useEffect, useMemo, useState } from 'react'
import { createMemoryRouter, RouterProvider } from 'react-router-dom'
import SynapseClient, {
getAccessTokenFromCookie,
getAuthenticatedOn,
Expand Down Expand Up @@ -72,6 +73,7 @@ export function StorybookComponentWrapper(props: {
}
parameters: {
stack?: SynapseStack
withRouter?: boolean
}
}
}) {
Expand Down Expand Up @@ -119,7 +121,7 @@ export function StorybookComponentWrapper(props: {
[accessToken, currentStack, storybookContext.args.isAuthenticated],
)

return (
const wrappedStory = (
<Suspense fallback={'global suspense loading...'}>
<QueryClientProvider client={storybookQueryClient}>
<SynapseContextProvider
Expand All @@ -135,6 +137,19 @@ export function StorybookComponentWrapper(props: {
</QueryClientProvider>
</Suspense>
)

if (!storybookContext.parameters.withRouter) {
return wrappedStory
}

const router = createMemoryRouter([
{
path: '/',
element: wrappedStory,
},
])

return <RouterProvider router={router} />
}

export default StorybookComponentWrapper

0 comments on commit fae84ee

Please sign in to comment.