utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 console */
  6. import fullWidthIcon from '@ckeditor/ckeditor5-core/theme/icons/object-full-width.svg';
  7. import leftIcon from '@ckeditor/ckeditor5-core/theme/icons/object-left.svg';
  8. import centerIcon from '@ckeditor/ckeditor5-core/theme/icons/object-center.svg';
  9. import rightIcon from '@ckeditor/ckeditor5-core/theme/icons/object-right.svg';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import { normalizeImageStyles } from '../../src/imagestyle/utils';
  12. describe( 'ImageStyle utils', () => {
  13. let imageStyles;
  14. testUtils.createSinonSandbox();
  15. describe( 'normalizeImageStyles()', () => {
  16. // Since this function is all about normalizing the config object, make sure it doesn't throw
  17. // if the config is empty (which may happen e.g. if only ImageStyleUI was loaded).
  18. it( 'does not throw when given undefined', () => {
  19. expect( normalizeImageStyles() ).to.deep.equal( [] );
  20. } );
  21. describe( 'object format', () => {
  22. beforeEach( () => {
  23. imageStyles = normalizeImageStyles( [
  24. { name: 'foo', title: 'foo', icon: 'custom', isDefault: true, className: 'foo-class' },
  25. { name: 'bar', title: 'bar', icon: 'right', className: 'bar-class' },
  26. { name: 'baz', title: 'Side image', icon: 'custom', className: 'baz-class' },
  27. // Customized default styles.
  28. { name: 'full', icon: 'left', title: 'Custom title' }
  29. ] );
  30. } );
  31. it( 'should pass through if #name not found in default styles', () => {
  32. expect( imageStyles[ 0 ] ).to.deep.equal( {
  33. name: 'foo',
  34. title: 'foo',
  35. icon: 'custom',
  36. isDefault: true,
  37. className: 'foo-class'
  38. } );
  39. } );
  40. it( 'should use one of default icons if #icon matches', () => {
  41. expect( imageStyles[ 1 ].icon ).to.equal( rightIcon );
  42. } );
  43. it( 'should extend one of default styles if #name matches', () => {
  44. expect( imageStyles[ 3 ] ).to.deep.equal( {
  45. name: 'full',
  46. title: 'Custom title',
  47. icon: leftIcon,
  48. isDefault: true
  49. } );
  50. } );
  51. } );
  52. describe( 'string format', () => {
  53. it( 'should use one of default styles if #name matches', () => {
  54. expect( normalizeImageStyles( [ 'full' ] ) ).to.deep.equal( [ {
  55. name: 'full',
  56. title: 'Full size image',
  57. icon: fullWidthIcon,
  58. isDefault: true
  59. } ] );
  60. expect( normalizeImageStyles( [ 'side' ] ) ).to.deep.equal( [ {
  61. name: 'side',
  62. title: 'Side image',
  63. icon: rightIcon,
  64. className: 'image-style-side'
  65. } ] );
  66. expect( normalizeImageStyles( [ 'alignLeft' ] ) ).to.deep.equal( [ {
  67. name: 'alignLeft',
  68. title: 'Left aligned image',
  69. icon: leftIcon,
  70. className: 'image-style-align-left'
  71. } ] );
  72. expect( normalizeImageStyles( [ 'alignCenter' ] ) ).to.deep.equal( [ {
  73. name: 'alignCenter',
  74. title: 'Centered image',
  75. icon: centerIcon,
  76. className: 'image-style-align-center'
  77. } ] );
  78. expect( normalizeImageStyles( [ 'alignRight' ] ) ).to.deep.equal( [ {
  79. name: 'alignRight',
  80. title: 'Right aligned image',
  81. icon: rightIcon,
  82. className: 'image-style-align-right'
  83. } ] );
  84. } );
  85. it( 'should warn if a #name not found in default styles', () => {
  86. testUtils.sinon.stub( console, 'warn' );
  87. expect( normalizeImageStyles( [ 'foo' ] ) ).to.deep.equal( [ {
  88. name: 'foo'
  89. } ] );
  90. sinon.assert.calledOnce( console.warn );
  91. sinon.assert.calledWithExactly( console.warn,
  92. sinon.match( /^image-style-not-found/ ),
  93. { name: 'foo' },
  94. sinon.match.string // Link to the documentation
  95. );
  96. } );
  97. } );
  98. } );
  99. } );