image.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window */
  6. import ClassicTestEditor from 'tests/core/_utils/classictesteditor.js';
  7. import Image from 'ckeditor5/image/image.js';
  8. import Paragraph from 'ckeditor5/paragraph/paragraph.js';
  9. import ImageEngine from 'ckeditor5/image/imageengine.js';
  10. import MouseDownObserver from 'ckeditor5/image/mousedownobserver.js';
  11. import { getData as getModelData } from 'ckeditor5/engine/dev-utils/model.js';
  12. describe( 'Image', () => {
  13. let editor, document, viewDocument, imageFeature;
  14. beforeEach( () => {
  15. const editorElement = window.document.createElement( 'div' );
  16. window.document.body.appendChild( editorElement );
  17. return ClassicTestEditor.create( editorElement, {
  18. features: [ Image, Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. document = editor.document;
  23. viewDocument = editor.editing.view;
  24. imageFeature = editor.plugins.get( Image );
  25. } );
  26. } );
  27. it( 'should be loaded', () => {
  28. expect( imageFeature ).to.instanceOf( Image );
  29. } );
  30. it( 'should load ImageEngine', () => {
  31. expect( editor.plugins.get( ImageEngine ) ).to.instanceOf( ImageEngine );
  32. } );
  33. it( 'should add MouseDownObserver', () => {
  34. expect( viewDocument.getObserver( MouseDownObserver ) ).to.be.instanceOf( MouseDownObserver );
  35. } );
  36. it( 'should create selection in model on mousedown event', () => {
  37. editor.setData( '<figure class="image"><img src="image.png" alt="alt text" /></figure>' );
  38. const imageWidget = viewDocument.getRoot().getChild( 0 );
  39. const domEventDataMock = {
  40. target: imageWidget,
  41. preventDefault: sinon.spy()
  42. };
  43. viewDocument.fire( 'mousedown', domEventDataMock );
  44. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  45. expect( getModelData( document ) ).to.equal( '[<image alt="alt text" src="image.png"></image>]' );
  46. } );
  47. it( 'should do not change model if mousedown event is not on image', () => {
  48. editor.setData( '<p>foo bar</p><figure class="image"><img src="image.png" alt="alt text" /></figure>' );
  49. const paragraph = viewDocument.getRoot().getChild( 0 );
  50. const domEventDataMock = {
  51. target: paragraph,
  52. preventDefault: sinon.spy()
  53. };
  54. viewDocument.fire( 'mousedown', domEventDataMock );
  55. sinon.assert.notCalled( domEventDataMock.preventDefault );
  56. expect( getModelData( document ) )
  57. .to.equal( '<paragraph>[]foo bar</paragraph><image alt="alt text" src="image.png"></image>' );
  58. } );
  59. it( 'should not focus editable if already is focused', () => {
  60. editor.setData( '<figure class="image"><img src="image.png" alt="alt text" /></figure>' );
  61. const imageWidget = viewDocument.getRoot().getChild( 0 );
  62. const domEventDataMock = {
  63. target: imageWidget,
  64. preventDefault: sinon.spy()
  65. };
  66. const focusSpy = sinon.spy( viewDocument, 'focus' );
  67. viewDocument.isFocused = true;
  68. viewDocument.fire( 'mousedown', domEventDataMock );
  69. sinon.assert.calledOnce( domEventDataMock.preventDefault );
  70. sinon.assert.notCalled( focusSpy );
  71. expect( getModelData( document ) ).to.equal( '[<image alt="alt text" src="image.png"></image>]' );
  72. } );
  73. } );