imagetextalternative.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Image from '../src/image';
  7. import ImageToolbar from '../src/imagetoolbar';
  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 global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. /* global Event */
  15. describe( 'ImageTextAlternative', () => {
  16. let editor, plugin, command, balloonPanel, form;
  17. beforeEach( () => {
  18. const 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. newEditor.editing.view.attachDomRoot( editorElement );
  26. plugin = editor.plugins.get( ImageTextAlternative );
  27. command = editor.commands.get( 'imageTextAlternative' );
  28. balloonPanel = plugin.balloonPanel;
  29. form = plugin.form;
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( plugin ).to.be.instanceOf( ImageTextAlternative );
  37. } );
  38. it( 'should load ImageTextAlternativeEngine plugin', () => {
  39. expect( editor.plugins.get( ImageTextAlternativeEngine ) ).to.be.instanceOf( ImageTextAlternativeEngine );
  40. } );
  41. describe( 'toolbar button', () => {
  42. let button;
  43. beforeEach( () => {
  44. button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  45. } );
  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. const spy = sinon.spy( balloonPanel, 'attach' );
  57. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  58. button.fire( 'execute' );
  59. sinon.assert.calledOnce( spy );
  60. } );
  61. it( 'should set alt attribute value to textarea and select it', () => {
  62. const spy = sinon.spy( form.labeledInput, 'select' );
  63. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  64. button.fire( 'execute' );
  65. sinon.assert.calledOnce( spy );
  66. expect( plugin.form.labeledInput.value ).equals( 'foo bar' );
  67. } );
  68. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  69. const spy = sinon.spy( form.labeledInput, 'select' );
  70. setData( editor.document, '[<image src=""></image>]' );
  71. button.fire( 'execute' );
  72. sinon.assert.calledOnce( spy );
  73. expect( plugin.form.labeledInput.value ).equals( '' );
  74. } );
  75. } );
  76. describe( 'balloon panel form', () => {
  77. // https://github.com/ckeditor/ckeditor5-image/issues/114
  78. it( 'should make sure the input always stays in sync with the value of the command', () => {
  79. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  80. // Mock the value of the input after some past editing.
  81. form.labeledInput.value = 'foo';
  82. // Mock the user using the form, changing the value but clicking "Cancel".
  83. // so the command's value is not updated.
  84. form.labeledInput.inputView.element.value = 'This value was canceled.';
  85. // Mock the user editing the same image once again.
  86. setData( editor.document, '[<image src="" alt="foo"></image>]' );
  87. button.fire( 'execute' );
  88. expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
  89. } );
  90. it( 'should execute command on submit', () => {
  91. const spy = sinon.spy( editor, 'execute' );
  92. form.fire( 'submit' );
  93. sinon.assert.calledOnce( spy );
  94. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', { newValue: form.labeledInput.inputView.element.value } );
  95. } );
  96. it( 'should detach panel on cancel', () => {
  97. const spy = sinon.spy( balloonPanel, 'detach' );
  98. form.fire( 'cancel' );
  99. sinon.assert.called( spy );
  100. } );
  101. it( 'should show ImageToolbar on cancel if present', () => {
  102. const editorElement = global.document.createElement( 'div' );
  103. global.document.body.appendChild( editorElement );
  104. return ClassicTestEditor.create( editorElement, {
  105. plugins: [ ImageTextAlternative, Image, ImageToolbar ],
  106. image: {
  107. toolbar: [ 'imageTextAlternative' ]
  108. }
  109. } )
  110. .then( newEditor => {
  111. newEditor.editing.view.attachDomRoot( editorElement );
  112. const plugin = newEditor.plugins.get( ImageTextAlternative );
  113. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  114. const form = plugin.form;
  115. const spy = sinon.spy( toolbarPlugin, 'show' );
  116. form.fire( 'cancel' );
  117. sinon.assert.called( spy );
  118. } );
  119. } );
  120. describe( 'close listeners', () => {
  121. let hidePanelSpy;
  122. beforeEach( () => {
  123. hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
  124. } );
  125. describe( 'keyboard', () => {
  126. it( 'should close after `ESC` press', () => {
  127. balloonPanel.isVisible = true;
  128. const keyCode = keyCodes.esc;
  129. const event = global.document.createEvent( 'Events' );
  130. event.initEvent( 'keydown', true, true );
  131. event.which = keyCode;
  132. event.keyCode = keyCode;
  133. global.document.dispatchEvent( event );
  134. sinon.assert.called( hidePanelSpy );
  135. } );
  136. } );
  137. describe( 'mouse', () => {
  138. it( 'should close and not focus editable on click outside the panel', () => {
  139. balloonPanel.isVisible = true;
  140. global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  141. sinon.assert.called( hidePanelSpy );
  142. } );
  143. it( 'should not close on click inside the panel', () => {
  144. balloonPanel.isVisible = true;
  145. balloonPanel.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  146. sinon.assert.notCalled( hidePanelSpy );
  147. } );
  148. } );
  149. } );
  150. } );
  151. describe( 'working with ImageToolbar', () => {
  152. let editor, button, imageToolbarPlugin, plugin;
  153. beforeEach( () => {
  154. const editorElement = global.document.createElement( 'div' );
  155. global.document.body.appendChild( editorElement );
  156. return ClassicTestEditor.create( editorElement, {
  157. plugins: [ ImageTextAlternative, Image, ImageToolbar ],
  158. image: {
  159. toolbar: [ 'imageTextAlternative' ]
  160. }
  161. } )
  162. .then( newEditor => {
  163. editor = newEditor;
  164. editor.editing.view.attachDomRoot( editorElement );
  165. button = newEditor.ui.componentFactory.create( 'imageTextAlternative' );
  166. imageToolbarPlugin = newEditor.plugins.get( ImageToolbar );
  167. plugin = editor.plugins.get( ImageTextAlternative );
  168. } );
  169. } );
  170. afterEach( () => editor.destroy() );
  171. it( 'should hide ImageToolbar when visible', () => {
  172. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  173. const spy = sinon.spy( imageToolbarPlugin, 'hide' );
  174. button.fire( 'execute' );
  175. sinon.assert.calledOnce( spy );
  176. } );
  177. it( 'ImageToolbar should not show when text alternative panel is visible', () => {
  178. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  179. button.fire( 'execute' );
  180. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  181. editor.editing.view.render();
  182. sinon.assert.notCalled( spy );
  183. } );
  184. it( 'ImageToolbar should show when text alternative panel is not visible', () => {
  185. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  186. button.fire( 'execute' );
  187. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  188. plugin.balloonPanel.isVisible = false;
  189. sinon.assert.called( spy );
  190. } );
  191. } );
  192. } );