utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. import { normalizeOptions } from '../../src/fontsize/utils';
  6. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  7. describe( 'FontSizeEditing Utils', () => {
  8. describe( 'normalizeOptions()', () => {
  9. it( 'should discard unsupported values', () => {
  10. expect( normalizeOptions( [ () => {}, 'default', 'unknown' ] ) ).to.deep.equal( [ { title: 'Default', model: undefined } ] );
  11. } );
  12. it( 'should pass through object definition', () => {
  13. expect( normalizeOptions( [ {
  14. title: 'My Size',
  15. model: 'my-size',
  16. view: { name: 'span', styles: 'font-size: 12em;', priority: 7 }
  17. } ] ) ).to.deep.equal( [
  18. {
  19. title: 'My Size',
  20. model: 'my-size',
  21. view: { name: 'span', styles: 'font-size: 12em;', priority: 7 }
  22. }
  23. ] );
  24. } );
  25. describe( 'named presets', () => {
  26. it( 'should return defined presets', () => {
  27. expect( normalizeOptions( [ 'tiny', 'small', 'default', 'big', 'huge' ] ) ).to.deep.equal( [
  28. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 7 } },
  29. { title: 'Small', model: 'small', view: { name: 'span', classes: 'text-small', priority: 7 } },
  30. { title: 'Default', model: undefined },
  31. { title: 'Big', model: 'big', view: { name: 'span', classes: 'text-big', priority: 7 } },
  32. { title: 'Huge', model: 'huge', view: { name: 'span', classes: 'text-huge', priority: 7 } }
  33. ] );
  34. } );
  35. it( 'should add "view" definition if missing', () => {
  36. const tinyOption = {
  37. title: 'Tiny',
  38. model: 'tiny'
  39. };
  40. expect( normalizeOptions( [ tinyOption ] ) ).to.deep.equal( [
  41. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 7 } }
  42. ] );
  43. } );
  44. it( 'should add "view.priority" to returned definition if missing', () => {
  45. const tinyOption = {
  46. title: 'Tiny',
  47. model: 'tiny',
  48. view: {
  49. name: 'span',
  50. classes: 'text-tiny'
  51. }
  52. };
  53. expect( normalizeOptions( [ tinyOption ] ) ).to.deep.equal( [
  54. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 7 } }
  55. ] );
  56. } );
  57. it( 'should not modify "view.priority" if already specified', () => {
  58. const tinyOption = {
  59. title: 'Tiny',
  60. model: 'tiny',
  61. view: {
  62. name: 'span',
  63. classes: 'text-tiny',
  64. priority: 10
  65. }
  66. };
  67. expect( normalizeOptions( [ tinyOption ] ) ).to.deep.equal( [
  68. { title: 'Tiny', model: 'tiny', view: { name: 'span', classes: 'text-tiny', priority: 10 } }
  69. ] );
  70. } );
  71. } );
  72. describe( 'numerical presets', () => {
  73. it( 'should return generated presets', () => {
  74. expect( normalizeOptions( [ '10', '12', 'default', '14.1', 18.3 ] ) ).to.deep.equal( [
  75. { title: '10', model: '10px', view: { name: 'span', styles: { 'font-size': '10px' }, priority: 7 } },
  76. { title: '12', model: '12px', view: { name: 'span', styles: { 'font-size': '12px' }, priority: 7 } },
  77. { title: 'Default', model: undefined },
  78. { title: '14.1', model: '14.1px', view: { name: 'span', styles: { 'font-size': '14.1px' }, priority: 7 } },
  79. { title: '18.3', model: '18.3px', view: { name: 'span', styles: { 'font-size': '18.3px' }, priority: 7 } }
  80. ] );
  81. } );
  82. it( 'should add "view" definition if missing', () => {
  83. const numericOption = {
  84. title: '18',
  85. model: '18px'
  86. };
  87. expect( normalizeOptions( [ numericOption ] ) ).to.deep.equal( [
  88. { title: '18', model: '18px', view: { name: 'span', styles: { 'font-size': '18px' }, priority: 7 } }
  89. ] );
  90. } );
  91. it( 'should discard incomprehensible value', () => {
  92. const numericOption = {
  93. title: '18',
  94. model: 'unknown'
  95. };
  96. expect( normalizeOptions( [ numericOption ] ) ).to.deep.equal( [] );
  97. } );
  98. it( 'should add "view.priority" to returned definition if missing', () => {
  99. const numericOption = {
  100. title: '18',
  101. model: '18px',
  102. view: {
  103. name: 'span',
  104. styles: { 'font-size': '18px' }
  105. }
  106. };
  107. expect( normalizeOptions( [ numericOption ] ) ).to.deep.equal( [
  108. { title: '18', model: '18px', view: { name: 'span', styles: { 'font-size': '18px' }, priority: 7 } }
  109. ] );
  110. } );
  111. it( 'should not modify "view.priority" if already specified', () => {
  112. const numericOption = {
  113. title: '18',
  114. model: '18px',
  115. view: {
  116. name: 'span',
  117. styles: { 'font-size': '18px' },
  118. priority: 10
  119. }
  120. };
  121. expect( normalizeOptions( [ numericOption ] ) ).to.deep.equal( [
  122. { title: '18', model: '18px', view: { name: 'span', styles: { 'font-size': '18px' }, priority: 10 } }
  123. ] );
  124. } );
  125. it( 'should throw an error if definition misses "model" value', () => {
  126. const definition = {
  127. title: '18'
  128. };
  129. expectToThrowCKEditorError( () => {
  130. normalizeOptions( [ definition ] );
  131. }, /font-size-invalid-definition/, null, definition );
  132. } );
  133. } );
  134. } );
  135. } );