Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

test: improve coverage (#1780)

Himself65f5574c68fede
diff --git a/packages/workspace/src/local/__tests__/crud.spec.ts b/packages/workspace/src/local/__tests__/crud.spec.ts
new file mode 100644
index 0000000000..0b7b3adbd9
--- /dev/null
+++ b/packages/workspace/src/local/__tests__/crud.spec.ts
@@ -0,0 +1,68 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import 'fake-indexeddb/auto';
+
+import type { LocalWorkspace, WorkspaceCRUD } from '@affine/workspace/type';
+import { WorkspaceFlavour } from '@affine/workspace/type';
+import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
+import { Workspace } from '@blocksuite/store';
+import { afterEach, assertType, describe, expect, test } from 'vitest';
+
+import { CRUD } from '../crud';
+
+afterEach(() => {
+ localStorage.clear();
+});
+
+describe('crud', () => {
+ test('type', () => {
+ assertType<WorkspaceCRUD<WorkspaceFlavour.LOCAL>>(CRUD);
+ });
+
+ test('basic', async () => {
+ const workspace = await CRUD.get('not_exist');
+ expect(workspace).toBeNull();
+
+ expect(await CRUD.list()).toEqual([]);
+ });
+
+ test('delete not exist', async () => {
+ expect(async () =>
+ CRUD.delete({
+ id: 'not_exist',
+ flavour: WorkspaceFlavour.LOCAL,
+ blockSuiteWorkspace: new Workspace({ id: 'test' }),
+ providers: [],
+ })
+ ).rejects.toThrowError();
+ });
+
+ test('create & delete', async () => {
+ const workspace = new Workspace({ id: 'test' })
+ .register(AffineSchemas)
+ .register(__unstableSchemas);
+ const page = workspace.createPage('test');
+ const pageBlockId = page.addBlock('affine:page', {
+ title: new page.Text(''),
+ });
+ page.addBlock('affine:surface', {}, null);
+ const frameId = page.addBlock('affine:frame', {}, pageBlockId);
+ page.addBlock('affine:paragraph', {}, frameId);
+
+ const id = await CRUD.create(workspace);
+ const list = await CRUD.list();
+ expect(list.length).toBe(1);
+ expect(list[0].id).toBe(id);
+ const localWorkspace = list.at(0) as LocalWorkspace;
+ expect(localWorkspace.id).toBe(id);
+ expect(localWorkspace.flavour).toBe(WorkspaceFlavour.LOCAL);
+ expect(
+ Workspace.Y.encodeStateAsUpdate(localWorkspace.blockSuiteWorkspace.doc)
+ ).toEqual(Workspace.Y.encodeStateAsUpdate(workspace.doc));
+
+ await CRUD.delete(localWorkspace);
+ expect(await CRUD.get(id)).toBeNull();
+ expect(await CRUD.list()).toEqual([]);
+ });
+});