refactor(ckeditor5-math): move tests from sinon to vitest

This commit is contained in:
Elian Doran 2025-05-09 23:56:09 +03:00
parent 382d16ec68
commit da3c7d2541
No known key found for this signature in database
4 changed files with 64 additions and 70 deletions

View File

@ -2,8 +2,7 @@ import Mathematics from '../src/math.js';
import AutoMath from '../src/automath.js'; import AutoMath from '../src/automath.js';
import { ClassicEditor, Clipboard, Paragraph, Undo, Typing, _getModelData as getData, _setModelData as setData } from 'ckeditor5'; import { ClassicEditor, Clipboard, Paragraph, Undo, Typing, _getModelData as getData, _setModelData as setData } from 'ckeditor5';
import { expect } from 'chai'; import { expect } from 'chai';
import type { SinonFakeTimers } from 'sinon'; import { describe, beforeEach, it, afterEach, vi, VitestUtils } from "vitest";
import { describe, beforeEach, it, afterEach } from "vitest";
describe( 'AutoMath - integration', () => { describe( 'AutoMath - integration', () => {
let editorElement: HTMLDivElement, editor: ClassicEditor; let editorElement: HTMLDivElement, editor: ClassicEditor;
@ -50,14 +49,12 @@ describe( 'AutoMath - integration', () => {
} ); } );
describe( 'use fake timers', () => { describe( 'use fake timers', () => {
let clock: SinonFakeTimers;
beforeEach( () => { beforeEach( () => {
clock = sinon.useFakeTimers(); vi.useFakeTimers();
} ); } );
afterEach( () => { afterEach( () => {
clock.restore(); vi.useRealTimers();
} ); } );
it( 'replaces pasted text with mathtex element after 100ms', () => { it( 'replaces pasted text with mathtex element after 100ms', () => {
@ -68,7 +65,7 @@ describe( 'AutoMath - integration', () => {
'<paragraph>\\[x^2\\][]</paragraph>' '<paragraph>\\[x^2\\][]</paragraph>'
); );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>[<mathtex display="true" equation="x^2" type="script"></mathtex>]</paragraph>' '<paragraph>[<mathtex display="true" equation="x^2" type="script"></mathtex>]</paragraph>'
@ -83,7 +80,7 @@ describe( 'AutoMath - integration', () => {
'<paragraph>\\(x^2\\)[]</paragraph>' '<paragraph>\\(x^2\\)[]</paragraph>'
); );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>[<mathtex display="false" equation="x^2" type="script"></mathtex>]</paragraph>' '<paragraph>[<mathtex display="false" equation="x^2" type="script"></mathtex>]</paragraph>'
@ -98,7 +95,7 @@ describe( 'AutoMath - integration', () => {
'<paragraph>\\[x^2\\][]</paragraph>' '<paragraph>\\[x^2\\][]</paragraph>'
); );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
editor.commands.execute( 'undo' ); editor.commands.execute( 'undo' );
@ -111,7 +108,7 @@ describe( 'AutoMath - integration', () => {
setData( editor.model, '<paragraph>[Foo]</paragraph>' ); setData( editor.model, '<paragraph>[Foo]</paragraph>' );
pasteHtml( editor, '\\[x^2\\]' ); pasteHtml( editor, '\\[x^2\\]' );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>[<mathtex display="true" equation="x^2" type="script"></mathtex>]</paragraph>' '<paragraph>[<mathtex display="true" equation="x^2" type="script"></mathtex>]</paragraph>'
@ -122,7 +119,7 @@ describe( 'AutoMath - integration', () => {
setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' ); setData( editor.model, '<paragraph>Fo[o</paragraph><paragraph>Ba]r</paragraph>' );
pasteHtml( editor, '\\[x^2\\]' ); pasteHtml( editor, '\\[x^2\\]' );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>Fo[<mathtex display="true" equation="x^2" type="script"></mathtex>]r</paragraph>' '<paragraph>Fo[<mathtex display="true" equation="x^2" type="script"></mathtex>]r</paragraph>'
@ -133,7 +130,7 @@ describe( 'AutoMath - integration', () => {
setData( editor.model, '<paragraph>Foo []Bar</paragraph>' ); setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
pasteHtml( editor, '\\[x^2\\]' ); pasteHtml( editor, '\\[x^2\\]' );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>Foo ' + '<paragraph>Foo ' +
@ -146,7 +143,7 @@ describe( 'AutoMath - integration', () => {
setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' ); setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' );
pasteHtml( editor, '\\[x^2\\]' ); pasteHtml( editor, '\\[x^2\\]' );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>Foo ' + '<paragraph>Foo ' +
@ -159,7 +156,7 @@ describe( 'AutoMath - integration', () => {
setData( editor.model, '<paragraph>[]</paragraph>' ); setData( editor.model, '<paragraph>[]</paragraph>' );
pasteHtml( editor, '\\[x^2\\] \\[\\sqrt{x}2\\]' ); pasteHtml( editor, '\\[x^2\\] \\[\\sqrt{x}2\\]' );
clock.tick( 100 ); vi.advanceTimersByTime( 100 );
expect( getData( editor.model ) ).to.equal( expect( getData( editor.model ) ).to.equal(
'<paragraph>\\[x^2\\] \\[\\sqrt{x}2\\][]</paragraph>' '<paragraph>\\[x^2\\] \\[\\sqrt{x}2\\][]</paragraph>'

View File

@ -2,6 +2,7 @@ import { Math as MathDll, AutoformatMath as AutoformatMathDll } from '../src';
import Math from '../src/math'; import Math from '../src/math';
import AutoformatMath from '../src/autoformatmath'; import AutoformatMath from '../src/autoformatmath';
import { expect } from 'chai'; import { expect } from 'chai';
import { describe, it } from 'vitest';
describe( 'CKEditor5 Math DLL', () => { describe( 'CKEditor5 Math DLL', () => {
it( 'exports Math', () => { it( 'exports Math', () => {

View File

@ -5,9 +5,7 @@ import MathUI from '../src/mathui';
import MainFormView from '../src/ui/mainformview'; import MainFormView from '../src/ui/mainformview';
import { ClassicEditor, ContextualBalloon, ButtonView, View, Paragraph, ClickObserver, keyCodes, _setModelData as setModelData } from 'ckeditor5'; import { ClassicEditor, ContextualBalloon, ButtonView, View, Paragraph, ClickObserver, keyCodes, _setModelData as setModelData } from 'ckeditor5';
import { expect } from 'chai'; import { describe, beforeEach, it, afterEach, vi, expect, MockInstance, expectTypeOf } from "vitest";
import type { SinonSpy } from 'sinon';
import { describe, beforeEach, it, afterEach } from "vitest";
describe( 'MathUI', () => { describe( 'MathUI', () => {
let editorElement: HTMLDivElement; let editorElement: HTMLDivElement;
@ -43,8 +41,8 @@ describe( 'MathUI', () => {
formView = mathUIFeature.formView; formView = mathUIFeature.formView;
// There is no point to execute BalloonPanelView attachTo and pin methods so lets override it. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
sinon.stub( balloon.view, 'attachTo' ).returns( false ); vi.spyOn( balloon.view, 'attachTo' ).mockReturnValue( false );
sinon.stub( balloon.view, 'pin' ).returns(); vi.spyOn( balloon.view, 'pin' ).mockReturnValue();
formView?.render(); formView?.render();
} ); } );
@ -93,19 +91,19 @@ describe( 'MathUI', () => {
} ); } );
it( 'should call #_showUI upon #execute', () => { it( 'should call #_showUI upon #execute', () => {
const spy = sinon.stub( mathUIFeature, '_showUI' ).returns( ); const spy = vi.spyOn( mathUIFeature, '_showUI' ).mockReturnValue();
mathButton.fire( 'execute' ); mathButton.fire( 'execute' );
sinon.assert.calledOnce( spy ); expect(spy).toHaveBeenCalledOnce();
} ); } );
} ); } );
} ); } );
describe( '_showUI()', () => { describe( '_showUI()', () => {
let balloonAddSpy: SinonSpy; let balloonAddSpy: MockInstance;
beforeEach( () => { beforeEach( () => {
balloonAddSpy = sinon.spy( balloon, 'add' ); balloonAddSpy = vi.spyOn( balloon, 'add' );
editor.editing.view.document.isFocused = true; editor.editing.view.document.isFocused = true;
} ); } );
@ -138,12 +136,12 @@ describe( 'MathUI', () => {
mathUIFeature._showUI(); mathUIFeature._showUI();
expect( balloon.visibleView ).to.equal( formView ); expect( balloon.visibleView ).to.equal( formView );
sinon.assert.calledWithExactly( balloonAddSpy, { expect(balloonAddSpy).toHaveBeenCalledWith({
view: formView, view: formView,
position: { position: {
target: selectedRange target: selectedRange
} }
} ); });
} ); } );
it( 'should add #mainFormView to the balloon and attach the balloon to the selection when selection is collapsed', () => { it( 'should add #mainFormView to the balloon and attach the balloon to the selection when selection is collapsed', () => {
@ -153,7 +151,7 @@ describe( 'MathUI', () => {
mathUIFeature._showUI(); mathUIFeature._showUI();
expect( balloon.visibleView ).to.equal( formView ); expect( balloon.visibleView ).to.equal( formView );
sinon.assert.calledWithExactly( balloonAddSpy, { expect(balloonAddSpy).toHaveBeenCalledWith( balloonAddSpy, {
view: formView, view: formView,
position: { position: {
target: selectedRange target: selectedRange
@ -195,21 +193,20 @@ describe( 'MathUI', () => {
} ); } );
it( 'should focus the `editable` by default', () => { it( 'should focus the `editable` by default', () => {
const spy = sinon.spy( editor.editing.view, 'focus' ); const spy = vi.spyOn( editor.editing.view, 'focus' );
mathUIFeature._hideUI(); mathUIFeature._hideUI();
// First call is from _removeFormView. // First call is from _removeFormView.
sinon.assert.calledTwice( spy ); expect(spy).toHaveBeenCalledTimes(2);
} ); } );
it( 'should focus the `editable` before before removing elements from the balloon', () => { it( 'should focus the `editable` before before removing elements from the balloon', () => {
const focusSpy = sinon.spy( editor.editing.view, 'focus' ); const focusSpy = vi.spyOn( editor.editing.view, 'focus' );
const removeSpy = sinon.spy( balloon, 'remove' ); const removeSpy = vi.spyOn( balloon, 'remove' );
mathUIFeature._hideUI(); mathUIFeature._hideUI();
expect(focusSpy).toHaveBeenCalledBefore(removeSpy);
expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
} ); } );
it( 'should not throw an error when views are not in the `balloon`', () => { it( 'should not throw an error when views are not in the `balloon`', () => {
@ -221,19 +218,19 @@ describe( 'MathUI', () => {
} ); } );
it( 'should clear ui#update listener from the ViewDocument', () => { it( 'should clear ui#update listener from the ViewDocument', () => {
const spy = sinon.spy(); const spy = vi.fn();
mathUIFeature.listenTo( editor.ui, 'update', spy ); mathUIFeature.listenTo( editor.ui, 'update', spy );
mathUIFeature._hideUI(); mathUIFeature._hideUI();
editor.ui.fire( 'update' ); editor.ui.fire( 'update' );
sinon.assert.notCalled( spy ); expect(spy).not.toHaveBeenCalled();
} ); } );
} ); } );
describe( 'keyboard support', () => { describe( 'keyboard support', () => {
it( 'should show the UI on Ctrl+M keystroke', () => { it( 'should show the UI on Ctrl+M keystroke', () => {
const spy = sinon.stub( mathUIFeature, '_showUI' ).returns( ); const spy = vi.spyOn( mathUIFeature, '_showUI' ).mockReturnValue( );
const command = editor.commands.get( 'math' )!; const command = editor.commands.get( 'math' )!;
command.isEnabled = false; command.isEnabled = false;
@ -244,23 +241,23 @@ describe( 'MathUI', () => {
altKey: false, altKey: false,
shiftKey: false, shiftKey: false,
metaKey: false, metaKey: false,
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
}; };
editor.keystrokes.press( keydata ); editor.keystrokes.press( keydata );
sinon.assert.notCalled( spy ); expect(spy).not.toHaveBeenCalled();
command.isEnabled = true; command.isEnabled = true;
editor.keystrokes.press( keydata ); editor.keystrokes.press( keydata );
sinon.assert.calledOnce( spy ); expect(spy).toHaveBeenCalled();
} ); } );
it( 'should prevent default action on Ctrl+M keystroke', () => { it( 'should prevent default action on Ctrl+M keystroke', () => {
const preventDefaultSpy = sinon.spy(); const preventDefaultSpy = vi.fn();
const stopPropagationSpy = sinon.spy(); const stopPropagationSpy = vi.fn();
const keyEvtData = { const keyEvtData = {
altKey: false, altKey: false,
@ -274,8 +271,8 @@ describe( 'MathUI', () => {
editor.keystrokes.press( keyEvtData ); editor.keystrokes.press( keyEvtData );
sinon.assert.calledOnce( preventDefaultSpy ); expect(preventDefaultSpy).toHaveBeenCalledOnce();
sinon.assert.calledOnce( stopPropagationSpy ); expect(stopPropagationSpy).toHaveBeenCalledOnce();
} ); } );
it( 'should make stack with math visible on Ctrl+M keystroke - no math', () => { it( 'should make stack with math visible on Ctrl+M keystroke - no math', () => {
@ -294,8 +291,8 @@ describe( 'MathUI', () => {
altKey: false, altKey: false,
shiftKey: false, shiftKey: false,
metaKey: false, metaKey: false,
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
}; };
editor.keystrokes.press( keyEvtData ); editor.keystrokes.press( keyEvtData );
@ -322,47 +319,47 @@ describe( 'MathUI', () => {
shiftKey: false, shiftKey: false,
metaKey: false, metaKey: false,
// @ts-expect-error - preventDefault // @ts-expect-error - preventDefault
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
} ); } );
expect( balloon.visibleView ).to.equal( formView ); expect( balloon.visibleView ).to.equal( formView );
} ); } );
it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => { it( 'should hide the UI after Esc key press (from editor) and not focus the editable', () => {
const spy = sinon.spy( mathUIFeature, '_hideUI' ); const spy = vi.spyOn( mathUIFeature, '_hideUI' );
const keyEvtData = { const keyEvtData = {
altKey: false, altKey: false,
ctrlKey: false, ctrlKey: false,
shiftKey: false, shiftKey: false,
metaKey: false, metaKey: false,
keyCode: keyCodes.esc, keyCode: keyCodes.esc,
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
}; };
// Balloon is visible. // Balloon is visible.
mathUIFeature._showUI(); mathUIFeature._showUI();
editor.keystrokes.press( keyEvtData ); editor.keystrokes.press( keyEvtData );
sinon.assert.calledWithExactly( spy ); expect(spy).toHaveBeenCalledExactlyOnceWith();
} ); } );
it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => { it( 'should not hide the UI after Esc key press (from editor) when UI is open but is not visible', () => {
const spy = sinon.spy( mathUIFeature, '_hideUI' ); const spy = vi.spyOn( mathUIFeature, '_hideUI' );
const keyEvtData = { const keyEvtData = {
altKey: false, altKey: false,
shiftKey: false, shiftKey: false,
ctrlKey: false, ctrlKey: false,
metaKey: false, metaKey: false,
keyCode: keyCodes.esc, keyCode: keyCodes.esc,
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
}; };
const viewMock = new View(); const viewMock = new View();
sinon.stub( viewMock, 'render' ); vi.spyOn( viewMock, 'render' );
sinon.stub( viewMock, 'destroy' ); vi.spyOn( viewMock, 'destroy' );
mathUIFeature._showUI(); mathUIFeature._showUI();
@ -370,27 +367,27 @@ describe( 'MathUI', () => {
balloon.add( { view: viewMock } ); balloon.add( { view: viewMock } );
editor.keystrokes.press( keyEvtData ); editor.keystrokes.press( keyEvtData );
sinon.assert.notCalled( spy ); expect(spy).not.toHaveBeenCalled();
} ); } );
} ); } );
describe( 'mouse support', () => { describe( 'mouse support', () => {
it( 'should hide the UI and not focus editable upon clicking outside the UI', () => { it( 'should hide the UI and not focus editable upon clicking outside the UI', () => {
const spy = sinon.spy( mathUIFeature, '_hideUI' ); const spy = vi.spyOn( mathUIFeature, '_hideUI' );
mathUIFeature._showUI(); mathUIFeature._showUI();
document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) ); document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
sinon.assert.calledWithExactly( spy ); expect(spy).toHaveBeenCalledExactlyOnceWith();
} ); } );
it( 'should not hide the UI upon clicking inside the the UI', () => { it( 'should not hide the UI upon clicking inside the the UI', () => {
const spy = sinon.spy( mathUIFeature, '_hideUI' ); const spy = vi.spyOn( mathUIFeature, '_hideUI' );
mathUIFeature._showUI(); mathUIFeature._showUI();
balloon.view.element!.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) ); balloon.view.element!.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
sinon.assert.notCalled( spy ); expect(spy).not.toHaveBeenCalled();
} ); } );
} ); } );
@ -420,7 +417,7 @@ describe( 'MathUI', () => {
} ); } );
it( 'should execute math command on mainFormView#submit event', () => { it( 'should execute math command on mainFormView#submit event', () => {
const executeSpy = sinon.spy( editor, 'execute' ); const executeSpy = vi.spyOn( editor, 'execute' );
formView!.mathInputView.value = 'x^2'; formView!.mathInputView.value = 'x^2';
expect( formView!.mathInputView.fieldView.element!.value ).to.equal( 'x^2' ); expect( formView!.mathInputView.fieldView.element!.value ).to.equal( 'x^2' );
@ -428,8 +425,7 @@ describe( 'MathUI', () => {
formView!.mathInputView.fieldView.element!.value = 'x^2'; formView!.mathInputView.fieldView.element!.value = 'x^2';
formView!.fire( 'submit' ); formView!.fire( 'submit' );
expect( executeSpy.calledOnce ).to.be.true; expect(executeSpy).toHaveBeenCalledExactlyOnceWith('math', 'x^2');
expect( executeSpy.calledWith( 'math', 'x^2' ) ).to.be.true;
} ); } );
it( 'should hide the balloon on mainFormView#cancel if math command does not have a value', () => { it( 'should hide the balloon on mainFormView#cancel if math command does not have a value', () => {
@ -446,8 +442,8 @@ describe( 'MathUI', () => {
metaKey: false, metaKey: false,
ctrlKey: false, ctrlKey: false,
keyCode: keyCodes.esc, keyCode: keyCodes.esc,
preventDefault: sinon.spy(), preventDefault: vi.fn(),
stopPropagation: sinon.spy() stopPropagation: vi.fn()
}; };
mathUIFeature._showUI(); mathUIFeature._showUI();
@ -460,12 +456,12 @@ describe( 'MathUI', () => {
it( 'should blur math input element before hiding the view', () => { it( 'should blur math input element before hiding the view', () => {
mathUIFeature._showUI(); mathUIFeature._showUI();
const focusSpy = sinon.spy( formView!.saveButtonView, 'focus' ); const focusSpy = vi.spyOn( formView!.saveButtonView, 'focus' );
const removeSpy = sinon.spy( balloon, 'remove' ); const removeSpy = vi.spyOn( balloon, 'remove' );
formView!.fire( 'cancel' ); formView!.fire( 'cancel' );
expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true ); expect(focusSpy).toHaveBeenCalledBefore(removeSpy);
} ); } );
} ); } );
} ); } );

View File

@ -207,7 +207,7 @@ describe( 'MermaidEditing', () => {
const previewContainerView = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 2 ); const previewContainerView = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 2 );
domPreviewContainer = editor.editing.view.domConverter.viewToDom( previewContainerView ); domPreviewContainer = editor.editing.view.domConverter.viewToDom( previewContainerView );
renderMermaidStub = sinon.stub( editor.plugins.get( 'MermaidEditing' ), '_renderMermaid' ); renderMermaidStub = vi.spyOn( editor.plugins.get( 'MermaidEditing' ), '_renderMermaid' );
} ); } );
afterEach( () => { afterEach( () => {