imagetextalternative.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.lebeledInput, 'select' );
  63. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  64. button.fire( 'execute' );
  65. sinon.assert.calledOnce( spy );
  66. expect( plugin.form.lebeledInput.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.lebeledInput, 'select' );
  70. setData( editor.document, '[<image src=""></image>]' );
  71. button.fire( 'execute' );
  72. sinon.assert.calledOnce( spy );
  73. expect( plugin.form.lebeledInput.value ).equals( '' );
  74. } );
  75. } );
  76. describe( 'balloon panel form', () => {
  77. it( 'should execute command on submit', () => {
  78. const spy = sinon.spy( editor, 'execute' );
  79. form.fire( 'submit' );
  80. sinon.assert.calledOnce( spy );
  81. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', { newValue: form.lebeledInput.inputView.element.value } );
  82. } );
  83. it( 'should detach panel on cancel', () => {
  84. const spy = sinon.spy( balloonPanel, 'detach' );
  85. form.fire( 'cancel' );
  86. sinon.assert.called( spy );
  87. } );
  88. it( 'should show ImageToolbar on cancel if present', () => {
  89. const editorElement = global.document.createElement( 'div' );
  90. global.document.body.appendChild( editorElement );
  91. return ClassicTestEditor.create( editorElement, {
  92. plugins: [ ImageTextAlternative, Image, ImageToolbar ],
  93. image: {
  94. toolbar: [ 'imageTextAlternative' ]
  95. }
  96. } )
  97. .then( newEditor => {
  98. newEditor.editing.view.attachDomRoot( editorElement );
  99. const plugin = newEditor.plugins.get( ImageTextAlternative );
  100. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  101. const form = plugin.form;
  102. const spy = sinon.spy( toolbarPlugin, 'show' );
  103. form.fire( 'cancel' );
  104. sinon.assert.called( spy );
  105. } );
  106. } );
  107. describe( 'close listeners', () => {
  108. let hidePanelSpy;
  109. beforeEach( () => {
  110. hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
  111. } );
  112. describe( 'keyboard', () => {
  113. it( 'should close after `ESC` press', () => {
  114. balloonPanel.isVisible = true;
  115. const keyCode = keyCodes.esc;
  116. const event = global.document.createEvent( 'Events' );
  117. event.initEvent( 'keydown', true, true );
  118. event.which = keyCode;
  119. event.keyCode = keyCode;
  120. global.document.dispatchEvent( event );
  121. sinon.assert.called( hidePanelSpy );
  122. } );
  123. } );
  124. describe( 'mouse', () => {
  125. it( 'should close and not focus editable on click outside the panel', () => {
  126. balloonPanel.isVisible = true;
  127. global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  128. sinon.assert.called( hidePanelSpy );
  129. } );
  130. it( 'should not close on click inside the panel', () => {
  131. balloonPanel.isVisible = true;
  132. balloonPanel.element.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  133. sinon.assert.notCalled( hidePanelSpy );
  134. } );
  135. } );
  136. } );
  137. } );
  138. describe( 'working with ImageToolbar', () => {
  139. let editor, button, imageToolbarPlugin, plugin;
  140. beforeEach( () => {
  141. const editorElement = global.document.createElement( 'div' );
  142. global.document.body.appendChild( editorElement );
  143. return ClassicTestEditor.create( editorElement, {
  144. plugins: [ ImageTextAlternative, Image, ImageToolbar ],
  145. image: {
  146. toolbar: [ 'imageTextAlternative' ]
  147. }
  148. } )
  149. .then( newEditor => {
  150. editor = newEditor;
  151. editor.editing.view.attachDomRoot( editorElement );
  152. button = newEditor.ui.componentFactory.create( 'imageTextAlternative' );
  153. imageToolbarPlugin = newEditor.plugins.get( ImageToolbar );
  154. plugin = editor.plugins.get( ImageTextAlternative );
  155. } );
  156. } );
  157. afterEach( () => editor.destroy() );
  158. it( 'should hide ImageToolbar when visible', () => {
  159. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  160. const spy = sinon.spy( imageToolbarPlugin, 'hide' );
  161. button.fire( 'execute' );
  162. sinon.assert.calledOnce( spy );
  163. } );
  164. it( 'ImageToolbar should not show when text alternative panel is visible', () => {
  165. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  166. button.fire( 'execute' );
  167. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  168. editor.editing.view.render();
  169. sinon.assert.notCalled( spy );
  170. } );
  171. it( 'ImageToolbar should show when text alternative panel is not visible', () => {
  172. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  173. button.fire( 'execute' );
  174. const spy = sinon.spy( imageToolbarPlugin, 'show' );
  175. plugin.balloonPanel.isVisible = false;
  176. sinon.assert.called( spy );
  177. } );
  178. } );
  179. } );