utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
  6. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  7. import { toImageWidget, isImageWidget, isImage } from '../src/utils';
  8. import { isWidget, getFakeSelectionLabel } from '../src/widget/utils';
  9. describe( 'image widget utils', () => {
  10. let element, image;
  11. beforeEach( () => {
  12. image = new ViewElement( 'img' );
  13. element = new ViewElement( 'figure', null, image );
  14. toImageWidget( element, ( t ) => t );
  15. } );
  16. describe( 'toImageWidget()', () => {
  17. it( 'should be widgetized', () => {
  18. expect( isWidget( element ) ).to.be.true;
  19. } );
  20. it( 'should set fake selection label', () => {
  21. expect( getFakeSelectionLabel( element ) ).to.equal( 'image widget' );
  22. } );
  23. it( 'should set fake selection label combined with alt attribute', () => {
  24. image.setAttribute( 'alt', 'foo bar baz' );
  25. expect( getFakeSelectionLabel( element ) ).to.equal( 'foo bar baz image widget' );
  26. } );
  27. } );
  28. describe( 'isImageWidget()', () => {
  29. it( 'should return true for elements marked with toImageWidget()', () => {
  30. expect( isImageWidget( element ) ).to.be.true;
  31. } );
  32. it( 'should return false for non-widgetized elements', () => {
  33. expect( isImageWidget( new ViewElement( 'p' ) ) ).to.be.false;
  34. } );
  35. } );
  36. describe( 'isImage', () => {
  37. it( 'should return true for image element', () => {
  38. const image = new ModelElement( 'image' );
  39. expect( isImage( image ) ).to.be.true;
  40. } );
  41. it( 'should return true false for different elements', () => {
  42. const image = new ModelElement( 'foo' );
  43. expect( isImage( image ) ).to.be.false;
  44. } );
  45. it( 'should return true false for null and undefined', () => {
  46. expect( isImage( null ) ).to.be.false;
  47. expect( isImage( undefined ) ).to.be.false;
  48. } );
  49. } );
  50. } );