mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-13 22:04:21 +03:00
chore(core): add tests for toURLSearchParams (#8322)
This commit is contained in:
parent
661594aec8
commit
bed70cd51a
@ -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';
|
||||||
|
@ -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';
|
||||||
|
@ -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';
|
||||||
|
@ -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,
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
@ -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';
|
||||||
|
@ -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])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -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';
|
||||||
|
@ -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])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user