From bed70cd51a1dfc4e530feb717c648cb935b78deb Mon Sep 17 00:00:00 2001 From: fundon Date: Fri, 20 Sep 2024 04:14:48 +0000 Subject: [PATCH] chore(core): add tests for toURLSearchParams (#8322) --- .../block-suite-editor/lit-adaper.tsx | 2 +- .../components/hooks/affine/use-share-url.ts | 2 +- .../components/hooks/use-navigate-helper.ts | 2 +- .../docs-search/services/docs-search.ts | 2 +- .../navigation/__tests__/utils.spec.ts | 50 ++++++++++++++++++- .../core/src/modules/navigation/index.ts | 6 ++- .../core/src/modules/navigation/utils.ts | 28 +++++++++++ .../modules/workbench/entities/workbench.ts | 2 +- packages/frontend/core/src/utils/url.ts | 23 --------- 9 files changed, 87 insertions(+), 30 deletions(-) diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx b/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx index 70b653b30b..9a4c0b470d 100644 --- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx +++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/lit-adaper.tsx @@ -6,8 +6,8 @@ import { import { useJournalInfoHelper } from '@affine/core/components/hooks/use-journal'; import { EditorService } from '@affine/core/modules/editor'; import { EditorSettingService } from '@affine/core/modules/editor-settting'; +import { toURLSearchParams } from '@affine/core/modules/navigation'; import { PeekViewService } from '@affine/core/modules/peek-view'; -import { toURLSearchParams } from '@affine/core/utils'; import type { DocMode } from '@blocksuite/blocks'; import { DocTitle, EdgelessEditor, PageEditor } from '@blocksuite/presets'; import type { Doc } from '@blocksuite/store'; diff --git a/packages/frontend/core/src/components/hooks/affine/use-share-url.ts b/packages/frontend/core/src/components/hooks/affine/use-share-url.ts index 597d636310..3eac45ae7e 100644 --- a/packages/frontend/core/src/components/hooks/affine/use-share-url.ts +++ b/packages/frontend/core/src/components/hooks/affine/use-share-url.ts @@ -1,6 +1,6 @@ import { notify } from '@affine/component'; import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch'; -import { toURLSearchParams } from '@affine/core/utils'; +import { toURLSearchParams } from '@affine/core/modules/navigation'; import { useI18n } from '@affine/i18n'; import { track } from '@affine/track'; import { type EditorHost } from '@blocksuite/block-std'; diff --git a/packages/frontend/core/src/components/hooks/use-navigate-helper.ts b/packages/frontend/core/src/components/hooks/use-navigate-helper.ts index 4da69c4166..24561cc2f8 100644 --- a/packages/frontend/core/src/components/hooks/use-navigate-helper.ts +++ b/packages/frontend/core/src/components/hooks/use-navigate-helper.ts @@ -1,4 +1,4 @@ -import { toURLSearchParams } from '@affine/core/utils'; +import { toURLSearchParams } from '@affine/core/modules/navigation'; import type { DocMode } from '@blocksuite/blocks'; import { createContext, useCallback, useContext, useMemo } from 'react'; import type { NavigateFunction, NavigateOptions } from 'react-router-dom'; diff --git a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts index b76755353d..9436abf8e5 100644 --- a/packages/frontend/core/src/modules/docs-search/services/docs-search.ts +++ b/packages/frontend/core/src/modules/docs-search/services/docs-search.ts @@ -1,4 +1,4 @@ -import { toURLSearchParams } from '@affine/core/utils'; +import { toURLSearchParams } from '@affine/core/modules/navigation'; import type { WorkspaceService } from '@toeverything/infra'; import { fromPromise, diff --git a/packages/frontend/core/src/modules/navigation/__tests__/utils.spec.ts b/packages/frontend/core/src/modules/navigation/__tests__/utils.spec.ts index 787d082495..f6899272f5 100644 --- a/packages/frontend/core/src/modules/navigation/__tests__/utils.spec.ts +++ b/packages/frontend/core/src/modules/navigation/__tests__/utils.spec.ts @@ -2,7 +2,7 @@ import { afterEach } from 'node:test'; import { beforeEach, expect, test, vi } from 'vitest'; -import { resolveLinkToDoc } from '../utils'; +import { resolveLinkToDoc, toURLSearchParams } from '../utils'; function defineTest( input: string, @@ -89,3 +89,51 @@ const testCases: [string, ReturnType][] = [ for (const [input, expected] of testCases) { defineTest(input, expected); } + +function defineTestWithToURLSearchParams( + input?: Partial>, + expected?: ReturnType +) { + test(`toURLSearchParams(${JSON.stringify(input)})`, () => { + const result = toURLSearchParams(input); + expect(result).toEqual(expected); + }); +} + +const testCases2: [ + Partial | undefined>, + ReturnType, +][] = [ + [undefined, undefined], + [ + { blockIds: ['x'] }, + new URLSearchParams({ + blockIds: 'x', + }), + ], + [{ blockIds: [] }, new URLSearchParams()], + [ + { blockIds: ['', 'x', ''] }, + new URLSearchParams({ + blockIds: 'x', + }), + ], + [{ mode: undefined }, new URLSearchParams()], + [{ mode: '' }, new URLSearchParams()], + [ + { mode: 'page', blockIds: ['x', 'y', 'z'], elementIds: ['a', 'b', 'c'] }, + new URLSearchParams({ + mode: 'page', + blockIds: 'x,y,z', + elementIds: 'a,b,c', + }), + ], + [ + { mode: undefined, blockIds: undefined, elementIds: undefined }, + new URLSearchParams(), + ], +]; + +for (const [input, expected] of testCases2) { + defineTestWithToURLSearchParams(input, expected); +} diff --git a/packages/frontend/core/src/modules/navigation/index.ts b/packages/frontend/core/src/modules/navigation/index.ts index 616852ed6b..853e1e89f5 100644 --- a/packages/frontend/core/src/modules/navigation/index.ts +++ b/packages/frontend/core/src/modules/navigation/index.ts @@ -1,5 +1,9 @@ export { Navigator } from './entities/navigator'; -export { resolveLinkToDoc, resolveRouteLinkMeta } from './utils'; +export { + resolveLinkToDoc, + resolveRouteLinkMeta, + toURLSearchParams, +} from './utils'; export { NavigationButtons } from './view/navigation-buttons'; import { type Framework, WorkspaceScope } from '@toeverything/infra'; diff --git a/packages/frontend/core/src/modules/navigation/utils.ts b/packages/frontend/core/src/modules/navigation/utils.ts index 6944ecf42e..0b12b46153 100644 --- a/packages/frontend/core/src/modules/navigation/utils.ts +++ b/packages/frontend/core/src/modules/navigation/utils.ts @@ -141,3 +141,31 @@ export const paramsParseOptions: ParseOptions = { refreshKey: 'string', }, }; + +export function toURLSearchParams( + params?: Partial> +) { + if (!params) return; + + const items = Object.entries(params) + .filter(([_, v]) => !isNil(v)) + .filter(([_, v]) => { + if (typeof v === 'string') { + return v.length > 0; + } + if (Array.isArray(v)) { + return v.length > 0; + } + return false; + }) + .map(([k, v]) => [k, Array.isArray(v) ? v.filter(v => v.length) : v]) as [ + string, + string | string[], + ][]; + + return new URLSearchParams( + items + .filter(([_, v]) => v.length) + .map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v]) + ); +} diff --git a/packages/frontend/core/src/modules/workbench/entities/workbench.ts b/packages/frontend/core/src/modules/workbench/entities/workbench.ts index 59d9d8f1d5..a1fae2beda 100644 --- a/packages/frontend/core/src/modules/workbench/entities/workbench.ts +++ b/packages/frontend/core/src/modules/workbench/entities/workbench.ts @@ -1,4 +1,4 @@ -import { toURLSearchParams } from '@affine/core/utils'; +import { toURLSearchParams } from '@affine/core/modules/navigation'; import { Unreachable } from '@affine/env/constant'; import type { ReferenceParams } from '@blocksuite/blocks'; import { Entity, LiveData } from '@toeverything/infra'; diff --git a/packages/frontend/core/src/utils/url.ts b/packages/frontend/core/src/utils/url.ts index 0740033af5..f58d29927c 100644 --- a/packages/frontend/core/src/utils/url.ts +++ b/packages/frontend/core/src/utils/url.ts @@ -1,5 +1,4 @@ import { appInfo } from '@affine/electron-api'; -import { isNil } from 'lodash-es'; interface AppUrlOptions { desktop?: boolean | string; @@ -32,25 +31,3 @@ export function buildAppUrl(path: string, opts: AppUrlOptions = {}) { return new URL(path, webBase).toString(); } } - -export function toURLSearchParams( - params?: Partial> -) { - if (!params) return; - - const items = Object.entries(params) - .filter(([_, v]) => !isNil(v)) - .filter(([_, v]) => { - if (typeof v === 'string') { - return v.length > 0; - } - if (Array.isArray(v)) { - return v.length > 0; - } - return false; - }) as [string, string | string[]][]; - - return new URLSearchParams( - items.map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v]) - ); -}