8
0

utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import log from '@ckeditor/ckeditor5-utils/src/log';
  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. testUtils.createSinonSandbox();
  13. describe( 'ImageStyle utils', () => {
  14. let imageStyles;
  15. describe( 'imageStyles()', () => {
  16. describe( 'object format', () => {
  17. beforeEach( () => {
  18. imageStyles = normalizeImageStyles( [
  19. { name: 'foo', title: 'foo', icon: 'custom', isDefault: true, className: 'foo-class' },
  20. { name: 'bar', title: 'bar', icon: 'right', className: 'bar-class' },
  21. { name: 'baz', title: 'Side image', icon: 'custom', className: 'baz-class' },
  22. // Customized default styles.
  23. { name: 'imageStyleFull', icon: 'left', title: 'Custom title' }
  24. ] );
  25. } );
  26. it( 'should pass through if #name not found in default styles', () => {
  27. expect( imageStyles[ 0 ] ).to.deep.equal( {
  28. name: 'foo',
  29. title: 'foo',
  30. icon: 'custom',
  31. isDefault: true,
  32. className: 'foo-class'
  33. } );
  34. } );
  35. it( 'should use one of default icons if #icon matches', () => {
  36. expect( imageStyles[ 1 ].icon ).to.equal( rightIcon );
  37. } );
  38. it( 'should extend one of default styles if #name matches', () => {
  39. expect( imageStyles[ 3 ] ).to.deep.equal( {
  40. name: 'imageStyleFull',
  41. title: 'Custom title',
  42. icon: leftIcon,
  43. isDefault: true
  44. } );
  45. } );
  46. } );
  47. describe( 'string format', () => {
  48. it( 'should use one of default styles if #name matches', () => {
  49. expect( normalizeImageStyles( [ 'imageStyleFull' ] ) ).to.deep.equal( [ {
  50. name: 'imageStyleFull',
  51. title: 'Full size image',
  52. icon: fullWidthIcon,
  53. isDefault: true
  54. } ] );
  55. expect( normalizeImageStyles( [ 'imageStyleSide' ] ) ).to.deep.equal( [ {
  56. name: 'imageStyleSide',
  57. title: 'Side image',
  58. icon: rightIcon,
  59. className: 'image-style-side'
  60. } ] );
  61. expect( normalizeImageStyles( [ 'imageStyleAlignLeft' ] ) ).to.deep.equal( [ {
  62. name: 'imageStyleAlignLeft',
  63. title: 'Left aligned image',
  64. icon: leftIcon,
  65. className: 'image-style-align-left'
  66. } ] );
  67. expect( normalizeImageStyles( [ 'imageStyleAlignCenter' ] ) ).to.deep.equal( [ {
  68. name: 'imageStyleAlignCenter',
  69. title: 'Centered image',
  70. icon: centerIcon,
  71. className: 'image-style-align-center'
  72. } ] );
  73. expect( normalizeImageStyles( [ 'imageStyleAlignRight' ] ) ).to.deep.equal( [ {
  74. name: 'imageStyleAlignRight',
  75. title: 'Right aligned image',
  76. icon: rightIcon,
  77. className: 'image-style-align-right'
  78. } ] );
  79. } );
  80. it( 'should warn if a #name not found in default styles', () => {
  81. testUtils.sinon.stub( log, 'warn' );
  82. expect( normalizeImageStyles( [ 'foo' ] ) ).to.deep.equal( [ {
  83. name: 'foo'
  84. } ] );
  85. sinon.assert.calledOnce( log.warn );
  86. sinon.assert.calledWithExactly( log.warn,
  87. sinon.match( /^image-style-not-found/ ),
  88. { name: 'foo' }
  89. );
  90. } );
  91. } );
  92. } );
  93. } );