imagetextalternative.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global Event, document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Image from '../src/image';
  8. import ImageTextAlternative from '../src/imagetextalternative';
  9. import ImageTextAlternativeEngine from '../src/imagetextalternative/imagetextalternativeengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  15. describe( 'ImageTextAlternative', () => {
  16. let editor, viewDocument, model, view, doc, plugin, command, form, balloon, editorElement, button;
  17. beforeEach( () => {
  18. editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ ImageTextAlternative, Image ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. view = editor.editing.view;
  28. viewDocument = view.document;
  29. doc = model.document;
  30. newEditor.editing.view.attachDomRoot( editorElement );
  31. plugin = editor.plugins.get( ImageTextAlternative );
  32. command = editor.commands.get( 'imageTextAlternative' );
  33. form = plugin._form;
  34. balloon = editor.plugins.get( 'ContextualBalloon' );
  35. button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. return editor.destroy();
  41. } );
  42. it( 'should be loaded', () => {
  43. expect( plugin ).to.be.instanceOf( ImageTextAlternative );
  44. } );
  45. it( 'should load ImageTextAlternativeEngine plugin', () => {
  46. expect( editor.plugins.get( ImageTextAlternativeEngine ) ).to.be.instanceOf( ImageTextAlternativeEngine );
  47. } );
  48. describe( 'toolbar button', () => {
  49. it( 'should be registered in component factory', () => {
  50. expect( button ).to.be.instanceOf( ButtonView );
  51. } );
  52. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  53. command.isEnabled = true;
  54. expect( button.isEnabled ).to.be.true;
  55. command.isEnabled = false;
  56. expect( button.isEnabled ).to.be.false;
  57. } );
  58. it( 'should show balloon panel on execute', () => {
  59. expect( balloon.visibleView ).to.be.null;
  60. setData( model, '[<image src="" alt="foo bar"></image>]' );
  61. button.fire( 'execute' );
  62. expect( balloon.visibleView ).to.equal( form );
  63. // Make sure successive execute does not throw, e.g. attempting
  64. // to display the form twice.
  65. button.fire( 'execute' );
  66. expect( balloon.visibleView ).to.equal( form );
  67. } );
  68. it( 'should set alt attribute value to textarea and select it', () => {
  69. const spy = sinon.spy( form.labeledInput, 'select' );
  70. setData( model, '[<image src="" alt="foo bar"></image>]' );
  71. button.fire( 'execute' );
  72. sinon.assert.calledOnce( spy );
  73. expect( form.labeledInput.value ).equals( 'foo bar' );
  74. } );
  75. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  76. const spy = sinon.spy( form.labeledInput, 'select' );
  77. setData( model, '[<image src=""></image>]' );
  78. button.fire( 'execute' );
  79. sinon.assert.calledOnce( spy );
  80. expect( form.labeledInput.value ).equals( '' );
  81. } );
  82. } );
  83. describe( 'balloon panel form', () => {
  84. // https://github.com/ckeditor/ckeditor5-image/issues/114
  85. it( 'should make sure the input always stays in sync with the value of the command', () => {
  86. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  87. // Mock the value of the input after some past editing.
  88. form.labeledInput.value = 'foo';
  89. // Mock the user using the form, changing the value but clicking "Cancel".
  90. // so the command's value is not updated.
  91. form.labeledInput.inputView.element.value = 'This value was canceled.';
  92. // Mock the user editing the same image once again.
  93. setData( model, '[<image src="" alt="foo"></image>]' );
  94. button.fire( 'execute' );
  95. expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
  96. } );
  97. it( 'should execute command on submit', () => {
  98. const spy = sinon.spy( editor, 'execute' );
  99. form.fire( 'submit' );
  100. sinon.assert.calledOnce( spy );
  101. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  102. newValue: form.labeledInput.inputView.element.value
  103. } );
  104. } );
  105. it( 'should hide the panel on cancel and focus the editing view', () => {
  106. const spy = sinon.spy( editor.editing.view, 'focus' );
  107. setData( model, '[<image src="" alt="foo bar"></image>]' );
  108. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  109. expect( balloon.visibleView ).to.equal( form );
  110. form.fire( 'cancel' );
  111. expect( balloon.visibleView ).to.be.null;
  112. sinon.assert.calledOnce( spy );
  113. } );
  114. it( 'should not engage when the form is in the balloon yet invisible', () => {
  115. setData( model, '[<image src=""></image>]' );
  116. button.fire( 'execute' );
  117. expect( balloon.visibleView ).to.equal( form );
  118. const lastView = new View();
  119. lastView.element = document.createElement( 'div' );
  120. balloon.add( { view: lastView } );
  121. expect( balloon.visibleView ).to.equal( lastView );
  122. button.fire( 'execute' );
  123. expect( balloon.visibleView ).to.equal( lastView );
  124. } );
  125. describe( 'integration with the editor selection (#change event)', () => {
  126. it( 'should re-position the form', () => {
  127. setData( model, '[<image src=""></image>]' );
  128. button.fire( 'execute' );
  129. const spy = sinon.spy( balloon, 'updatePosition' );
  130. viewDocument.fire( 'change' );
  131. sinon.assert.calledOnce( spy );
  132. } );
  133. it( 'should hide the form and focus editable when image widget has been removed by external change', () => {
  134. setData( model, '[<image src=""></image>]' );
  135. button.fire( 'execute' );
  136. const remveSpy = sinon.spy( balloon, 'remove' );
  137. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  138. // EnqueueChange automatically fires #render event.
  139. model.enqueueChange( 'transparent', writer => {
  140. writer.remove( doc.selection.getFirstRange() );
  141. } );
  142. sinon.assert.calledWithExactly( remveSpy, form );
  143. sinon.assert.calledOnce( focusSpy );
  144. } );
  145. } );
  146. describe( 'close listeners', () => {
  147. describe( 'keyboard', () => {
  148. it( 'should close upon Esc key press and focus the editing view', () => {
  149. const hideSpy = sinon.spy( plugin, '_hideForm' );
  150. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  151. setData( model, '[<image src=""></image>]' );
  152. button.fire( 'execute' );
  153. const keyEvtData = {
  154. keyCode: keyCodes.esc,
  155. preventDefault: sinon.spy(),
  156. stopPropagation: sinon.spy()
  157. };
  158. form.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( hideSpy );
  160. sinon.assert.calledOnce( keyEvtData.preventDefault );
  161. sinon.assert.calledOnce( focusSpy );
  162. } );
  163. } );
  164. describe( 'mouse', () => {
  165. it( 'should close and not focus editable on click outside the panel', () => {
  166. const hideSpy = sinon.spy( plugin, '_hideForm' );
  167. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  168. setData( model, '[<image src=""></image>]' );
  169. button.fire( 'execute' );
  170. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  171. sinon.assert.called( hideSpy );
  172. sinon.assert.notCalled( focusSpy );
  173. } );
  174. it( 'should not close on click inside the panel', () => {
  175. const spy = sinon.spy( plugin, '_hideForm' );
  176. setData( model, '[<image src=""></image>]' );
  177. button.fire( 'execute' );
  178. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  179. sinon.assert.notCalled( spy );
  180. } );
  181. } );
  182. } );
  183. } );
  184. } );