imagetextalternative.js 6.3 KB

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