Skip to content

Commit

Permalink
chore(editor): move legacy doc collection to test workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Jan 3, 2025
1 parent cfd64f1 commit c108f53
Show file tree
Hide file tree
Showing 25 changed files with 124 additions and 165 deletions.
10 changes: 7 additions & 3 deletions blocksuite/blocks/src/__tests__/database/database.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
} from '@blocksuite/affine-model';
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
import type { BlockModel, Doc } from '@blocksuite/store';
import { DocCollection, IdGeneratorType, Schema } from '@blocksuite/store';
import { Schema } from '@blocksuite/store';
import {
createAutoIncrementIdGenerator,
TestWorkspace,
} from '@blocksuite/store/test';
import { beforeEach, describe, expect, test } from 'vitest';

const AffineSchemas = [
Expand All @@ -29,15 +33,15 @@ const AffineSchemas = [
];

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register(AffineSchemas);
return { id: 'test-collection', idGenerator, schema };
}

function createTestDoc(docId = 'doc0') {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: docId });
doc.load();
Expand Down
10 changes: 3 additions & 7 deletions blocksuite/blocks/src/__tests__/utils/create-job.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
DocCollection,
Job,
type JobMiddleware,
Schema,
} from '@blocksuite/store';
import { Job, type JobMiddleware, Schema } from '@blocksuite/store';
import { TestWorkspace } from '@blocksuite/store/test';

