fix(storybook): fix storybook test (#5970)

This commit is contained in:
EYHN 2024-03-01 02:30:04 +00:00
parent d4e78dd3d0
commit a231884d9e
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C
2 changed files with 53 additions and 29 deletions

View File

@ -1,6 +1,11 @@
import type { WorkspaceSubPath } from '@affine/core/shared';
import { useCallback, useMemo } from 'react';
import { type NavigateOptions, type To, useLocation } from 'react-router-dom';
import { createContext, useCallback, useContext, useMemo } from 'react';
import {
type NavigateFunction,
type NavigateOptions,
type To,
useLocation,
} from 'react-router-dom';
import { router } from '../router';
@ -9,16 +14,20 @@ export enum RouteLogic {
PUSH = 'push',
}
function navigate(to: To, option?: { replace?: boolean }) {
function defaultNavigate(to: To, option?: { replace?: boolean }) {
router.navigate(to, option).catch(err => {
console.error('Failed to navigate', err);
});
}
export const NavigateContext = createContext<NavigateFunction | null>(null);
// todo: add a name -> path helper in the results
export function useNavigateHelper() {
const location = useLocation();
const navigate = useContext(NavigateContext) ?? defaultNavigate;
const jumpToPage = useCallback(
(
workspaceId: string,
@ -29,7 +38,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToPageBlock = useCallback(
(
@ -42,7 +51,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToCollections = useCallback(
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
@ -50,7 +59,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToTags = useCallback(
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
@ -58,7 +67,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToTag = useCallback(
(
@ -70,7 +79,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToCollection = useCallback(
(
@ -82,7 +91,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToPublicWorkspacePage = useCallback(
(
@ -94,7 +103,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const jumpToSubPath = useCallback(
(
@ -106,7 +115,7 @@ export function useNavigateHelper() {
replace: logic === RouteLogic.REPLACE,
});
},
[]
[navigate]
);
const isPublicWorkspace = useMemo(() => {
@ -124,22 +133,31 @@ export function useNavigateHelper() {
[jumpToPage, jumpToPublicWorkspacePage, isPublicWorkspace]
);
const jumpToIndex = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/', {
replace: logic === RouteLogic.REPLACE,
});
}, []);
const jumpToIndex = useCallback(
(logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/', {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
);
const jumpTo404 = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/404', {
replace: logic === RouteLogic.REPLACE,
});
}, []);
const jumpToExpired = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/expired', {
replace: logic === RouteLogic.REPLACE,
});
}, []);
const jumpTo404 = useCallback(
(logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/404', {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
);
const jumpToExpired = useCallback(
(logic: RouteLogic = RouteLogic.PUSH) => {
return navigate('/expired', {
replace: logic === RouteLogic.REPLACE,
});
},
[navigate]
);
const jumpToSignIn = useCallback(
(
logic: RouteLogic = RouteLogic.PUSH,
@ -150,7 +168,7 @@ export function useNavigateHelper() {
...otherOptions,
});
},
[]
[navigate]
);
return useMemo(

View File

@ -1,8 +1,9 @@
import { NavigateContext } from '@affine/core/hooks/use-navigate-helper';
import { workbenchRoutes } from '@affine/core/router';
import { assertExists } from '@blocksuite/global/utils';
import type { StoryFn } from '@storybook/react';
import { screen, userEvent, waitFor, within } from '@storybook/testing-library';
import { Outlet, useLocation } from 'react-router-dom';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import {
reactRouterOutlets,
reactRouterParameters,
@ -11,8 +12,13 @@ import {
const FakeApp = () => {
const location = useLocation();
const navigate = useNavigate();
// fixme: `key` is a hack to force the storybook to re-render the outlet
return <Outlet key={location.pathname} />;
return (
<NavigateContext.Provider value={navigate}>
<Outlet key={location.pathname} />
</NavigateContext.Provider>
);
};
export default {