|
|
@@ -3,23 +3,23 @@
|
|
|
* For licensing, see LICENSE.md.
|
|
|
*/
|
|
|
|
|
|
+/* global Event, document */
|
|
|
+
|
|
|
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
|
|
|
import Image from '../src/image';
|
|
|
-import ImageToolbar from '../src/imagetoolbar';
|
|
|
import ImageTextAlternative from '../src/imagetextalternative';
|
|
|
import ImageTextAlternativeEngine from '../src/imagetextalternative/imagetextalternativeengine';
|
|
|
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
|
|
+import View from '@ckeditor/ckeditor5-ui/src/view';
|
|
|
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
|
|
|
import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
|
|
|
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
|
|
|
|
|
|
-/* global Event */
|
|
|
-
|
|
|
describe( 'ImageTextAlternative', () => {
|
|
|
- let editor, plugin, command, balloonPanel, form;
|
|
|
+ let editor, editingView, doc, plugin, command, form, balloon, editorElement, button;
|
|
|
|
|
|
beforeEach( () => {
|
|
|
- const editorElement = global.document.createElement( 'div' );
|
|
|
+ editorElement = global.document.createElement( 'div' );
|
|
|
global.document.body.appendChild( editorElement );
|
|
|
|
|
|
return ClassicTestEditor.create( editorElement, {
|
|
|
@@ -27,15 +27,20 @@ describe( 'ImageTextAlternative', () => {
|
|
|
} )
|
|
|
.then( newEditor => {
|
|
|
editor = newEditor;
|
|
|
+ editingView = editor.editing.view;
|
|
|
+ doc = editor.document;
|
|
|
newEditor.editing.view.attachDomRoot( editorElement );
|
|
|
plugin = editor.plugins.get( ImageTextAlternative );
|
|
|
command = editor.commands.get( 'imageTextAlternative' );
|
|
|
- balloonPanel = plugin.balloonPanel;
|
|
|
- form = plugin.form;
|
|
|
+ form = plugin._form;
|
|
|
+ balloon = editor.plugins.get( 'ContextualBalloon' );
|
|
|
+ button = editor.ui.componentFactory.create( 'imageTextAlternative' );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
afterEach( () => {
|
|
|
+ editorElement.remove();
|
|
|
+
|
|
|
return editor.destroy();
|
|
|
} );
|
|
|
|
|
|
@@ -48,12 +53,6 @@ describe( 'ImageTextAlternative', () => {
|
|
|
} );
|
|
|
|
|
|
describe( 'toolbar button', () => {
|
|
|
- let button;
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
- button = editor.ui.componentFactory.create( 'imageTextAlternative' );
|
|
|
- } );
|
|
|
-
|
|
|
it( 'should be registered in component factory', () => {
|
|
|
expect( button ).to.be.instanceOf( ButtonView );
|
|
|
} );
|
|
|
@@ -67,29 +66,37 @@ describe( 'ImageTextAlternative', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should show balloon panel on execute', () => {
|
|
|
- const spy = sinon.spy( balloonPanel, 'attach' );
|
|
|
- setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
|
|
|
+ expect( balloon.visibleView ).to.be.null;
|
|
|
+
|
|
|
+ setData( doc, '[<image src="" alt="foo bar"></image>]' );
|
|
|
+
|
|
|
button.fire( 'execute' );
|
|
|
+ expect( balloon.visibleView ).to.equal( form );
|
|
|
|
|
|
- sinon.assert.calledOnce( spy );
|
|
|
+ // Make sure successive execute does not throw, e.g. attempting
|
|
|
+ // to display the form twice.
|
|
|
+ button.fire( 'execute' );
|
|
|
+ expect( balloon.visibleView ).to.equal( form );
|
|
|
} );
|
|
|
|
|
|
it( 'should set alt attribute value to textarea and select it', () => {
|
|
|
const spy = sinon.spy( form.labeledInput, 'select' );
|
|
|
- setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
|
|
|
- button.fire( 'execute' );
|
|
|
|
|
|
+ setData( doc, '[<image src="" alt="foo bar"></image>]' );
|
|
|
+
|
|
|
+ button.fire( 'execute' );
|
|
|
sinon.assert.calledOnce( spy );
|
|
|
- expect( plugin.form.labeledInput.value ).equals( 'foo bar' );
|
|
|
+ expect( form.labeledInput.value ).equals( 'foo bar' );
|
|
|
} );
|
|
|
|
|
|
it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
|
|
|
const spy = sinon.spy( form.labeledInput, 'select' );
|
|
|
- setData( editor.document, '[<image src=""></image>]' );
|
|
|
- button.fire( 'execute' );
|
|
|
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+
|
|
|
+ button.fire( 'execute' );
|
|
|
sinon.assert.calledOnce( spy );
|
|
|
- expect( plugin.form.labeledInput.value ).equals( '' );
|
|
|
+ expect( form.labeledInput.value ).equals( '' );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
@@ -106,7 +113,7 @@ describe( 'ImageTextAlternative', () => {
|
|
|
form.labeledInput.inputView.element.value = 'This value was canceled.';
|
|
|
|
|
|
// Mock the user editing the same image once again.
|
|
|
- setData( editor.document, '[<image src="" alt="foo"></image>]' );
|
|
|
+ setData( doc, '[<image src="" alt="foo"></image>]' );
|
|
|
|
|
|
button.fire( 'execute' );
|
|
|
expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
|
|
|
@@ -117,126 +124,110 @@ describe( 'ImageTextAlternative', () => {
|
|
|
form.fire( 'submit' );
|
|
|
|
|
|
sinon.assert.calledOnce( spy );
|
|
|
- sinon.assert.calledWithExactly( spy, 'imageTextAlternative', { newValue: form.labeledInput.inputView.element.value } );
|
|
|
+ sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
|
|
|
+ newValue: form.labeledInput.inputView.element.value
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- it( 'should detach panel on cancel', () => {
|
|
|
- const spy = sinon.spy( balloonPanel, 'detach' );
|
|
|
+ it( 'should hide the panel on cancel and focus the editing view', () => {
|
|
|
+ const spy = sinon.spy( editor.editing.view, 'focus' );
|
|
|
+
|
|
|
+ setData( doc, '[<image src="" alt="foo bar"></image>]' );
|
|
|
+
|
|
|
+ editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
|
|
|
+ expect( balloon.visibleView ).to.equal( form );
|
|
|
+
|
|
|
form.fire( 'cancel' );
|
|
|
+ expect( balloon.visibleView ).to.be.null;
|
|
|
+ sinon.assert.calledOnce( spy );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not engage when the form is in the balloon yet invisible', () => {
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
+ expect( balloon.visibleView ).to.equal( form );
|
|
|
|
|
|
- sinon.assert.called( spy );
|
|
|
+ const lastView = new View();
|
|
|
+ lastView.element = document.createElement( 'div' );
|
|
|
+
|
|
|
+ balloon.add( { view: lastView } );
|
|
|
+ expect( balloon.visibleView ).to.equal( lastView );
|
|
|
+
|
|
|
+ button.fire( 'execute' );
|
|
|
+ expect( balloon.visibleView ).to.equal( lastView );
|
|
|
} );
|
|
|
|
|
|
- it( 'should show ImageToolbar on cancel if present', () => {
|
|
|
- const editorElement = global.document.createElement( 'div' );
|
|
|
- global.document.body.appendChild( editorElement );
|
|
|
-
|
|
|
- return ClassicTestEditor.create( editorElement, {
|
|
|
- plugins: [ ImageTextAlternative, Image, ImageToolbar ],
|
|
|
- image: {
|
|
|
- toolbar: [ 'imageTextAlternative' ]
|
|
|
- }
|
|
|
- } )
|
|
|
- .then( newEditor => {
|
|
|
- newEditor.editing.view.attachDomRoot( editorElement );
|
|
|
- const plugin = newEditor.plugins.get( ImageTextAlternative );
|
|
|
- const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
|
|
|
- const form = plugin.form;
|
|
|
-
|
|
|
- const spy = sinon.spy( toolbarPlugin, 'show' );
|
|
|
- form.fire( 'cancel' );
|
|
|
-
|
|
|
- sinon.assert.called( spy );
|
|
|
+ describe( 'integration with the editor selection (#render event)', () => {
|
|
|
+ it( 'should re-position the form', () => {
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
+
|
|
|
+ const spy = sinon.spy( balloon, 'updatePosition' );
|
|
|
+
|
|
|
+ editingView.fire( 'render' );
|
|
|
+ sinon.assert.calledOnce( spy );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'close listeners', () => {
|
|
|
- let hidePanelSpy;
|
|
|
+ describe( 'integration with the editor focus', () => {
|
|
|
+ it( 'should hide the toolbar when the editor loses focus and the image is selected', () => {
|
|
|
+ editor.ui.focusTracker.isFocused = true;
|
|
|
+
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
|
|
|
- beforeEach( () => {
|
|
|
- hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
|
|
|
+ const spy = sinon.spy( balloon, 'remove' );
|
|
|
+
|
|
|
+ editor.ui.focusTracker.isFocused = false;
|
|
|
+ sinon.assert.calledWithExactly( spy, form );
|
|
|
} );
|
|
|
+ } );
|
|
|
|
|
|
+ describe( 'close listeners', () => {
|
|
|
describe( 'keyboard', () => {
|
|
|
- it( 'should close after `ESC` press', () => {
|
|
|
- balloonPanel.isVisible = true;
|
|
|
- const keyCode = keyCodes.esc;
|
|
|
- const event = global.document.createEvent( 'Events' );
|
|
|
- event.initEvent( 'keydown', true, true );
|
|
|
- event.which = keyCode;
|
|
|
- event.keyCode = keyCode;
|
|
|
- global.document.dispatchEvent( event );
|
|
|
-
|
|
|
- sinon.assert.called( hidePanelSpy );
|
|
|
+ it( 'should close upon Esc key press and focus the editing view', () => {
|
|
|
+ const hideSpy = sinon.spy( plugin, '_hideForm' );
|
|
|
+ const focusSpy = sinon.spy( editor.editing.view, 'focus' );
|
|
|
+
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
+
|
|
|
+ const keyEvtData = {
|
|
|
+ keyCode: keyCodes.esc,
|
|
|
+ preventDefault: sinon.spy(),
|
|
|
+ stopPropagation: sinon.spy()
|
|
|
+ };
|
|
|
+
|
|
|
+ form.keystrokes.press( keyEvtData );
|
|
|
+ sinon.assert.calledOnce( hideSpy );
|
|
|
+ sinon.assert.calledOnce( keyEvtData.preventDefault );
|
|
|
+ sinon.assert.calledOnce( focusSpy );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
describe( 'mouse', () => {
|
|
|
it( 'should close and not focus editable on click outside the panel', () => {
|
|
|
- balloonPanel.isVisible = true;
|
|
|
- global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
|
|
|
+ const hideSpy = sinon.spy( plugin, '_hideForm' );
|
|
|
+ const focusSpy = sinon.spy( editor.editing.view, 'focus' );
|
|
|
|
|
|
- sinon.assert.called( hidePanelSpy );
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
+
|
|
|
+ global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
|
|
|
+ sinon.assert.called( hideSpy );
|
|
|
+ sinon.assert.notCalled( focusSpy );
|
|
|
} );
|
|
|
|
|
|
it( 'should not close on click inside the panel', () => {
|
|
|
- balloonPanel.isVisible = true;
|
|
|
- balloonPanel.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
|
|
|
+ const spy = sinon.spy( plugin, '_hideForm' );
|
|
|
|
|
|
- sinon.assert.notCalled( hidePanelSpy );
|
|
|
- } );
|
|
|
- } );
|
|
|
- } );
|
|
|
- } );
|
|
|
+ setData( doc, '[<image src=""></image>]' );
|
|
|
+ button.fire( 'execute' );
|
|
|
|
|
|
- describe( 'working with ImageToolbar', () => {
|
|
|
- let editor, button, imageToolbarPlugin, plugin;
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
- const editorElement = global.document.createElement( 'div' );
|
|
|
- global.document.body.appendChild( editorElement );
|
|
|
-
|
|
|
- return ClassicTestEditor.create( editorElement, {
|
|
|
- plugins: [ ImageTextAlternative, Image, ImageToolbar ],
|
|
|
- image: {
|
|
|
- toolbar: [ 'imageTextAlternative' ]
|
|
|
- }
|
|
|
- } )
|
|
|
- .then( newEditor => {
|
|
|
- editor = newEditor;
|
|
|
- editor.editing.view.attachDomRoot( editorElement );
|
|
|
- button = newEditor.ui.componentFactory.create( 'imageTextAlternative' );
|
|
|
- imageToolbarPlugin = newEditor.plugins.get( ImageToolbar );
|
|
|
- plugin = editor.plugins.get( ImageTextAlternative );
|
|
|
+ form.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
|
|
|
+ sinon.assert.notCalled( spy );
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|
|
|
-
|
|
|
- afterEach( () => editor.destroy() );
|
|
|
-
|
|
|
- it( 'should hide ImageToolbar when visible', () => {
|
|
|
- setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
|
|
|
- const spy = sinon.spy( imageToolbarPlugin, 'hide' );
|
|
|
- button.fire( 'execute' );
|
|
|
-
|
|
|
- sinon.assert.calledOnce( spy );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'ImageToolbar should not show when text alternative panel is visible', () => {
|
|
|
- setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
|
|
|
- button.fire( 'execute' );
|
|
|
- const spy = sinon.spy( imageToolbarPlugin, 'show' );
|
|
|
- editor.editing.view.render();
|
|
|
-
|
|
|
- sinon.assert.notCalled( spy );
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'ImageToolbar should show when text alternative panel is not visible', () => {
|
|
|
- setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
|
|
|
- button.fire( 'execute' );
|
|
|
- const spy = sinon.spy( imageToolbarPlugin, 'show' );
|
|
|
- plugin.balloonPanel.isVisible = false;
|
|
|
-
|
|
|
- sinon.assert.called( spy );
|
|
|
- } );
|
|
|
} );
|
|
|
} );
|