Skip to content

Commit

Permalink
chore(core): add tests for toURLSearchParams (#8322)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 20, 2024
1 parent 661594a commit bed70cd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,3 +89,51 @@ const testCases: [string, ReturnType<typeof resolveLinkToDoc>][] = [
for (const [input, expected] of testCases) {
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);
}
6 changes: 5 additions & 1 deletion packages/frontend/core/src/modules/navigation/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
28 changes: 28 additions & 0 deletions packages/frontend/core/src/modules/navigation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,31 @@ export const paramsParseOptions: ParseOptions = {
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])
);
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
23 changes: 0 additions & 23 deletions packages/frontend/core/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { appInfo } from '@affine/electron-api';
import { isNil } from 'lodash-es';

interface AppUrlOptions {
desktop?: boolean | string;
Expand Down Expand Up @@ -32,25 +31,3 @@ export function buildAppUrl(path: string, opts: AppUrlOptions = {}) {
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])
);
}

0 comments on commit bed70cd

Please sign in to comment.