imagetextalternative.js 7.8 KB

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