utils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  6. import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
  7. import { captionElementCreator, isCaption, getCaptionFromImage } from '../../src/imagecaption/utils';
  8. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  9. describe( 'image captioning utils', () => {
  10. let element, document;
  11. beforeEach( () => {
  12. document = new ViewDocument();
  13. const creator = captionElementCreator( document );
  14. element = creator();
  15. } );
  16. describe( 'editableCaptionCreator', () => {
  17. it( 'should create figcatpion editable element', () => {
  18. expect( element ).to.be.instanceOf( ViewEditableElement );
  19. expect( element.name ).to.equal( 'figcaption' );
  20. expect( isCaption( element ) ).to.be.true;
  21. } );
  22. it( 'should be created in context of proper document', () => {
  23. expect( element.document ).to.equal( document );
  24. } );
  25. it( 'should add proper class when element is focused', () => {
  26. element.isFocused = true;
  27. expect( element.hasClass( 'focused' ) ).to.be.true;
  28. element.isFocused = false;
  29. expect( element.hasClass( 'focused' ) ).to.be.false;
  30. } );
  31. } );
  32. describe( 'isCaptionEditable', () => {
  33. it( 'should return true for elements created with creator', () => {
  34. expect( isCaption( element ) ).to.be.true;
  35. } );
  36. it( 'should return false for other elements', () => {
  37. const editable = new ViewEditableElement( 'figcaption', { contenteditable: true } ) ;
  38. editable.document = document;
  39. expect( isCaption( editable ) ).to.be.false;
  40. } );
  41. } );
  42. describe( 'getCaptionFromImage', () => {
  43. it( 'should return caption element from image element', () => {
  44. const dummy = new ModelElement( 'dummy' );
  45. const caption = new ModelElement( 'caption' );
  46. const image = new ModelElement( 'image', null, [ dummy, caption ] );
  47. expect( getCaptionFromImage( image ) ).to.equal( caption );
  48. } );
  49. it( 'should return null when caption element is not present', () => {
  50. const image = new ModelElement( 'image' );
  51. expect( getCaptionFromImage( image ) ).to.be.null;
  52. } );
  53. } );
  54. } );