test: improve coverage (#1780)

This commit is contained in:
Himself65 2023-04-02 02:16:53 -05:00 committed by GitHub
parent 60324b8967
commit f5574c68fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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([]);
});
});