mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-07 01:09:54 +08:00
fix(storybook): fix storybook test (#5970)
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
import type { WorkspaceSubPath } from '@affine/core/shared';
|
import type { WorkspaceSubPath } from '@affine/core/shared';
|
||||||
import { useCallback, useMemo } from 'react';
|
import { createContext, useCallback, useContext, useMemo } from 'react';
|
||||||
import { type NavigateOptions, type To, useLocation } from 'react-router-dom';
|
import {
|
||||||
|
type NavigateFunction,
|
||||||
|
type NavigateOptions,
|
||||||
|
type To,
|
||||||
|
useLocation,
|
||||||
|
} from 'react-router-dom';
|
||||||
|
|
||||||
import { router } from '../router';
|
import { router } from '../router';
|
||||||
|
|
||||||
@@ -9,16 +14,20 @@ export enum RouteLogic {
|
|||||||
PUSH = 'push',
|
PUSH = 'push',
|
||||||
}
|
}
|
||||||
|
|
||||||
function navigate(to: To, option?: { replace?: boolean }) {
|
function defaultNavigate(to: To, option?: { replace?: boolean }) {
|
||||||
router.navigate(to, option).catch(err => {
|
router.navigate(to, option).catch(err => {
|
||||||
console.error('Failed to navigate', err);
|
console.error('Failed to navigate', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const NavigateContext = createContext<NavigateFunction | null>(null);
|
||||||
|
|
||||||
// todo: add a name -> path helper in the results
|
// todo: add a name -> path helper in the results
|
||||||
export function useNavigateHelper() {
|
export function useNavigateHelper() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
|
const navigate = useContext(NavigateContext) ?? defaultNavigate;
|
||||||
|
|
||||||
const jumpToPage = useCallback(
|
const jumpToPage = useCallback(
|
||||||
(
|
(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
@@ -29,7 +38,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToPageBlock = useCallback(
|
const jumpToPageBlock = useCallback(
|
||||||
(
|
(
|
||||||
@@ -42,7 +51,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToCollections = useCallback(
|
const jumpToCollections = useCallback(
|
||||||
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
|
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
|
||||||
@@ -50,7 +59,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToTags = useCallback(
|
const jumpToTags = useCallback(
|
||||||
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
|
(workspaceId: string, logic: RouteLogic = RouteLogic.PUSH) => {
|
||||||
@@ -58,7 +67,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToTag = useCallback(
|
const jumpToTag = useCallback(
|
||||||
(
|
(
|
||||||
@@ -70,7 +79,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToCollection = useCallback(
|
const jumpToCollection = useCallback(
|
||||||
(
|
(
|
||||||
@@ -82,7 +91,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToPublicWorkspacePage = useCallback(
|
const jumpToPublicWorkspacePage = useCallback(
|
||||||
(
|
(
|
||||||
@@ -94,7 +103,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
const jumpToSubPath = useCallback(
|
const jumpToSubPath = useCallback(
|
||||||
(
|
(
|
||||||
@@ -106,7 +115,7 @@ export function useNavigateHelper() {
|
|||||||
replace: logic === RouteLogic.REPLACE,
|
replace: logic === RouteLogic.REPLACE,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
|
|
||||||
const isPublicWorkspace = useMemo(() => {
|
const isPublicWorkspace = useMemo(() => {
|
||||||
@@ -124,22 +133,31 @@ export function useNavigateHelper() {
|
|||||||
[jumpToPage, jumpToPublicWorkspacePage, isPublicWorkspace]
|
[jumpToPage, jumpToPublicWorkspacePage, isPublicWorkspace]
|
||||||
);
|
);
|
||||||
|
|
||||||
const jumpToIndex = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
|
const jumpToIndex = useCallback(
|
||||||
return navigate('/', {
|
(logic: RouteLogic = RouteLogic.PUSH) => {
|
||||||
replace: logic === RouteLogic.REPLACE,
|
return navigate('/', {
|
||||||
});
|
replace: logic === RouteLogic.REPLACE,
|
||||||
}, []);
|
});
|
||||||
|
},
|
||||||
|
[navigate]
|
||||||
|
);
|
||||||
|
|
||||||
const jumpTo404 = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
|
const jumpTo404 = useCallback(
|
||||||
return navigate('/404', {
|
(logic: RouteLogic = RouteLogic.PUSH) => {
|
||||||
replace: logic === RouteLogic.REPLACE,
|
return navigate('/404', {
|
||||||
});
|
replace: logic === RouteLogic.REPLACE,
|
||||||
}, []);
|
});
|
||||||
const jumpToExpired = useCallback((logic: RouteLogic = RouteLogic.PUSH) => {
|
},
|
||||||
return navigate('/expired', {
|
[navigate]
|
||||||
replace: logic === RouteLogic.REPLACE,
|
);
|
||||||
});
|
const jumpToExpired = useCallback(
|
||||||
}, []);
|
(logic: RouteLogic = RouteLogic.PUSH) => {
|
||||||
|
return navigate('/expired', {
|
||||||
|
replace: logic === RouteLogic.REPLACE,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[navigate]
|
||||||
|
);
|
||||||
const jumpToSignIn = useCallback(
|
const jumpToSignIn = useCallback(
|
||||||
(
|
(
|
||||||
logic: RouteLogic = RouteLogic.PUSH,
|
logic: RouteLogic = RouteLogic.PUSH,
|
||||||
@@ -150,7 +168,7 @@ export function useNavigateHelper() {
|
|||||||
...otherOptions,
|
...otherOptions,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[]
|
[navigate]
|
||||||
);
|
);
|
||||||
|
|
||||||
return useMemo(
|
return useMemo(
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
import { NavigateContext } from '@affine/core/hooks/use-navigate-helper';
|
||||||
import { workbenchRoutes } from '@affine/core/router';
|
import { workbenchRoutes } from '@affine/core/router';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import type { StoryFn } from '@storybook/react';
|
import type { StoryFn } from '@storybook/react';
|
||||||
import { screen, userEvent, waitFor, within } from '@storybook/testing-library';
|
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 {
|
import {
|
||||||
reactRouterOutlets,
|
reactRouterOutlets,
|
||||||
reactRouterParameters,
|
reactRouterParameters,
|
||||||
@@ -11,8 +12,13 @@ import {
|
|||||||
|
|
||||||
const FakeApp = () => {
|
const FakeApp = () => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
const navigate = useNavigate();
|
||||||
// fixme: `key` is a hack to force the storybook to re-render the outlet
|
// 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 {
|
export default {
|
||||||
|
|||||||
Reference in New Issue
Block a user