linkimageui.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import LinkImageUI from '../src/linkimageui';
  9. import LinkUI from '../src/linkui';
  10. import LinkCommand from '../src/linkcommand';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  13. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  17. describe( 'LinkImageUI', () => {
  18. let editor, viewDocument, editorElement;
  19. let linkImageComponent, linkButton, actionsView;
  20. let plugin;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. editorElement = document.createElement( 'div' );
  24. document.body.appendChild( editorElement );
  25. return ClassicTestEditor
  26. .create( editorElement, {
  27. plugins: [ LinkImageUI, Paragraph ]
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. viewDocument = editor.editing.view.document;
  32. linkImageComponent = editor.ui.componentFactory.create( 'linkImage' );
  33. plugin = editor.plugins.get( 'LinkImageUI' );
  34. linkButton = plugin.linkButtonView;
  35. actionsView = plugin.actionsView;
  36. linkButton.render();
  37. actionsView.render();
  38. } );
  39. } );
  40. afterEach( () => {
  41. editorElement.remove();
  42. return editor.destroy();
  43. } );
  44. it( 'should be named"', () => {
  45. expect( LinkImageUI.pluginName ).to.equal( 'LinkImageUI' );
  46. } );
  47. describe( 'init()', () => {
  48. it( 'should create #_linkUIPlugin instance of LinkUI', () => {
  49. expect( plugin._linkUIPlugin ).to.be.instanceOf( LinkUI );
  50. } );
  51. it( 'should create #_linkCommand instance of LinkCommand', () => {
  52. expect( plugin._linkCommand ).to.be.instanceOf( LinkCommand );
  53. } );
  54. it( 'should listen to the click event on the images', () => {
  55. const linkPlugin = editor.plugins.get( 'LinkUI' );
  56. const listenToSpy = sinon.stub( linkPlugin, 'listenTo' );
  57. listenToSpy( viewDocument, 'click' );
  58. viewDocument.fire( 'click' );
  59. sinon.assert.calledOnce( listenToSpy );
  60. } );
  61. } );
  62. describe( 'link toolbar UI component', () => {
  63. it( 'should be registered', () => {
  64. expect( linkImageComponent ).to.be.instanceOf( View );
  65. } );
  66. it( 'should have "ck-link-image-options" class', () => {
  67. linkImageComponent.render();
  68. expect( linkImageComponent.element.classList.contains( 'ck-link-image-options' ) ).to.be.true;
  69. } );
  70. it( 'should contain #linkButton and #actionsView', () => {
  71. expect( linkImageComponent.template.children.length ).to.equal( 2 );
  72. expect( linkImageComponent.template.children[ 0 ] ).to.be.instanceOf( ButtonView );
  73. expect( linkImageComponent.template.children[ 0 ] ).equals( linkButton );
  74. expect( linkImageComponent.template.children[ 1 ] ).to.be.instanceOf( View );
  75. expect( linkImageComponent.template.children[ 1 ] ).equals( actionsView );
  76. } );
  77. describe( 'link button', () => {
  78. it( 'should have a toggleable button', () => {
  79. expect( linkButton.isToggleable ).to.be.true;
  80. } );
  81. it( 'should be bound to the link command', () => {
  82. const command = plugin._linkCommand;
  83. command.isEnabled = true;
  84. command.value = 'http://ckeditor.com';
  85. expect( linkButton.isOn ).to.be.true;
  86. expect( linkButton.isEnabled ).to.be.true;
  87. command.isEnabled = false;
  88. command.value = undefined;
  89. expect( linkButton.isOn ).to.be.false;
  90. expect( linkButton.isEnabled ).to.be.false;
  91. } );
  92. it( 'should call #_showUI upon #execute', () => {
  93. const spy = testUtils.sinon.stub( plugin._linkUIPlugin, '_showUI' );
  94. linkButton.fire( 'execute' );
  95. sinon.assert.calledWithExactly( spy, true );
  96. } );
  97. } );
  98. describe( 'actions view', () => {
  99. it( 'should bound "ck-hidden" class to the "isVisible" state', () => {
  100. actionsView.isVisible = true;
  101. expect( actionsView.element.classList.contains( 'ck-hidden' ) ).to.be.false;
  102. actionsView.isVisible = false;
  103. expect( actionsView.element.classList.contains( 'ck-hidden' ) ).to.be.true;
  104. } );
  105. } );
  106. } );
  107. describe( 'click', () => {
  108. it( 'should prevent default behavior if image has "linkHref" attribute', () => {
  109. setModelData( editor.model, '[<image src="" linkHref="https://example.com"></image>]' );
  110. const img = editor.model.document.selection.getSelectedElement();
  111. const data = fakeEventData();
  112. const eventInfo = new EventInfo( img, 'click' );
  113. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  114. viewDocument.fire( 'click', domEventDataMock );
  115. expect( data.preventDefault.called ).to.be.true;
  116. expect( eventInfo.source.name ).to.equal( 'image' );
  117. } );
  118. } );
  119. describe( '#_isImageLinked', () => {
  120. it( 'should return "true" when selected link has "linkHref" attribute', () => {
  121. setModelData( editor.model, '[<image src="" linkHref="https://example.com"></image>]' );
  122. const img = editor.model.document.selection.getSelectedElement();
  123. const spy = sinon.spy( plugin, '_isImageLinked' );
  124. expect( spy( img ) ).to.be.true;
  125. } );
  126. it( 'should return "false" when selected link hasn\'t "linkHref" attribute', () => {
  127. setModelData( editor.model, '[<image src=""></image>]' );
  128. const img = editor.model.document.selection.getSelectedElement();
  129. const spy = sinon.spy( plugin, '_isImageLinked' );
  130. expect( spy( img ) ).to.be.false;
  131. } );
  132. it( 'should return "false" when selected element isn\'t an image', () => {
  133. setModelData( editor.model, '<paragraph>foo</paragraph>' );
  134. const el = editor.model.document.getRoot().getChild( 0 );
  135. const spy = sinon.spy( plugin, '_isImageLinked' );
  136. expect( spy( el ) ).to.be.false;
  137. expect( el.name ).to.not.equal( 'image' );
  138. } );
  139. } );
  140. describe( 'event handling', () => {
  141. it( 'should show #actionsView after "submit"', () => {
  142. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  143. linkUIPlugin.formView.fire( 'submit' );
  144. expect( linkButton.isVisible ).to.be.false;
  145. expect( actionsView.isVisible ).to.be.true;
  146. } );
  147. it( 'should show #actionsView after "cancel"', () => {
  148. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  149. linkUIPlugin.formView.fire( 'cancel' );
  150. expect( linkButton.isVisible ).to.be.false;
  151. expect( actionsView.isVisible ).to.be.true;
  152. } );
  153. it( 'should show #linkButton after "unlink"', () => {
  154. actionsView.fire( 'unlink' );
  155. expect( linkButton.isVisible ).to.be.true;
  156. expect( actionsView.isVisible ).to.be.false;
  157. } );
  158. } );
  159. } );
  160. function fakeEventData() {
  161. return {
  162. preventDefault: sinon.spy()
  163. };
  164. }