chore: remove createEmptyEditor legacy test misc (#10291)

This commit is contained in:
Saul-Mirone
2025-02-19 13:23:41 +00:00
parent 2b11941c0e
commit 091ba7bb51
7 changed files with 222 additions and 348 deletions

View File

@@ -1,23 +1,21 @@
import '../declare-test-window.js';
import type { EditorHost } from '@blocksuite/block-std';
import type {
BlockSuiteFlags,
DatabaseBlockModel,
ListType,
RichText,
} from '@blocksuite/blocks';
import type { Container } from '@blocksuite/global/di';
import { assertExists } from '@blocksuite/global/utils';
import type { InlineRange, InlineRootElement } from '@blocksuite/inline';
import type { AffineEditorContainer } from '@blocksuite/presets';
import type { BlockModel, ExtensionType } from '@blocksuite/store';
import type { BlockModel } from '@blocksuite/store';
import { uuidv4 } from '@blocksuite/store';
import type { ConsoleMessage, Locator, Page } from '@playwright/test';
import { expect } from '@playwright/test';
import lz from 'lz-string';
import { currentEditorIndex, multiEditor } from '../multiple-editor.js';
import { currentEditorIndex } from '../multiple-editor.js';
import {
pressArrowRight,
pressEnter,
@@ -29,12 +27,6 @@ import {
type,
} from './keyboard.js';
declare global {
interface WindowEventMap {
'blocksuite:doc-ready': CustomEvent<string>;
}
}
export const defaultPlaygroundURL = new URL(
`http://localhost:${process.env.CI ? 4173 : 5173}/`
);
@@ -55,186 +47,6 @@ export const getSelectionRect = async (page: Page): Promise<DOMRect> => {
return rect;
};
/**
* @example
* ```ts
* await initEmptyEditor(page, { enable_some_flag: true });
* ```
*/
async function initEmptyEditor({
page,
flags = {},
noInit = false,
multiEditor = false,
}: {
page: Page;
flags?: Partial<BlockSuiteFlags>;
noInit?: boolean;
multiEditor?: boolean;
}) {
await page.evaluate(
([flags, noInit, multiEditor]) => {
const { collection } = window;
async function waitForMountPageEditor(
doc: ReturnType<typeof collection.createDoc>
) {
doc.load();
if (!doc.root) {
await new Promise(resolve => doc.slots.rootAdded.once(resolve));
}
// add app root from https://github.com/toeverything/blocksuite/commit/947201981daa64c5ceeca5fd549460c34e2dabfa
const appRoot = document.querySelector('#app');
if (!appRoot) {
throw new Error('Cannot find app root element(#app).');
}
const createEditor = () => {
const editor = document.createElement('affine-editor-container');
for (const [key, value] of Object.entries(flags)) {
doc
.get(window.$blocksuite.blocks.FeatureFlagService)
.setFlag(key as keyof BlockSuiteFlags, value);
}
doc
.get(window.$blocksuite.blocks.FeatureFlagService)
.setFlag('enable_advanced_block_visibility', true);
editor.doc = doc;
editor.autofocus = true;
const defaultExtensions: ExtensionType[] = [
...window.$blocksuite.defaultExtensions(),
{
setup: (di: Container) => {
di.addImpl(window.$blocksuite.identifiers.ParseDocUrlService, {
parseDocUrl() {
return undefined;
},
});
},
},
{
setup: (di: Container) => {
di.override(
window.$blocksuite.identifiers.DocModeProvider,
// @ts-expect-error set mock service
window.$blocksuite.mockServices.mockDocModeService(
() => editor.mode,
(mode: 'page' | 'edgeless') => editor.switchEditor(mode)
)
);
},
},
];
editor.pageSpecs = [...editor.pageSpecs, ...defaultExtensions];
editor.edgelessSpecs = [
...editor.edgelessSpecs,
...defaultExtensions,
];
editor.std
.get(window.$blocksuite.identifiers.RefNodeSlotsProvider)
.docLinkClicked.on(({ pageId: docId }) => {
const newDoc = collection.getDoc(docId);
if (!newDoc) {
throw new Error(`Failed to jump to page ${docId}`);
}
editor.doc = newDoc;
});
appRoot.append(editor);
return editor;
};
const editor = createEditor();
if (multiEditor) createEditor();
editor.updateComplete
.then(() => {
const debugMenu = document.createElement('starter-debug-menu');
const docsPanel = document.createElement('docs-panel');
const framePanel = document.createElement('custom-frame-panel');
const outlinePanel = document.createElement('custom-outline-panel');
const outlineViewer = document.createElement(
'custom-outline-viewer'
);
const leftSidePanel = document.createElement('left-side-panel');
// @ts-expect-error set test editor
docsPanel.editor = editor;
// @ts-expect-error set test editor
framePanel.editor = editor;
// @ts-expect-error set test editor
outlinePanel.editor = editor;
// @ts-expect-error set test editor
outlineViewer.editor = editor;
// @ts-expect-error set test collection
debugMenu.collection = collection;
// @ts-expect-error set test editor
debugMenu.editor = editor;
// @ts-expect-error set test docsPanel
debugMenu.docsPanel = docsPanel;
// @ts-expect-error set test framePanel
debugMenu.framePanel = framePanel;
// @ts-expect-error set test outlineViewer
debugMenu.outlineViewer = outlineViewer;
// @ts-expect-error set test outlinePanel
debugMenu.outlinePanel = outlinePanel;
// @ts-expect-error set test leftSidePanel
debugMenu.leftSidePanel = leftSidePanel;
document.body.append(debugMenu);
document.body.append(leftSidePanel);
document.body.append(framePanel);
document.body.append(outlinePanel);
document.body.append(outlineViewer);
window.debugMenu = debugMenu;
window.editor = editor;
window.doc = doc;
Object.defineProperty(globalThis, 'host', {
get() {
return document.querySelector<EditorHost>('editor-host');
},
});
Object.defineProperty(globalThis, 'std', {
get() {
return document.querySelector<EditorHost>('editor-host')?.std;
},
});
window.dispatchEvent(
new CustomEvent('blocksuite:doc-ready', { detail: doc.id })
);
})
.catch(console.error);
}
if (noInit) {
const firstDoc = collection.docs.values().next().value?.getStore() as
| ReturnType<typeof collection.createDoc>
| undefined;
if (firstDoc) {
window.doc = firstDoc;
waitForMountPageEditor(firstDoc).catch;
} else {
collection.slots.docCreated.on(docId => {
const doc = collection.getDoc(docId);
if (!doc) {
throw new Error(`Failed to get doc ${docId}`);
}
window.doc = doc;
waitForMountPageEditor(doc).catch(console.error);
});
}
} else {
collection.meta.initialize();
const doc = collection.createDoc({ id: 'doc:home' });
window.doc = doc;
waitForMountPageEditor(doc).catch(console.error);
}
},
[flags, noInit, multiEditor] as const
);
await waitNextFrame(page);
}
export const getEditorLocator = (page: Page) => {
return page.locator('affine-editor-container').nth(currentEditorIndex);
};
@@ -316,6 +128,14 @@ export async function enterPlaygroundRoom(
}
url.searchParams.set('room', room);
url.searchParams.set('blobSource', blobSource?.join(',') || 'idb');
for (const [key, value] of Object.entries(ops?.flags || {})) {
if (value) {
url.searchParams.append('flag', key);
}
}
if (ops?.noInit) {
url.searchParams.set('noInit', 'true');
}
await page.goto(url.toString());
// See https://github.com/microsoft/playwright/issues/5546
@@ -353,13 +173,6 @@ export async function enterPlaygroundRoom(
throw new Error(`Uncaught exception: "${exception}"\n${exception.stack}`);
});
await initEmptyEditor({
page,
flags: ops?.flags,
noInit: ops?.noInit,
multiEditor,
});
const locator = page.locator('affine-editor-container');
await locator.isVisible();
await page.evaluate(async () => {
@@ -419,7 +232,6 @@ export async function enterPlaygroundWithList(
) {
const room = generateRandomRoomId();
await page.goto(`${DEFAULT_PLAYGROUND}?room=${room}`);
await initEmptyEditor({ page });
await page.evaluate(
({ contents, type }: { contents: string[]; type: ListType }) => {
@@ -1439,7 +1251,7 @@ export async function mockParseDocUrlService(
) {
await page.evaluate(mapping => {
const parseDocUrlService = window.host.std.get(
window.$blocksuite.identifiers.ParseDocUrlService
window.$blocksuite.blocks.ParseDocUrlProvider
);
parseDocUrlService.parseDocUrl = (url: string) => {
const docId = mapping[url];

View File

@@ -52,7 +52,6 @@ import {
inlineEditorInnerTextToString,
} from './actions/misc.js';
import { getStringFromRichText } from './inline-editor.js';
import { currentEditorIndex } from './multiple-editor.js';
export { assertExists };
@@ -170,15 +169,14 @@ export async function assertRichTextInlineDeltas(
i = 0
) {
const actual = await page.evaluate(
([i, currentEditorIndex]) => {
const editorHost =
document.querySelectorAll('editor-host')[currentEditorIndex];
const inlineRoot = editorHost.querySelectorAll<InlineRootElement>(
([i]) => {
const editorHost = document.querySelector('editor-host');
const inlineRoot = editorHost?.querySelectorAll<InlineRootElement>(
'rich-text [data-v-root="true"]'
)[i];
return inlineRoot.inlineEditor.yTextDeltas;
return inlineRoot?.inlineEditor.yTextDeltas;
},
[i, currentEditorIndex]
[i]
);
expect(actual).toEqual(deltas);
}
@@ -194,9 +192,8 @@ export async function assertTextContain(page: Page, text: string, i = 0) {
}
export async function assertRichTexts(page: Page, texts: string[]) {
const actualTexts = await page.evaluate(currentEditorIndex => {
const editorHost =
document.querySelectorAll('editor-host')[currentEditorIndex];
const actualTexts = await page.evaluate(() => {
const editorHost = document.querySelector('editor-host');
const richTexts = Array.from(
editorHost?.querySelectorAll<RichText>('rich-text') ?? []
);
@@ -204,7 +201,7 @@ export async function assertRichTexts(page: Page, texts: string[]) {
const editor = richText.inlineEditor as AffineInlineEditor;
return editor.yText.toString();
});
}, currentEditorIndex);
});
expect(actualTexts).toEqual(texts);
}
@@ -318,14 +315,13 @@ export async function assertRichTextInlineRange(
rangeLength = 0
) {
const actual = await page.evaluate(
([richTextIndex, currentEditorIndex]) => {
const editorHost =
document.querySelectorAll('editor-host')[currentEditorIndex];
([richTextIndex]) => {
const editorHost = document.querySelector('editor-host');
const richText = editorHost?.querySelectorAll('rich-text')[richTextIndex];
const inlineEditor = richText.inlineEditor;
const inlineEditor = richText?.inlineEditor;
return inlineEditor?.getInlineRange();
},
[richTextIndex, currentEditorIndex]
[richTextIndex]
);
expect(actual).toEqual({ index: rangeIndex, length: rangeLength });
}
@@ -365,11 +361,10 @@ export async function assertTextFormat(
resultObj: unknown
) {
const actual = await page.evaluate(
({ richTextIndex, index, currentEditorIndex }) => {
const editorHost =
document.querySelectorAll('editor-host')[currentEditorIndex];
const richText = editorHost.querySelectorAll('rich-text')[richTextIndex];
const inlineEditor = richText.inlineEditor;
({ richTextIndex, index }) => {
const editorHost = document.querySelector('editor-host');
const richText = editorHost?.querySelectorAll('rich-text')[richTextIndex];
const inlineEditor = richText?.inlineEditor;
if (!inlineEditor) {
throw new Error('Inline editor is undefined');
}
@@ -380,7 +375,7 @@ export async function assertTextFormat(
});
return result;
},
{ richTextIndex, index, currentEditorIndex }
{ richTextIndex, index }
);
expect(actual).toEqual(resultObj);
}
@@ -391,26 +386,25 @@ export async function assertRichTextModelType(
index = 0
) {
const actual = await page.evaluate(
({ index, BLOCK_ID_ATTR, currentEditorIndex }) => {
const editorHost =
document.querySelectorAll('editor-host')[currentEditorIndex];
const richText = editorHost.querySelectorAll('rich-text')[index];
const block = richText.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
({ index, BLOCK_ID_ATTR }) => {
const editorHost = document.querySelector('editor-host');
const richText = editorHost?.querySelectorAll('rich-text')[index];
const block = richText?.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
if (!block) {
throw new Error('block component is undefined');
}
return (block.model as BlockModel<{ type: string }>).type;
},
{ index, BLOCK_ID_ATTR, currentEditorIndex }
{ index, BLOCK_ID_ATTR }
);
expect(actual).toEqual(type);
}
export async function assertTextFormats(page: Page, resultObj: unknown[]) {
const actual = await page.evaluate(index => {
const editorHost = document.querySelectorAll('editor-host')[index];
const elements = editorHost.querySelectorAll('rich-text');
const actual = await page.evaluate(() => {
const editorHost = document.querySelector('editor-host');
const elements = editorHost?.querySelectorAll('rich-text') ?? [];
return Array.from(elements).map(el => {
const inlineEditor = el.inlineEditor;
if (!inlineEditor) {
@@ -423,7 +417,7 @@ export async function assertTextFormats(page: Page, resultObj: unknown[]) {
});
return result;
});
}, currentEditorIndex);
});
expect(actual).toEqual(resultObj);
}
@@ -620,16 +614,16 @@ export async function assertBlockProps(
}
export async function assertBlockTypes(page: Page, blockTypes: string[]) {
const actual = await page.evaluate(index => {
const editor = document.querySelectorAll('affine-editor-container')[index];
const elements = editor?.querySelectorAll('[data-block-id]');
const actual = await page.evaluate(() => {
const editor = document.querySelector('affine-editor-container');
const elements = editor?.querySelectorAll('[data-block-id]') ?? [];
return (
Array.from(elements)
.slice(2)
// @ts-ignore
.map(el => el.model.type)
);
}, currentEditorIndex);
});
expect(actual).toEqual(blockTypes);
}

View File

@@ -1,20 +1,6 @@
import type {
DocModeProvider,
DocModeService,
ParseDocUrlProvider,
QuickSearchProvider,
ThemeProvider,
} from '@blocksuite/affine-shared/services';
import type { EditorHost } from '@blocksuite/block-std';
import type { RefNodeSlotsProvider } from '@blocksuite/blocks';
import type { AffineEditorContainer } from '@blocksuite/presets';
import type {
BlockModel,
ExtensionType,
Store,
Transformer,
Workspace,
} from '@blocksuite/store';
import type { Store, Transformer, Workspace } from '@blocksuite/store';
declare global {
interface Window {
@@ -29,22 +15,9 @@ declare global {
};
editor: typeof import('@blocksuite/presets');
blockStd: typeof import('@blocksuite/block-std');
identifiers: {
QuickSearchProvider: typeof QuickSearchProvider;
DocModeProvider: typeof DocModeProvider;
ThemeProvider: typeof ThemeProvider;
RefNodeSlotsProvider: typeof RefNodeSlotsProvider;
ParseDocUrlService: typeof ParseDocUrlProvider;
};
defaultExtensions: () => ExtensionType[];
mockServices: {
mockDocModeService: typeof DocModeService;
};
};
collection: Workspace;
blockSchema: Record<string, typeof BlockModel>;
doc: Store;
debugMenu: HTMLElement;
editor: AffineEditorContainer;
host: EditorHost;
job: Transformer;

View File

@@ -3,13 +3,9 @@ import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { expect, type Page, test as baseTest } from '@playwright/test';
import { test as baseTest } from '@playwright/test';
import {
enterPlaygroundRoom,
initEmptyParagraphState,
} from './actions/misc.js';
import { currentEditorIndex, scope } from './multiple-editor.js';
import { scope } from './multiple-editor.js';
const istanbulTempDir = process.env.ISTANBUL_TEMP_DIR
? path.resolve(process.env.ISTANBUL_TEMP_DIR)
@@ -59,52 +55,3 @@ export const test = baseTest.extend<{}>({
}
},
});
if (scope) {
test.beforeEach(async ({ browser }, testInfo) => {
if (!testInfo.title.startsWith(scope!)) {
testInfo.fn = () => {
testInfo.skip();
};
testInfo.skip();
await browser.close();
}
});
let page: Page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
});
// oxlint-disable-next-line no-empty-pattern
test.afterAll(async ({}, testInfo) => {
if (!testInfo.title.startsWith(scope!)) {
return;
}
const focusInSecondEditor = await page.evaluate(
([currentEditorIndex]) => {
const editor = document.querySelectorAll('affine-editor-container')[
currentEditorIndex
];
const selection = getSelection();
if (!selection || selection.rangeCount === 0) {
return true;
}
// once the range exists, it must be in the corresponding editor
return editor.contains(selection.getRangeAt(0).startContainer);
},
[currentEditorIndex]
);
expect(focusInSecondEditor).toBe(true);
});
test('ensure enable two editor', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
const count = await page.evaluate(() => {
return document.querySelectorAll('affine-editor-container').length;
});
expect(count).toBe(2);
});
}