imagetextalternative.js 7.6 KB

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