fix: reduce useState and useEffect (#2223)

This commit is contained in:
Himself65
2023-05-04 17:53:52 -05:00
committed by GitHub
parent 52b9734a7b
commit 097cce34b5
5 changed files with 164 additions and 81 deletions
+16 -4
View File
@@ -3,6 +3,7 @@
*/
import 'fake-indexeddb/auto';
import { UNTITLED_WORKSPACE_NAME } from '@affine/env';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import type { Page } from '@blocksuite/store';
import { assertExists } from '@blocksuite/store';
@@ -10,7 +11,7 @@ import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
import { renderHook } from '@testing-library/react';
import { useBlockSuiteWorkspacePageIsPublic } from '@toeverything/hooks/use-block-suite-workspace-page-is-public';
import { useBlockSuiteWorkspacePageTitle } from '@toeverything/hooks/use-block-suite-workspace-page-title';
import { describe, expect, test } from 'vitest';
import { describe, expect, test, vitest } from 'vitest';
import { beforeEach } from 'vitest';
import { useBlockSuiteWorkspaceName } from '../use-block-suite-workspace-name';
@@ -30,9 +31,9 @@ beforeEach(async () => {
const frameId = page.addBlock('affine:frame', {}, pageBlockId);
page.addBlock('affine:paragraph', {}, frameId);
};
initPage(blockSuiteWorkspace.createPage('page0'));
initPage(blockSuiteWorkspace.createPage('page1'));
initPage(blockSuiteWorkspace.createPage('page2'));
initPage(blockSuiteWorkspace.createPage({ id: 'page0' }));
initPage(blockSuiteWorkspace.createPage({ id: 'page1' }));
initPage(blockSuiteWorkspace.createPage({ id: 'page2' }));
});
describe('useBlockSuiteWorkspaceName', () => {
@@ -48,6 +49,17 @@ describe('useBlockSuiteWorkspaceName', () => {
workspaceNameHook.result.current[1]('test 3');
expect(blockSuiteWorkspace.meta.name).toBe('test 3');
});
test('null', () => {
const workspaceNameHook = renderHook(() =>
useBlockSuiteWorkspaceName(null)
);
vitest.spyOn(globalThis.console, 'warn');
expect(workspaceNameHook.result.current[0]).toBe(UNTITLED_WORKSPACE_NAME);
workspaceNameHook.result.current[1]('test');
expect(globalThis.console.warn).toHaveBeenCalledTimes(2);
expect(workspaceNameHook.result.current[0]).toBe(UNTITLED_WORKSPACE_NAME);
});
});
describe('useBlockSuiteWorkspacePageTitle', () => {