import { defaultImageProxyMiddleware } from '../../_common/transformers/middlewares.js';
import { AffineSchemas } from '../../schemas.js';
Expand All @@ -25,7 +21,7 @@ export function createJob(middlewares?: JobMiddleware[]) {
const testMiddlewares = middlewares ?? [];
testMiddlewares.push(defaultImageProxyMiddleware);
const schema = new Schema().register(AffineSchemas);
const docCollection = new DocCollection({ schema });
const docCollection = new TestWorkspace({ schema });
docCollection.meta.initialize();
return new Job({
schema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { DocCollection, IdGeneratorType, Schema } from '@blocksuite/store';
import { Schema } from '@blocksuite/store';
import {
createAutoIncrementIdGenerator,
TestWorkspace,
} from '@blocksuite/store/test';
import { describe, expect, test } from 'vitest';

import { effects } from '../effects.js';
Expand All @@ -14,7 +18,7 @@ import { testSpecs } from './test-spec.js';
effects();

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register([RootBlockSchema, NoteBlockSchema, HeadingBlockSchema]);
return { id: 'test-collection', idGenerator, schema };
Expand All @@ -26,7 +30,7 @@ function wait(time: number) {

describe('editor host', () => {
test('editor host should rerender model when view changes', async () => {
const collection = new DocCollection(createTestOptions());
const collection = new TestWorkspace(createTestOptions());

collection.meta.initialize();
const doc = collection.createDoc({ id: 'home' });
Expand Down
3 changes: 2 additions & 1 deletion blocksuite/framework/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@types/lodash.merge": "^4.6.9"
},
"exports": {
".": "./src/index.ts"
".": "./src/index.ts",
"./test": "./src/test/index.ts"
},
"files": [
"src",
Expand Down
7 changes: 4 additions & 3 deletions blocksuite/framework/store/src/__tests__/block.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
type SchemaToModel,
} from '../schema/index.js';
import { Block, type YBlock } from '../store/doc/block/index.js';
import { DocCollection, IdGeneratorType } from '../store/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';

const pageSchema = defineBlockSchema({
flavour: 'page',
Expand All @@ -28,7 +29,7 @@ const pageSchema = defineBlockSchema({
type RootModel = SchemaToModel<typeof pageSchema>;

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register([pageSchema]);
return { id: 'test-collection', idGenerator, schema };
Expand All @@ -37,7 +38,7 @@ function createTestOptions() {
const defaultDocId = 'doc:home';
function createTestDoc(docId = defaultDocId) {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: docId });
doc.load();
Expand Down
28 changes: 15 additions & 13 deletions blocksuite/framework/store/src/__tests__/collection.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';

import { COLLECTION_VERSION, PAGE_VERSION } from '../consts.js';
import type { BlockModel, BlockSchemaType, Doc } from '../index.js';
import { DocCollection, IdGeneratorType, Schema } from '../index.js';
import { Schema } from '../index.js';
import type { DocMeta } from '../store/workspace.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import type { BlockSuiteDoc } from '../yjs/index.js';
import {
NoteBlockSchema,
Expand All @@ -23,7 +25,7 @@ export const BlockSchemas = [
] as BlockSchemaType[];

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register(BlockSchemas);
return { id: 'test-collection', idGenerator, schema };
Expand Down Expand Up @@ -60,7 +62,7 @@ function createRoot(doc: Doc) {

function createTestDoc(docId = defaultDocId) {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: docId });
doc.load();
Expand Down Expand Up @@ -92,7 +94,7 @@ beforeEach(() => {
describe('basic', () => {
it('can init collection', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc = collection.createDoc({ id: 'doc:home' });
Expand Down Expand Up @@ -132,7 +134,7 @@ describe('basic', () => {
it('init collection with custom id generator', () => {
const options = createTestOptions();
let id = 100;
const collection = new DocCollection({
const collection = new TestWorkspace({
...options,
idGenerator: () => {
return String(id++);
Expand All @@ -151,7 +153,7 @@ describe('basic', () => {

it('doc ready lifecycle', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({
id: 'space:0',
Expand All @@ -176,9 +178,9 @@ describe('basic', () => {

it('collection docs with yjs applyUpdate', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const collection2 = new DocCollection(options);
const collection2 = new TestWorkspace(options);
const doc = collection.createDoc({
id: 'space:0',
});
Expand Down Expand Up @@ -378,7 +380,7 @@ describe('addBlock', () => {

it('can add and remove multi docs', async () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc0 = collection.createDoc({ id: 'doc:home' });
Expand All @@ -403,7 +405,7 @@ describe('addBlock', () => {

it('can remove doc that has not been loaded', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc0 = collection.createDoc({ id: 'doc:home' });
Expand All @@ -414,7 +416,7 @@ describe('addBlock', () => {

it('can set doc state', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
collection.createDoc({ id: 'doc:home' });

Expand Down Expand Up @@ -456,7 +458,7 @@ describe('addBlock', () => {

it('can set collection common meta fields', async () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);

queueMicrotask(() => collection.meta.setName('hello'));
await waitOnce(collection.meta.commonFieldsUpdated);
Expand Down Expand Up @@ -863,7 +865,7 @@ describe('getBlock', () => {
describe('flags', () => {
it('update flags', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const awareness = collection.awarenessStore;
Expand Down
20 changes: 9 additions & 11 deletions blocksuite/framework/store/src/__tests__/doc.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { expect, test, vi } from 'vitest';
import * as Y from 'yjs';

import { Schema } from '../schema/index.js';
import {
BlockViewType,
DocCollection,
IdGeneratorType,
} from '../store/index.js';
import { BlockViewType } from '../store/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import {
DividerBlockSchema,
ListBlockSchema,
Expand All @@ -25,15 +23,15 @@ const BlockSchemas = [
];

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register(BlockSchemas);
return { id: 'test-collection', idGenerator, schema };
}

test('trigger props updated', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc = collection.createDoc({ id: 'home' });
Expand Down Expand Up @@ -93,7 +91,7 @@ test('trigger props updated', () => {

test('stash and pop', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc = collection.createDoc({ id: 'home' });
Expand Down Expand Up @@ -163,7 +161,7 @@ test('stash and pop', () => {

test('always get latest value in onChange', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();

const doc = collection.createDoc({ id: 'home' });
Expand Down Expand Up @@ -210,7 +208,7 @@ test('always get latest value in onChange', () => {

test('query', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc1 = collection.createDoc({ id: 'home' });
doc1.load();
Expand Down Expand Up @@ -247,7 +245,7 @@ test('query', () => {

test('local readonly', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc1 = collection.createDoc({ id: 'home' });
doc1.load();
Expand Down
7 changes: 4 additions & 3 deletions blocksuite/framework/store/src/__tests__/schema.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { describe, expect, it, vi } from 'vitest';
import { type BlockModel, defineBlockSchema } from '../schema/base.js';
import { SchemaValidateError } from '../schema/error.js';
import { Schema } from '../schema/index.js';
import { DocCollection, IdGeneratorType } from '../store/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';
import {
DividerBlockSchema,
ListBlockSchema,
Expand All @@ -15,7 +16,7 @@ import {
} from './test-schema.js';

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register(BlockSchemas);
return { id: 'test-collection', idGenerator, schema };
Expand Down Expand Up @@ -60,7 +61,7 @@ const BlockSchemas = [
const defaultDocId = 'doc0';
function createTestDoc(docId = defaultDocId) {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: docId });
doc.load();
Expand Down
4 changes: 2 additions & 2 deletions blocksuite/framework/store/src/__tests__/test-utils-dom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { DocCollection } from '../store/index.js';
import type { TestWorkspace } from '../test';

declare global {
interface WindowEventMap {
'test-result': CustomEvent<TestResult>;
}
interface Window {
collection: DocCollection;
collection: TestWorkspace;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
Schema,
type SchemaToModel,
} from '../schema/index.js';
import { DocCollection, IdGeneratorType } from '../store/index.js';
import { TestWorkspace } from '../test/test-workspace.js';
import { AssetsManager, BaseBlockTransformer } from '../transformer/index.js';
import { createAutoIncrementIdGenerator } from '../utils/id-generator.js';

const docSchema = defineBlockSchema({
flavour: 'page',
Expand Down Expand Up @@ -44,7 +45,7 @@ const docSchema = defineBlockSchema({
type RootBlockModel = SchemaToModel<typeof docSchema>;

function createTestOptions() {
const idGenerator = IdGeneratorType.AutoIncrement;
const idGenerator = createAutoIncrementIdGenerator();
const schema = new Schema();
schema.register([docSchema]);
return { id: 'test-collection', idGenerator, schema };
Expand All @@ -56,7 +57,7 @@ const assets = new AssetsManager({ blob: blobCRUD });

test('model to snapshot', () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: 'home' });
doc.load();
Expand All @@ -73,7 +74,7 @@ test('model to snapshot', () => {

test('snapshot to model', async () => {
const options = createTestOptions();
const collection = new DocCollection(options);
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: 'home' });
doc.load();
Expand Down
Loading

0 comments on commit c108f53

Please sign in to comment.