chore(core): add tests for toURLSearchParams (#8322)

This commit is contained in:
fundon 2024-09-20 04:14:48 +00:00
parent 661594aec8
commit bed70cd51a
No known key found for this signature in database
GPG Key ID: 398BFA91AC539CF7
9 changed files with 87 additions and 30 deletions

View File

@ -6,8 +6,8 @@ import {
import { useJournalInfoHelper } from '@affine/core/components/hooks/use-journal'; import { useJournalInfoHelper } from '@affine/core/components/hooks/use-journal';
import { EditorService } from '@affine/core/modules/editor'; import { EditorService } from '@affine/core/modules/editor';
import { EditorSettingService } from '@affine/core/modules/editor-settting'; import { EditorSettingService } from '@affine/core/modules/editor-settting';
import { toURLSearchParams } from '@affine/core/modules/navigation';
import { PeekViewService } from '@affine/core/modules/peek-view'; import { PeekViewService } from '@affine/core/modules/peek-view';
import { toURLSearchParams } from '@affine/core/utils';
import type { DocMode } from '@blocksuite/blocks'; import type { DocMode } from '@blocksuite/blocks';
import { DocTitle, EdgelessEditor, PageEditor } from '@blocksuite/presets'; import { DocTitle, EdgelessEditor, PageEditor } from '@blocksuite/presets';
import type { Doc } from '@blocksuite/store'; import type { Doc } from '@blocksuite/store';

View File

@ -1,6 +1,6 @@
import { notify } from '@affine/component'; import { notify } from '@affine/component';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch'; 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 { useI18n } from '@affine/i18n';
import { track } from '@affine/track'; import { track } from '@affine/track';
import { type EditorHost } from '@blocksuite/block-std'; import { type EditorHost } from '@blocksuite/block-std';

View File

@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/utils'; import { toURLSearchParams } from '@affine/core/modules/navigation';
import type { DocMode } from '@blocksuite/blocks'; import type { DocMode } from '@blocksuite/blocks';
import { createContext, useCallback, useContext, useMemo } from 'react'; import { createContext, useCallback, useContext, useMemo } from 'react';
import type { NavigateFunction, NavigateOptions } from 'react-router-dom'; import type { NavigateFunction, NavigateOptions } from 'react-router-dom';

View File

@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/utils'; import { toURLSearchParams } from '@affine/core/modules/navigation';
import type { WorkspaceService } from '@toeverything/infra'; import type { WorkspaceService } from '@toeverything/infra';
import { import {
fromPromise, fromPromise,

View File

@ -2,7 +2,7 @@ import { afterEach } from 'node:test';
import { beforeEach, expect, test, vi } from 'vitest'; import { beforeEach, expect, test, vi } from 'vitest';
import { resolveLinkToDoc } from '../utils'; import { resolveLinkToDoc, toURLSearchParams } from '../utils';
function defineTest( function defineTest(
input: string, input: string,
@ -89,3 +89,51 @@ const testCases: [string, ReturnType<typeof resolveLinkToDoc>][] = [
for (const [input, expected] of testCases) { for (const [input, expected] of testCases) {
defineTest(input, expected); defineTest(input, expected);
} }
function defineTestWithToURLSearchParams(
input?: Partial<Record<string, string | string[]>>,
expected?: ReturnType<typeof toURLSearchParams>
) {
test(`toURLSearchParams(${JSON.stringify(input)})`, () => {
const result = toURLSearchParams(input);
expect(result).toEqual(expected);
});
}
const testCases2: [
Partial<Record<string, string | string[]> | undefined>,
ReturnType<typeof toURLSearchParams>,
][] = [
[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);
}

View File

@ -1,5 +1,9 @@
export { Navigator } from './entities/navigator'; export { Navigator } from './entities/navigator';
export { resolveLinkToDoc, resolveRouteLinkMeta } from './utils'; export {
resolveLinkToDoc,
resolveRouteLinkMeta,
toURLSearchParams,
} from './utils';
export { NavigationButtons } from './view/navigation-buttons'; export { NavigationButtons } from './view/navigation-buttons';
import { type Framework, WorkspaceScope } from '@toeverything/infra'; import { type Framework, WorkspaceScope } from '@toeverything/infra';

View File

@ -141,3 +141,31 @@ export const paramsParseOptions: ParseOptions = {
refreshKey: 'string', refreshKey: 'string',
}, },
}; };
export function toURLSearchParams(
params?: Partial<Record<string, string | string[]>>
) {
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])
);
}

View File

@ -1,4 +1,4 @@
import { toURLSearchParams } from '@affine/core/utils'; import { toURLSearchParams } from '@affine/core/modules/navigation';
import { Unreachable } from '@affine/env/constant'; import { Unreachable } from '@affine/env/constant';
import type { ReferenceParams } from '@blocksuite/blocks'; import type { ReferenceParams } from '@blocksuite/blocks';
import { Entity, LiveData } from '@toeverything/infra'; import { Entity, LiveData } from '@toeverything/infra';

View File

@ -1,5 +1,4 @@
import { appInfo } from '@affine/electron-api'; import { appInfo } from '@affine/electron-api';
import { isNil } from 'lodash-es';
interface AppUrlOptions { interface AppUrlOptions {
desktop?: boolean | string; desktop?: boolean | string;
@ -32,25 +31,3 @@ export function buildAppUrl(path: string, opts: AppUrlOptions = {}) {
return new URL(path, webBase).toString(); return new URL(path, webBase).toString();
} }
} }
export function toURLSearchParams(
params?: Partial<Record<string, string | string[]>>
) {
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])
);
}