imagealternatetext.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 ImageAlternateText from '../../src/imagealternatetext/imagealternatetext';
  9. import ImageAlternateTextEngine from '../../src/imagealternatetext/imagealternatetextengine';
  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. describe( 'ImageAlternateText', () => {
  15. let editor, plugin, command, balloonPanel, form;
  16. beforeEach( () => {
  17. const editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicTestEditor.create( editorElement, {
  20. plugins: [ ImageAlternateText, Image ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. newEditor.editing.view.attachDomRoot( editorElement );
  25. plugin = editor.plugins.get( ImageAlternateText );
  26. command = editor.commands.get( 'imageAlternateText' );
  27. balloonPanel = plugin.balloonPanel;
  28. form = plugin.form;
  29. } );
  30. } );
  31. afterEach( () => {
  32. return editor.destroy();
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( plugin ).to.be.instanceOf( ImageAlternateText );
  36. } );
  37. it( 'should load ImageAlternateTextEngine plugin', () => {
  38. expect( editor.plugins.get( ImageAlternateTextEngine ) ).to.be.instanceOf( ImageAlternateTextEngine );
  39. } );
  40. describe( 'toolbar button', () => {
  41. let button;
  42. beforeEach( () => {
  43. button = editor.ui.componentFactory.create( 'imageAlternateText' );
  44. } );
  45. it( 'should be registered in component factory', () => {
  46. expect( button ).to.be.instanceOf( ButtonView );
  47. } );
  48. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  49. command.isEnabled = true;
  50. expect( button.isEnabled ).to.be.true;
  51. command.isEnabled = false;
  52. expect( button.isEnabled ).to.be.false;
  53. } );
  54. it( 'should show balloon panel on execute', () => {
  55. const spy = sinon.spy( balloonPanel, 'attach' );
  56. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  57. button.fire( 'execute' );
  58. sinon.assert.calledOnce( spy );
  59. } );
  60. it( 'should hide ImageToolbar on execute', () => {
  61. const editorElement = global.document.createElement( 'div' );
  62. global.document.body.appendChild( editorElement );
  63. return ClassicTestEditor.create( editorElement, {
  64. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  65. image: {
  66. toolbar: [ 'imageAlternateText' ]
  67. }
  68. } )
  69. .then( newEditor => {
  70. newEditor.editing.view.attachDomRoot( editorElement );
  71. const button = newEditor.ui.componentFactory.create( 'imageAlternateText' );
  72. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  73. const spy = sinon.spy( toolbarPlugin, 'hide' );
  74. setData( newEditor.document, '[<image src="" alt="foo bar"></image>]' );
  75. button.fire( 'execute' );
  76. sinon.assert.calledOnce( spy );
  77. newEditor.destroy();
  78. } );
  79. } );
  80. it( 'should set alt attribute value to textarea and select it', () => {
  81. const spy = sinon.spy( form.labeledTextarea, 'select' );
  82. setData( editor.document, '[<image src="" alt="foo bar"></image>]' );
  83. button.fire( 'execute' );
  84. sinon.assert.calledOnce( spy );
  85. expect( plugin.form.labeledTextarea.value ).equals( 'foo bar' );
  86. } );
  87. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  88. const spy = sinon.spy( form.labeledTextarea, 'select' );
  89. setData( editor.document, '[<image src=""></image>]' );
  90. button.fire( 'execute' );
  91. sinon.assert.calledOnce( spy );
  92. expect( plugin.form.labeledTextarea.value ).equals( '' );
  93. } );
  94. } );
  95. describe( 'balloon panel form', () => {
  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, 'imageAlternateText', { newValue: form.labeledTextarea.inputView.element.value } );
  101. } );
  102. it( 'should detach panel on cancel', () => {
  103. const spy = sinon.spy( balloonPanel, 'detach' );
  104. form.fire( 'cancel' );
  105. sinon.assert.called( spy );
  106. } );
  107. it( 'should show ImageToolbar on cancel if present', () => {
  108. const editorElement = global.document.createElement( 'div' );
  109. global.document.body.appendChild( editorElement );
  110. return ClassicTestEditor.create( editorElement, {
  111. plugins: [ ImageAlternateText, Image, ImageToolbar ],
  112. image: {
  113. toolbar: [ 'imageAlternateText' ]
  114. }
  115. } )
  116. .then( newEditor => {
  117. newEditor.editing.view.attachDomRoot( editorElement );
  118. const plugin = newEditor.plugins.get( ImageAlternateText );
  119. const toolbarPlugin = newEditor.plugins.get( ImageToolbar );
  120. const form = plugin.form;
  121. const spy = sinon.spy( toolbarPlugin, 'show' );
  122. form.fire( 'cancel' );
  123. sinon.assert.called( spy );
  124. } );
  125. } );
  126. describe( 'close listeners', () => {
  127. let hidePanelSpy;
  128. beforeEach( () => {
  129. hidePanelSpy = sinon.spy( balloonPanel, 'detach' );
  130. } );
  131. describe( 'keyboard', () => {
  132. it( 'should close after `ESC` press', () => {
  133. balloonPanel.isVisible = true;
  134. const keyCode = keyCodes.esc;
  135. const event = global.document.createEvent( 'Events' );
  136. event.initEvent( 'keydown', true, true );
  137. event.which = keyCode;
  138. event.keyCode = keyCode;
  139. global.document.dispatchEvent( event );
  140. sinon.assert.called( hidePanelSpy );
  141. } );
  142. } );
  143. } );
  144. } );
  145. } );