-
Notifications
You must be signed in to change notification settings - Fork 17
Create text editor test suite #4012
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
10
commits into
main
Choose a base branch
from
refine-text-editor-test-suite
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
10 commits
Select commit
Hold shift + click to select a range
db29119
feat(text-editor test suite): add schema builder
john-traas 1f1e38d
feat(text-editor test suite): add editor state builder
john-traas 644fe64
feat(text-editor test suite): add content generator
john-traas d591c48
feat(text-editor test suite): add editor view builder
john-traas ad6353e
feat(text-editor test suite): add command tester
john-traas f7994ed
feat(text-editor test suite): add event simulator utilities
john-traas 3cb418a
feat(text-editor test suite): add mock factories
john-traas 78583ef
chore(text-editor test suite): export all from test suite
john-traas 716adbb
docs(text editor test suite): add docs
john-traas 1c5745c
feat(text-editor): add tests for the table plugin
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
171 changes: 171 additions & 0 deletions
171
src/components/text-editor/prosemirror-adapter/plugins/table-plugin.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,171 @@ | ||
| import { Schema, NodeType } from 'prosemirror-model'; | ||
| import { Plugin } from 'prosemirror-state'; | ||
| import { | ||
| cleanupEditorView, | ||
| createCustomTestSchema, | ||
| createEditorState, | ||
| createEditorView, | ||
| } from '../../test-setup/editor-test-utils'; | ||
|
|
||
| import { | ||
| getTableEditingPlugins, | ||
| getTableNodes, | ||
| createStyleAttribute, | ||
| } from './table-plugin'; | ||
| import { EditorView } from 'prosemirror-view'; | ||
|
|
||
| describe('Table Plugin', () => { | ||
| let view: EditorView | null = null; | ||
| let container: HTMLElement | null = null; | ||
|
|
||
| afterEach(() => { | ||
| if (view) { | ||
| cleanupEditorView(view, container); | ||
| view = null; | ||
| container = null; | ||
| } | ||
| }); | ||
|
|
||
| describe('getTableEditingPlugins', () => { | ||
| it('should return an array with table editing plugin when tables are enabled', () => { | ||
| const plugins = getTableEditingPlugins(true); | ||
|
|
||
| expect(plugins).toBeInstanceOf(Array); | ||
| expect(plugins.length).toBe(1); | ||
| expect(plugins[0]).toBeInstanceOf(Plugin); | ||
| }); | ||
|
|
||
| it('should return an empty array when tables are disabled', () => { | ||
| const plugins = getTableEditingPlugins(false); | ||
|
|
||
| expect(plugins).toBeInstanceOf(Array); | ||
| expect(plugins.length).toBe(0); | ||
| }); | ||
|
|
||
| it('should create a working plugin that can be added to an editor state', () => { | ||
| const baseSchema = createCustomTestSchema({}); | ||
| const tableNodes = getTableNodes(); | ||
| let nodes = baseSchema.spec.nodes; | ||
|
|
||
| for (const [name, spec] of Object.entries(tableNodes)) { | ||
| nodes = nodes.addToEnd(name, spec); | ||
| } | ||
|
|
||
| const schema = new Schema({ | ||
| nodes: nodes, | ||
| marks: baseSchema.spec.marks, | ||
| }); | ||
|
john-traas marked this conversation as resolved.
|
||
|
|
||
| const content = '<p>Text with table support</p>'; | ||
| const plugins = getTableEditingPlugins(true); | ||
| const state = createEditorState(content, schema, plugins); | ||
|
|
||
| const result = createEditorView(state); | ||
| view = result.view; | ||
| container = result.container; | ||
|
|
||
| expect(plugins.length).toBe(1); | ||
| expect(view.state.plugins).toContain(plugins[0]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getTableNodes', () => { | ||
| it('should return an object with table node specs', () => { | ||
| const tableNodes = getTableNodes(); | ||
|
|
||
| expect(tableNodes).toBeDefined(); | ||
| expect(typeof tableNodes).toBe('object'); | ||
|
|
||
| expect(tableNodes.table).toBeDefined(); | ||
| expect(tableNodes.table_row).toBeDefined(); | ||
| expect(tableNodes.table_cell).toBeDefined(); | ||
| expect(tableNodes.table_header).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create node specs that can be added to a schema', () => { | ||
| const baseSchema = createCustomTestSchema({}); | ||
| const tableNodes = getTableNodes(); | ||
| let nodes = baseSchema.spec.nodes; | ||
|
|
||
| for (const [name, spec] of Object.entries(tableNodes)) { | ||
| nodes = nodes.addToEnd(name, spec); | ||
| } | ||
|
|
||
| const schema = new Schema({ | ||
| nodes: nodes, | ||
| marks: baseSchema.spec.marks, | ||
| }); | ||
|
|
||
| expect(schema.nodes.table instanceof NodeType).toBe(true); | ||
| expect(schema.nodes.table_row instanceof NodeType).toBe(true); | ||
| expect(schema.nodes.table_cell instanceof NodeType).toBe(true); | ||
| expect(schema.nodes.table_header instanceof NodeType).toBe(true); | ||
| }); | ||
|
|
||
| it('should include custom cell attributes for styling', () => { | ||
| const tableNodes = getTableNodes(); | ||
| const cellSpec = tableNodes.table_cell; | ||
|
|
||
| expect(tableNodes.table_cell).toBeDefined(); | ||
| expect(tableNodes.table_cell.attrs).toBeDefined(); | ||
| expect(tableNodes.table_cell.attrs.background).toBeDefined(); | ||
| expect(tableNodes.table_cell.attrs.color).toBeDefined(); | ||
|
|
||
| expect(cellSpec.attrs).toHaveProperty('background'); | ||
| expect(cellSpec.attrs).toHaveProperty('color'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Style attribute helper', () => { | ||
| it('should get style values from DOM elements', () => { | ||
| const backgroundAttr = createStyleAttribute('background-color'); | ||
|
|
||
| const element = document.createElement('td'); | ||
| element.style.backgroundColor = 'red'; | ||
|
|
||
| expect(backgroundAttr.getFromDOM(element)).toBe('red'); | ||
| }); | ||
|
|
||
| it('should set style values on attributes object', () => { | ||
| const backgroundAttr = createStyleAttribute('background-color'); | ||
| const attrs: Record<string, any> = {}; | ||
|
|
||
| backgroundAttr.setDOMAttr('blue', attrs); | ||
|
|
||
| expect(attrs.style).toBe('background-color: blue;'); | ||
| }); | ||
|
|
||
| it('should append to existing style attribute when setting multiple styles', () => { | ||
| const backgroundAttr = createStyleAttribute('background-color'); | ||
| const colorAttr = createStyleAttribute('color'); | ||
| const attrs: Record<string, any> = {}; | ||
|
|
||
| backgroundAttr.setDOMAttr('blue', attrs); | ||
| colorAttr.setDOMAttr('white', attrs); | ||
|
|
||
| expect(attrs.style).toBe('background-color: blue;color: white;'); | ||
| }); | ||
|
|
||
| it('should not modify the style attribute when value is falsy', () => { | ||
| const backgroundAttr = createStyleAttribute('background-color'); | ||
| const attrs: Record<string, any> = {}; | ||
|
|
||
| // Test with empty string | ||
| backgroundAttr.setDOMAttr('', attrs); | ||
| expect(attrs.style).toBeUndefined(); | ||
|
|
||
| // Test with null | ||
| backgroundAttr.setDOMAttr(null, attrs); | ||
| expect(attrs.style).toBeUndefined(); | ||
|
|
||
| // Test with undefined | ||
| backgroundAttr.setDOMAttr(undefined, attrs); | ||
| expect(attrs.style).toBeUndefined(); | ||
|
|
||
| // Test that existing style is preserved when falsy value is passed | ||
| attrs.style = 'color: red;'; | ||
| backgroundAttr.setDOMAttr('', attrs); | ||
| expect(attrs.style).toBe('color: red;'); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.