refactor(editor): remove assertExists (#10615)

This commit is contained in:
Saul-Mirone
2025-03-05 00:13:08 +00:00
parent a6692f70aa
commit b8ecfbdae6
106 changed files with 863 additions and 517 deletions

View File

@@ -5,7 +5,6 @@ import {
LayoutType,
NoteDisplayMode,
} from '@blocksuite/blocks';
import { assertExists } from '@blocksuite/global/utils';
import { beforeEach, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
@@ -68,12 +67,16 @@ describe('group', () => {
const shapeId = service.crud.addElement('shape', {
shapeType: 'rect',
});
assertExists(shapeId);
if (!shapeId) {
throw new Error('shapeId is not found');
}
map.set(noteId, true);
map.set(shapeId, true);
const groupId = service.crud.addElement('group', { children: map });
assertExists(groupId);
if (!groupId) {
throw new Error('groupId is not found');
}
expect(service.elements.length).toBe(2);
expect(doc.getBlock(noteId)).toBeDefined();
doc.captureSync();
@@ -92,12 +95,16 @@ describe('group', () => {
shapeType: 'rect',
xywh: '[0,0,100,100]',
});
assertExists(shape1);
if (!shape1) {
throw new Error('shape1 is not found');
}
const shape2 = service.crud.addElement('shape', {
shapeType: 'rect',
xywh: '[100,100,100,100]',
});
assertExists(shape2);
if (!shape2) {
throw new Error('shape2 is not found');
}
const note1 = addNote(doc, {
displayMode: NoteDisplayMode.DocAndEdgeless,
xywh: '[200,200,800,100]',
@@ -119,7 +126,9 @@ describe('group', () => {
children.set(note1, true);
const groupId = service.crud.addElement('group', { children });
assertExists(groupId);
if (!groupId) {
throw new Error('groupId is not found');
}
const group = service.crud.getElementById(groupId) as GroupElementModel;
const assertInitial = () => {
@@ -189,7 +198,9 @@ describe('group', () => {
test('empty group should have all zero xywh', () => {
const map = new Y.Map<boolean>();
const groupId = service.crud.addElement('group', { children: map });
assertExists(groupId);
if (!groupId) {
throw new Error('groupId is not found');
}
const group = service.crud.getElementById(groupId) as GroupElementModel;
expect(group.x).toBe(0);
@@ -253,7 +264,9 @@ describe('mindmap', () => {
],
};
const mindmapId = service.crud.addElement('mindmap', { children: tree });
assertExists(mindmapId);
if (!mindmapId) {
throw new Error('mindmapId is not found');
}
const mindmap = () =>
service.crud.getElementById(mindmapId) as MindmapElementModel;
@@ -304,7 +317,9 @@ describe('mindmap', () => {
type: LayoutType.RIGHT,
children: tree,
});
assertExists(mindmapId);
if (!mindmapId) {
throw new Error('mindmapId is not found');
}
const mindmap = () =>
service.crud.getElementById(mindmapId) as MindmapElementModel;
@@ -349,7 +364,10 @@ describe('mindmap', () => {
type: LayoutType.RIGHT,
children: tree,
});
assertExists(mindmapId);
if (!mindmapId) {
throw new Error('mindmapId is not found');
}
const mindmap = () =>
service.crud.getElementById(mindmapId) as MindmapElementModel;

View File

@@ -19,7 +19,6 @@ import {
ShapeType,
type TextElementModel,
} from '@blocksuite/blocks';
import { assertExists } from '@blocksuite/global/utils';
import { beforeEach, describe, expect, test } from 'vitest';
import { getDocRootBlock } from '../utils/edgeless.js';
@@ -44,7 +43,9 @@ describe('apply last props', () => {
const rectId = service.crud.addElement('shape', {
shapeType: ShapeType.Rect,
});
assertExists(rectId);
if (!rectId) {
throw new Error('rectId is not found');
}
const rectShape = service.crud.getElementById(rectId) as ShapeElementModel;
expect(rectShape.fillColor).toBe(DefaultTheme.shapeFillColor);
service.crud.updateElement(rectId, {
@@ -59,7 +60,9 @@ describe('apply last props', () => {
const diamondId = service.crud.addElement('shape', {
shapeType: ShapeType.Diamond,
});
assertExists(diamondId);
if (!diamondId) {
throw new Error('diamondId is not found');
}
const diamondShape = service.crud.getElementById(
diamondId
) as ShapeElementModel;
@@ -77,7 +80,9 @@ describe('apply last props', () => {
shapeType: ShapeType.Rect,
radius: 0.1,
});
assertExists(roundedRectId);
if (!roundedRectId) {
throw new Error('roundedRectId is not found');
}
const roundedRectShape = service.crud.getElementById(
roundedRectId
) as ShapeElementModel;
@@ -95,7 +100,9 @@ describe('apply last props', () => {
const rectId2 = service.crud.addElement('shape', {
shapeType: ShapeType.Rect,
});
assertExists(rectId2);
if (!rectId2) {
throw new Error('rectId2 is not found');
}
const rectShape2 = service.crud.getElementById(
rectId2
) as ShapeElementModel;
@@ -104,7 +111,9 @@ describe('apply last props', () => {
const diamondId2 = service.crud.addElement('shape', {
shapeType: ShapeType.Diamond,
});
assertExists(diamondId2);
if (!diamondId2) {
throw new Error('diamondId2 is not found');
}
const diamondShape2 = service.crud.getElementById(
diamondId2
) as ShapeElementModel;
@@ -114,7 +123,9 @@ describe('apply last props', () => {
shapeType: ShapeType.Rect,
radius: 0.1,
});
assertExists(roundedRectId2);
if (!roundedRectId2) {
throw new Error('roundedRectId2 is not found');
}
const roundedRectShape2 = service.crud.getElementById(
roundedRectId2
) as ShapeElementModel;
@@ -125,7 +136,9 @@ describe('apply last props', () => {
test('connector', () => {
const id = service.crud.addElement('connector', { mode: 0 });
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const connector = service.crud.getElementById(id) as ConnectorElementModel;
expect(connector.stroke).toBe(DefaultTheme.connectorColor);
expect(connector.strokeWidth).toBe(2);
@@ -135,7 +148,9 @@ describe('apply last props', () => {
service.crud.updateElement(id, { strokeWidth: 10 });
const id2 = service.crud.addElement('connector', { mode: 1 });
assertExists(id2);
if (!id2) {
throw new Error('id2 is not found');
}
const connector2 = service.crud.getElementById(
id2
) as ConnectorElementModel;
@@ -148,7 +163,9 @@ describe('apply last props', () => {
});
const id3 = service.crud.addElement('connector', { mode: 1 });
assertExists(id3);
if (!id3) {
throw new Error('id3 is not found');
}
const connector3 = service.crud.getElementById(
id3
) as ConnectorElementModel;
@@ -159,7 +176,9 @@ describe('apply last props', () => {
test('brush', () => {
const id = service.crud.addElement('brush', {});
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const brush = service.crud.getElementById(id) as BrushElementModel;
expect(brush.color).toEqual(DefaultTheme.black);
expect(brush.lineWidth).toBe(4);
@@ -172,7 +191,9 @@ describe('apply last props', () => {
test('text', () => {
const id = service.crud.addElement('text', {});
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const text = service.crud.getElementById(id) as TextElementModel;
expect(text.fontSize).toBe(24);
service.crud.updateElement(id, { fontSize: 36 });
@@ -184,7 +205,9 @@ describe('apply last props', () => {
test('mindmap', () => {
const id = service.crud.addElement('mindmap', {});
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const mindmap = service.crud.getElementById(id) as MindmapElementModel;
expect(mindmap.layoutType).toBe(LayoutType.RIGHT);
expect(mindmap.style).toBe(MindmapStyle.ONE);
@@ -194,7 +217,9 @@ describe('apply last props', () => {
});
const id2 = service.crud.addElement('mindmap', {});
assertExists(id2);
if (!id2) {
throw new Error('id2 is not found');
}
const mindmap2 = service.crud.getElementById(id2) as MindmapElementModel;
expect(mindmap2.layoutType).toBe(LayoutType.BALANCE);
expect(mindmap2.style).toBe(MindmapStyle.THREE);
@@ -203,7 +228,9 @@ describe('apply last props', () => {
test('edgeless-text', () => {
const surface = getSurfaceBlock(doc);
const id = service.crud.addBlock('affine:edgeless-text', {}, surface!.id);
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const text = service.crud.getElementById(id) as EdgelessTextBlockModel;
expect(text.color).toBe(DefaultTheme.textColor);
expect(text.fontFamily).toBe(FontFamily.Inter);
@@ -213,7 +240,9 @@ describe('apply last props', () => {
});
const id2 = service.crud.addBlock('affine:edgeless-text', {}, surface!.id);
assertExists(id2);
if (!id2) {
throw new Error('id2 is not found');
}
const text2 = service.crud.getElementById(id2) as EdgelessTextBlockModel;
expect(text2.color).toBe(DefaultTheme.StrokeColorShortMap.Green);
expect(text2.fontFamily).toBe(FontFamily.OrelegaOne);
@@ -221,7 +250,9 @@ describe('apply last props', () => {
test('note', () => {
const id = service.crud.addBlock('affine:note', {}, doc.root!.id);
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const note = service.crud.getElementById(id) as NoteBlockModel;
expect(note.background).toEqual(DefaultTheme.noteBackgrounColor);
expect(note.edgeless.style.shadowType).toBe(DEFAULT_NOTE_SHADOW);
@@ -235,7 +266,9 @@ describe('apply last props', () => {
});
const id2 = service.crud.addBlock('affine:note', {}, doc.root!.id);
assertExists(id2);
if (!id2) {
throw new Error('id2 is not found');
}
const note2 = service.crud.getElementById(id2) as NoteBlockModel;
expect(note2.background).toEqual(
DefaultTheme.NoteBackgroundColorMap.Purple
@@ -246,7 +279,9 @@ describe('apply last props', () => {
test('frame', () => {
const surface = getSurfaceBlock(doc);
const id = service.crud.addBlock('affine:frame', {}, surface!.id);
assertExists(id);
if (!id) {
throw new Error('id is not found');
}
const note = service.crud.getElementById(id) as FrameBlockModel;
expect(note.background).toBe('transparent');
service.crud.updateElement(id, {
@@ -254,7 +289,9 @@ describe('apply last props', () => {
});
const id2 = service.crud.addBlock('affine:frame', {}, surface!.id);
assertExists(id2);
if (!id2) {
throw new Error('id2 is not found');
}
const frame2 = service.crud.getElementById(id2) as FrameBlockModel;
expect(frame2.background).toBe(DefaultTheme.StrokeColorShortMap.Purple);
service.crud.updateElement(id2, {
@@ -262,7 +299,9 @@ describe('apply last props', () => {
});
const id3 = service.crud.addBlock('affine:frame', {}, surface!.id);
assertExists(id3);
if (!id3) {
throw new Error('id3 is not found');
}
const frame3 = service.crud.getElementById(id3) as FrameBlockModel;
expect(frame3.background).toEqual({ normal: '#def4e740' });
service.crud.updateElement(id3, {
@@ -270,7 +309,9 @@ describe('apply last props', () => {
});
const id4 = service.crud.addBlock('affine:frame', {}, surface!.id);
assertExists(id4);
if (!id4) {
throw new Error('id4 is not found');
}
const frame4 = service.crud.getElementById(id4) as FrameBlockModel;
expect(frame4.background).toEqual({
light: '#a381aa23',

View File

@@ -19,7 +19,6 @@ import {
StoreExtensions,
} from '@blocksuite/blocks';
import { AffineSchemas } from '@blocksuite/blocks/schemas';
import { assertExists } from '@blocksuite/global/utils';
import { Schema, Text } from '@blocksuite/store';
import {
createAutoIncrementIdGenerator,
@@ -67,7 +66,9 @@ async function createEditor(
) {
const app = document.createElement('div');
const blockCollection = collection.docs.values().next().value;
assertExists(blockCollection, 'Need to create a doc first');
if (!blockCollection) {
throw new Error('Need to create a doc first');
}
const doc = blockCollection.getStore();
const editor = new TestAffineEditorContainer();
editor.doc = doc;