-
Notifications
You must be signed in to change notification settings - Fork 18
refactor(text-editor): extract editor-config to enable real-stack testing #4139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
john-traas
wants to merge
9
commits into
main
Choose a base branch
from
text-editor-real-stack-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b4d9dc3
build: add clean script and prosemirror-test-builder dev dependency
john-traas d0aedb1
refactor(text-editor): extract schema and plugin assembly into editor…
john-traas 2b0ebeb
test(text-editor): add real-stack integration spec for editor-config
john-traas 141782c
test(text-editor): keep only load-bearing comments in the integration…
john-traas a367c56
test(text-editor): properly type the callback
john-traas 44cd26a
test(text-editor): assert plugins exist before testing on them
john-traas 99a679e
refactor(text-editor): reuse the ContentType type from editor-config
john-traas 97176d0
refactor(text-editor): tag ContentType as beta and update API report
john-traas ab4ae79
test(text-editor): access optional paste handlers with optional chaining
john-traas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/components/text-editor/prosemirror-adapter/editor-config.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { EditorState, TextSelection } from 'prosemirror-state'; | ||
| import { Slice } from 'prosemirror-model'; | ||
| import { EditorView } from 'prosemirror-view'; | ||
| import { | ||
| builders, | ||
| eq, | ||
| NodeBuilder, | ||
| MarkBuilder, | ||
| } from 'prosemirror-test-builder'; | ||
| import { | ||
| buildEditorSchema, | ||
| buildEditorPlugins, | ||
| EditorPluginsOptions, | ||
| } from './editor-config'; | ||
| import { MenuCommandFactory } from './menu/menu-commands'; | ||
| import { EditorMenuTypes } from './menu/types'; | ||
| import { ContentTypeConverter } from '../utils/content-type-converter'; | ||
| import { pluginKey as imageInserterPluginKey } from './plugins/image/inserter'; | ||
| import { linkPluginKey } from './plugins/link/link-plugin'; | ||
|
|
||
| /** | ||
| * Integration tests for the text editor's real stack: the production schema | ||
| * and the production *ordered* plugin list from `buildEditorSchema` / | ||
| * `buildEditorPlugins`. | ||
| * | ||
| * Transactions are applied at the state level (`state.apply`) rather than | ||
| * through an `EditorView`: state application runs every plugin's | ||
| * `filterTransaction`/`appendTransaction` (the cross-plugin integrity path) | ||
| * without needing a real DOM selection, which the spec environment lacks. | ||
| * View-driven behaviour (real key/paste events, scrolling, focus) is the | ||
| * domain of the e2e tests. | ||
| */ | ||
| describe('editor-config (real-stack integration)', () => { | ||
| const schema = buildEditorSchema({ | ||
| customElements: [], | ||
| contentType: 'html', | ||
| language: 'en', | ||
| }); | ||
| const factory = new MenuCommandFactory(schema); | ||
|
|
||
| // The trigger plugin takes a content converter but only invokes it on | ||
| // trigger events, which these tests never fire — a no-op keeps the setup | ||
| // hermetic. | ||
| const contentConverter: ContentTypeConverter = { | ||
| parseAsHTML: async () => '', | ||
| serialize: () => '', | ||
| }; | ||
| const noopImagePasted: EditorPluginsOptions['onImagePasted'] = (data) => | ||
| new CustomEvent('imagePasted', { detail: data }); | ||
|
|
||
| const plugins = buildEditorPlugins({ | ||
| schema: schema, | ||
| menuCommandFactory: factory, | ||
| contentConverter: contentConverter, | ||
| language: 'en', | ||
| contentType: 'html', | ||
| triggerCharacters: [], | ||
| onNewLinkSelection: () => undefined, | ||
| onImagePasted: noopImagePasted, | ||
| onActiveItemsChange: () => undefined, | ||
| }); | ||
|
|
||
| const builder = builders(schema, { p: { nodeType: 'paragraph' } }); | ||
| const doc = builder.doc as NodeBuilder; | ||
| const p = builder.p as NodeBuilder; | ||
| const strong = builder.strong as MarkBuilder; | ||
|
|
||
| describe('the real stack instantiates', () => { | ||
| it('builds the production schema (nodes + marks the editor uses)', () => { | ||
| expect(schema.nodes.image).toBeDefined(); | ||
| expect(schema.nodes.table).toBeDefined(); | ||
| expect(schema.nodes.bullet_list).toBeDefined(); | ||
| expect(schema.marks.strikethrough).toBeDefined(); | ||
| expect(schema.marks.link).toBeDefined(); | ||
| }); | ||
|
|
||
| it('assembles the full ordered plugin set', () => { | ||
| expect(Array.isArray(plugins)).toBe(true); | ||
| expect(plugins.length).toBeGreaterThan(8); | ||
|
|
||
| const state = EditorState.create({ doc: doc(p()), plugins }); | ||
| expect(state.plugins).toHaveLength(plugins.length); | ||
| }); | ||
| }); | ||
|
|
||
| describe('commands work against the real schema', () => { | ||
| it('the Bold command applies the strong mark to the selection', () => { | ||
| const startDoc = doc(p('<a>hello<b>')); | ||
| let state = EditorState.create({ | ||
| doc: startDoc, | ||
| plugins: plugins, | ||
| selection: TextSelection.create( | ||
| startDoc, | ||
| startDoc.tag.a, | ||
| startDoc.tag.b | ||
| ), | ||
| }); | ||
|
|
||
| const bold = factory.getCommand(EditorMenuTypes.Bold); | ||
| bold(state, (transaction) => { | ||
| state = state.apply(transaction); | ||
| }); | ||
|
|
||
| expect(eq(state.doc, doc(p(strong('hello'))))).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shared-event (paste) handler order', () => { | ||
| const linkPlugin = plugins.find( | ||
| (plugin) => plugin.spec.key === linkPluginKey | ||
| ); | ||
| const imagePlugin = plugins.find( | ||
| (plugin) => plugin.spec.key === imageInserterPluginKey | ||
| ); | ||
|
|
||
| it('both the link and image plugins register handlePaste, link first', () => { | ||
| expect(linkPlugin).toBeDefined(); | ||
| expect(imagePlugin).toBeDefined(); | ||
| expect(typeof linkPlugin?.props.handlePaste).toBe('function'); | ||
| expect(typeof imagePlugin?.props.handlePaste).toBe('function'); | ||
|
|
||
| // ProseMirror resolves handlePaste first-truthy-wins in plugin | ||
| // order, so the relative order of these two decides which claims a | ||
| // paste both could handle. | ||
| expect(plugins.indexOf(linkPlugin)).toBeLessThan( | ||
| plugins.indexOf(imagePlugin) | ||
| ); | ||
| }); | ||
|
|
||
| it('neither plugin claims a plain paste, so others still run', () => { | ||
| expect(linkPlugin).toBeDefined(); | ||
| expect(imagePlugin).toBeDefined(); | ||
|
|
||
| const view = {} as unknown as EditorView; | ||
| const plainPaste = { | ||
| clipboardData: { | ||
| getData: () => 'plain text without a link', | ||
| files: [], | ||
| }, | ||
| } as unknown as ClipboardEvent; | ||
|
|
||
| expect( | ||
| linkPlugin?.props.handlePaste?.(view, plainPaste, Slice.empty) | ||
| ).toBeFalsy(); | ||
| expect( | ||
| imagePlugin?.props.handlePaste?.(view, plainPaste, Slice.empty) | ||
| ).toBeFalsy(); | ||
| }); | ||
|
Copilot marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| describe('transactions stay consistent across the plugin set', () => { | ||
| it('keeps the document valid across a sequence of transactions', () => { | ||
| const startDoc = doc(p('<a>Hello<b>')); | ||
| let state = EditorState.create({ | ||
| doc: startDoc, | ||
| plugins: plugins, | ||
| selection: TextSelection.create( | ||
| startDoc, | ||
| startDoc.tag.a, | ||
| startDoc.tag.b | ||
| ), | ||
| }); | ||
|
|
||
| expect(() => { | ||
| const bold = factory.getCommand(EditorMenuTypes.Bold); | ||
| bold(state, (transaction) => { | ||
| state = state.apply(transaction); | ||
| }); | ||
|
|
||
| state = state.apply( | ||
| state.tr.setSelection(TextSelection.atEnd(state.doc)) | ||
| ); | ||
| state = state.apply(state.tr.insertText(' world')); | ||
|
|
||
| state.doc.check(); | ||
| }).not.toThrow(); | ||
|
|
||
| expect(state.doc.textContent).toBe('Hello world'); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't be a breaking change, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this should not be a breaking change. The accepted values and runtime behavior remain exactly the same:
'markdown' | 'html'. The only difference is that the inline union has been extracted into the sharedContentTypetype, giving us a single source of truth for the type definition.