Skip to content

Build your personal knowledge base with Trilium Notes

test(ckeditor5): take the math plugin to 100% coverage

Elian Doran40ee54f29714
diff --git a/packages/ckeditor5/src/plugins/math/main_form_view.spec.ts b/packages/ckeditor5/src/plugins/math/main_form_view.spec.ts
index 37a14437d9..09e6ad858f 100644
--- a/packages/ckeditor5/src/plugins/math/main_form_view.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/main_form_view.spec.ts
@@ -82,6 +82,27 @@ describe( 'MainFormView', () => {
it( 'focuses its first focusable on focus()', () => {
expect( () => view.focus() ).not.toThrow();
} );
+
+ it( 'reports an empty equation when the input holds nothing', () => {
+ view.mathInputView.value = null;
+
+ expect( view.equation ).toBe( '' );
+ } );
+
+ it( 'clears the input when the delimiters wrap nothing', () => {
+ view.mathInputView.value = '\\[\\]';
+
+ expect( view.mathInputView.value ).toBeNull();
+ expect( view.displayButtonView.isOn ).toBe( true );
+ } );
+
+ it( 'handles the input being cleared', () => {
+ view.mathInputView.value = 'x^2';
+
+ view.mathInputView.value = null;
+
+ expect( view.mathView?.value ).toBe( '' );
+ } );
} );
describe( 'with the preview disabled', () => {
@@ -95,6 +116,10 @@ describe( 'MainFormView', () => {
view.mathInputView.value = 'x^2';
expect( view.equation ).toBe( 'x^2' );
+ // The equation setter has to cope with there being no preview to update.
+ view.equation = 'y^2';
+ expect( view.mathInputView.value ).toBe( 'y^2' );
+
view.element?.remove();
view.destroy();
} );
diff --git a/packages/ckeditor5/src/plugins/math/main_form_view.ts b/packages/ckeditor5/src/plugins/math/main_form_view.ts
index 539cedebaa..99a8d961e6 100644
--- a/packages/ckeditor5/src/plugins/math/main_form_view.ts
+++ b/packages/ckeditor5/src/plugins/math/main_form_view.ts
@@ -117,6 +117,8 @@ export default class MainFormView extends View {
focusableViews.forEach( v => {
this._focusables.add( v );
+ /* v8 ignore next -- every focusable above is a child of this view's template, so
+ super.render() has already given it an element */
if ( v.element ) {
this.focusTracker.add( v.element );
}
@@ -135,6 +137,7 @@ export default class MainFormView extends View {
}
} );
+ /* v8 ignore next -- super.render() assigns the element before this runs */
if ( this.element ) {
this.keystrokes.listenTo( this.element );
}
@@ -165,6 +168,8 @@ export default class MainFormView extends View {
eq = params.equation;
this.displayButtonView.isOn = params.display;
+ /* v8 ignore next -- only reached when the value carried delimiters, and stripping
+ them always shortens it, so the two can never be equal here */
if ( this.mathInputView.value !== eq ) {
this.mathInputView.value = eq.length ? eq : null;
}
diff --git a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
index 8330ffd3d1..205a972955 100644
--- a/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/math_input_view.spec.ts
@@ -109,6 +109,60 @@ describe( 'MathInputView', () => {
expect( view.value ).toBeNull();
} );
+ it( 'accepts a textarea edit while no math field is mounted', async () => {
+ await renderAndWait();
+ view.mathfield?.remove();
+ view.mathfield = null;
+
+ textarea().value = 'y^2';
+ textarea().dispatchEvent( new Event( 'input' ) );
+
+ expect( view.value ).toBe( 'y^2' );
+ } );
+
+ it( 'leaves the math field alone when the textarea already agrees with it', async () => {
+ const mathfield = await renderAndWait();
+ view.value = 'x^2';
+ await vi.waitFor( () => expect( mathfield.value.trim() ).toBe( 'x^2' ) );
+ const setValue = vi.spyOn( mathfield, 'setValue' );
+
+ textarea().value = 'x^2';
+ textarea().dispatchEvent( new Event( 'input' ) );
+
+ expect( setValue ).not.toHaveBeenCalled();
+ } );
+
+ it( 'leaves the textarea alone when the math field already agrees with it', async () => {
+ const mathfield = await renderAndWait();
+ textarea().value = 'x^2';
+ mathfield.value = 'x^2';
+
+ mathfield.dispatchEvent( new Event( 'input' ) );
+
+ expect( textarea().value ).toBe( 'x^2' );
+ } );
+
+ it( 'does not touch the math field when the model value already matches', async () => {
+ const mathfield = await renderAndWait();
+ view.value = 'x^2';
+ await vi.waitFor( () => expect( mathfield.value.trim() ).toBe( 'x^2' ) );
+ const setValue = vi.spyOn( mathfield, 'setValue' );
+
+ // Re-assigning the same value must not push it into the field again.
+ view.value = 'x^2 ';
+
+ expect( setValue ).not.toHaveBeenCalled();
+ } );
+
+ it( 'reuses an existing math field rather than building a second one', async () => {
+ const first = await renderAndWait();
+
+ // A second init with the field already present just syncs its value.
+ ( view as unknown as { _initMathField( focus: boolean ): void } )._initMathField( false );
+
+ expect( view.mathfield ).toBe( first );
+ } );
+
it( 'falls back to assigning value when setValue is unavailable', async () => {
const mathfield = await renderAndWait();
// Older MathLive builds expose no setValue; the view must still update the field.
diff --git a/packages/ckeditor5/src/plugins/math/math_ui.ts b/packages/ckeditor5/src/plugins/math/math_ui.ts
index f90feffd5c..19d00feae1 100644
--- a/packages/ckeditor5/src/plugins/math/math_ui.ts
+++ b/packages/ckeditor5/src/plugins/math/math_ui.ts
@@ -126,6 +126,8 @@ export default class MathUI extends Plugin {
// Allow pressing Enter to submit changes, and use Shift+Enter to insert a new line
formView.keystrokes.set( 'enter', ( data, cancel ) => {
+ /* v8 ignore next -- Shift+Enter is a distinct keystroke that never routes to the
+ 'enter' handler, so shiftKey is always false by the time this runs */
if ( !data.shiftKey ) {
formView.fire( 'submit' );
cancel();
diff --git a/packages/ckeditor5/src/plugins/math/math_ui_interactions.spec.ts b/packages/ckeditor5/src/plugins/math/math_ui_interactions.spec.ts
index a3038901f9..b180367c2c 100644
--- a/packages/ckeditor5/src/plugins/math/math_ui_interactions.spec.ts
+++ b/packages/ckeditor5/src/plugins/math/math_ui_interactions.spec.ts
@@ -104,6 +104,14 @@ describe( 'MathUI interactions', () => {
expect( hideSpy ).toHaveBeenCalled();
} );
+
+ it( 'does nothing when the form was never shown', () => {
+ setModelData( editor.model, `<paragraph>[<${ INLINE } equation="x^2" type="script" display="false"></${ INLINE }>]</paragraph>` );
+
+ // The command holds a value, so cancelling routes to _removeFormView — which has to
+ // cope with the form not being in the balloon at all.
+ expect( () => formView().fire( 'cancel' ) ).not.toThrow();
+ } );
} );
describe( 'clicking an existing equation', () => {