8
0

imagealternatetext.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global Event */
  6. import ClassicTestEditor from 'ckeditor5-core/tests/_utils/classictesteditor';
  7. import Image from 'ckeditor5-image/src/image';
  8. import ImageToolbar from 'ckeditor5-image/src/imagetoolbar';
  9. import ImageAlternateText from 'ckeditor5-image/src/imagealternatetext/imagealternatetext';
  10. import ImageAlternateTextEngine from 'ckeditor5-image/src/imagealternatetext/imagealternatetextengine';
  11. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  12. import global from 'ckeditor5-utils/src/dom/global';
  13. import { setData } from 'ckeditor5-engine/src/dev-utils/model';
  14. import { keyCodes } from 'ckeditor5-utils/src/keyboard';
  15. describe( 'ImageAlternateText', () => {
  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: [ ImageAlternateText, Image ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. newEditor.editing.view.attachDomRoot( editorElement );
  26. plugin = editor.plugins.get( ImageAlternateText );
  27. command = editor.commands.get( 'imageAlternateText' );
  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( ImageAlternateText );
  37. } );
  38. it( 'should load ImageAlternateTextEngine plugin', () => {
  39. expect( editor.plugins.get( ImageAlternateTextEngine ) ).to.be.instanceOf( ImageAlternateTextEngine );
  40. } );
  41. describe( 'toolbar button', () => {
  42. let button;
  43. beforeEach( () => {
  44. button = editor.ui.componentFactory.create( 'imageAlternateText' );
  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 hide ImageToolbar on execute', () => {
  62. const editorElement = global.document.createElement( 'div' );
  63. global.document.body.appendChild( editorElement );
  64. return ClassicTestEditor.create( editorElement, {
  65. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  66. image: {
  67. toolbar: [ 'imageAlternateText' ]
  68. }
  69. } )
  70. .then( newEditor => {
  71. newEditor.editing.view.attachDomRoot( editorElement );
  72. const button = newEditor.ui.componentFactory.create( 'imageAlternateText' );
  73. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  74. const spy = sinon.spy( toolbarPlugin, 'hide' );
  75. setData( newEditor.document, '[<image src="" alt="foo bar"></image>]' );
  76. button.fire( 'execute' );
  77. sinon.assert.calledOnce( spy );
  78. newEditor.destroy();
  79. } );
  80. } );
  81. it( 'should set alt attribute value to textarea and select it', () => {
  82. const spy = sinon.spy( form.labeledTextarea, 'select' );
  83. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  84. button.fire( 'execute' );
  85. sinon.assert.calledOnce( spy );
  86. expect( plugin.form.labeledTextarea.value ).equals( 'foo bar' );
  87. } );
  88. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  89. const spy = sinon.spy( form.labeledTextarea, 'select' );
  90. setData( editor.document, '[<image src=""></image>]' );
  91. button.fire( 'execute' );
  92. sinon.assert.calledOnce( spy );
  93. expect( plugin.form.labeledTextarea.value ).equals( '' );
  94. } );
  95. } );
  96. describe( 'balloon panel form', () => {
  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, 'imageAlternateText', { newValue: form.labeledTextarea.inputView.element.value } );
  102. } );
  103. it( 'should detach panel on cancel', () => {
  104. const spy = sinon.spy( balloonPanel, 'detach' );
  105. form.fire( 'cancel' );
  106. sinon.assert.called( spy );
  107. } );
  108. it( 'should show ImageToolbar on cancel if present', () => {
  109. const editorElement = global.document.createElement( 'div' );
  110. global.document.body.appendChild( editorElement );
  111. return ClassicTestEditor.create( editorElement, {
  112. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  113. image: {
  114. toolbar: [ 'imageAlternateText' ]
  115. }
  116. } )
  117. .then( newEditor => {
  118. newEditor.editing.view.attachDomRoot( editorElement );
  119. const plugin = newEditor.plugins.get( ImageAlternateText );
  120. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  121. const form = plugin.form;
  122. const spy = sinon.spy( toolbarPlugin, 'show' );
  123. form.fire( 'cancel' );
  124. sinon.assert.called( spy );
  125. } );
  126. } );
  127. describe( 'close listeners', () => {
  128. let hidePanelSpy;
  129. beforeEach( () => {
  130. hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
  131. } );
  132. describe( 'keyboard', () => {
  133. it( 'should close after `ESC` press', () => {
  134. balloonPanel.isVisible = true;
  135. const keyCode = keyCodes.esc;
  136. const event = global.document.createEvent( 'Events' );
  137. event.initEvent( 'keydown', true, true );
  138. event.which = keyCode;
  139. event.keyCode = keyCode;
  140. global.document.dispatchEvent( event );
  141. sinon.assert.called( hidePanelSpy );
  142. } );
  143. } );
  144. describe( 'mouse', () => {
  145. it( 'should close on click outside the panel', () => {
  146. balloonPanel.isVisible = true;
  147. global.document.body.dispatchEvent( new Event( 'mouseup', { bubbles: true } ) );
  148. sinon.assert.called( hidePanelSpy );
  149. } );
  150. } );
  151. } );
  152. } );
  153. } );