linkimageui.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  8. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  9. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import LinkImageUI from '../src/linkimageui';
  14. describe( 'LinkImageUI', () => {
  15. let editor, viewDocument, editorElement;
  16. let plugin, linkButtonComponent, linkButton;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ LinkImageUI, Paragraph ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. viewDocument = editor.editing.view.document;
  28. linkButtonComponent = editor.ui.componentFactory.create( 'linkImage' );
  29. plugin = editor.plugins.get( 'LinkImageUI' );
  30. linkButton = plugin.linkButtonView;
  31. } );
  32. } );
  33. afterEach( () => {
  34. editorElement.remove();
  35. return editor.destroy();
  36. } );
  37. it( 'should be named"', () => {
  38. expect( LinkImageUI.pluginName ).to.equal( 'LinkImageUI' );
  39. } );
  40. describe( 'init()', () => {
  41. it( 'should listen to the click event on the images', () => {
  42. const linkPlugin = editor.plugins.get( 'LinkUI' );
  43. const listenToSpy = sinon.stub( linkPlugin, 'listenTo' );
  44. listenToSpy( viewDocument, 'click' );
  45. viewDocument.fire( 'click' );
  46. sinon.assert.calledOnce( listenToSpy );
  47. } );
  48. } );
  49. describe( 'link toolbar UI component', () => {
  50. it( 'should be registered', () => {
  51. expect( linkButtonComponent ).to.be.instanceOf( ButtonView );
  52. } );
  53. describe( 'link button', () => {
  54. it( 'should have a toggleable button', () => {
  55. expect( linkButton.isToggleable ).to.be.true;
  56. } );
  57. it( 'should be bound to the link command', () => {
  58. const command = editor.commands.get( 'link' );
  59. command.isEnabled = true;
  60. command.value = 'http://ckeditor.com';
  61. expect( linkButton.isOn ).to.be.true;
  62. expect( linkButton.isEnabled ).to.be.true;
  63. command.isEnabled = false;
  64. command.value = undefined;
  65. expect( linkButton.isOn ).to.be.false;
  66. expect( linkButton.isEnabled ).to.be.false;
  67. } );
  68. it( 'should call #_showUI upon #execute', () => {
  69. const spy = testUtils.sinon.stub( editor.plugins.get( 'LinkUI' ), '_showUI' );
  70. linkButton.fire( 'execute' );
  71. sinon.assert.calledWithExactly( spy, true );
  72. } );
  73. } );
  74. } );
  75. describe( 'click', () => {
  76. it( 'should prevent default behavior if image has "linkHref" attribute', () => {
  77. setModelData( editor.model, '[<image src="" linkHref="https://example.com"></image>]' );
  78. const img = editor.model.document.selection.getSelectedElement();
  79. const data = fakeEventData();
  80. const eventInfo = new EventInfo( img, 'click' );
  81. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  82. viewDocument.fire( 'click', domEventDataMock );
  83. expect( data.preventDefault.called ).to.be.true;
  84. expect( eventInfo.source.name ).to.equal( 'image' );
  85. } );
  86. } );
  87. describe( 'event handling', () => {
  88. it( 'should show plugin#actionsView after "execute" if image is already linked', () => {
  89. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  90. setModelData( editor.model, '[<image src="" linkHref="https://example.com"></image>]' );
  91. linkButton.fire( 'execute' );
  92. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.actionsView );
  93. } );
  94. it( 'should show plugin#formView after "execute" if image is not linked', () => {
  95. const linkUIPlugin = editor.plugins.get( 'LinkUI' );
  96. setModelData( editor.model, '[<image src=""></image>]' );
  97. linkButton.fire( 'execute' );
  98. expect( linkUIPlugin._balloon.visibleView ).to.equals( linkUIPlugin.formView );
  99. } );
  100. } );
  101. } );
  102. function fakeEventData() {
  103. return {
  104. preventDefault: sinon.spy()
  105. };
  106. }