utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import {
  6. FONT_COLOR,
  7. FONT_BACKGROUND_COLOR,
  8. normalizeColorOptions,
  9. addColorTableToDropdown,
  10. renderDowncastElement
  11. } from './../src/utils';
  12. import { createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  13. import ColorTableView from './../src/ui/colortableview';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. describe( 'utils', () => {
  16. testUtils.createSinonSandbox();
  17. it( 'plugin names has proper values', () => {
  18. expect( FONT_COLOR ).to.equal( 'fontColor' );
  19. expect( FONT_BACKGROUND_COLOR ).to.equal( 'fontBackgroundColor' );
  20. } );
  21. describe( 'normalizeColorOptions()', () => {
  22. it( 'should return normalized config object from string', () => {
  23. const normalizedOption = normalizeColorOptions( [ 'black' ] );
  24. expect( normalizedOption ).to.deep.equal( [
  25. {
  26. model: 'black',
  27. label: 'black',
  28. hasBorder: false,
  29. view: {
  30. name: 'span',
  31. styles: {
  32. color: 'black'
  33. }
  34. }
  35. }
  36. ] );
  37. } );
  38. it( 'should return normalized config object from object( color )', () => {
  39. const normalizedOption = normalizeColorOptions( [ { color: 'black' } ] );
  40. expect( normalizedOption ).to.deep.equal( [
  41. {
  42. model: 'black',
  43. label: 'black',
  44. hasBorder: false,
  45. view: {
  46. name: 'span',
  47. styles: {
  48. color: 'black'
  49. }
  50. }
  51. }
  52. ] );
  53. } );
  54. it( 'should return normalized config object from object( color, label )', () => {
  55. const normalizedOption = normalizeColorOptions( [
  56. {
  57. color: 'black',
  58. label: 'Black'
  59. }
  60. ] );
  61. expect( normalizedOption ).to.deep.equal( [
  62. {
  63. model: 'black',
  64. label: 'Black',
  65. hasBorder: false,
  66. view: {
  67. name: 'span',
  68. styles: {
  69. color: 'black'
  70. }
  71. }
  72. }
  73. ] );
  74. } );
  75. it( 'should return normalized config object from object( color, label, hasBorder )', () => {
  76. const normalizedOption = normalizeColorOptions( [
  77. {
  78. color: 'black',
  79. label: 'Black',
  80. hasBorder: true
  81. }
  82. ] );
  83. expect( normalizedOption ).to.deep.equal( [
  84. {
  85. model: 'black',
  86. label: 'Black',
  87. hasBorder: true,
  88. view: {
  89. name: 'span',
  90. styles: {
  91. color: 'black'
  92. }
  93. }
  94. }
  95. ] );
  96. } );
  97. it( 'should return normalized config object from object( color, hasBorder )', () => {
  98. const normalizedOption = normalizeColorOptions( [
  99. {
  100. color: 'black',
  101. hasBorder: true
  102. }
  103. ] );
  104. expect( normalizedOption ).to.deep.equal( [
  105. {
  106. model: 'black',
  107. label: 'black',
  108. hasBorder: true,
  109. view: {
  110. name: 'span',
  111. styles: {
  112. color: 'black'
  113. }
  114. }
  115. }
  116. ] );
  117. } );
  118. } );
  119. describe( 'addColorTableToDropdown()', () => {
  120. it( 'should create dropdown with color table', () => {
  121. const dropdown = createDropdown();
  122. dropdown.render();
  123. addColorTableToDropdown( {
  124. dropdownView: dropdown,
  125. colors: [
  126. {
  127. label: 'Black',
  128. color: '#000',
  129. options: {
  130. hasBorder: false
  131. }
  132. },
  133. {
  134. label: 'White',
  135. color: '#FFFFFF',
  136. options: {
  137. hasBorder: true
  138. }
  139. }
  140. ],
  141. columns: 2,
  142. removeButtonTooltip: 'Remove Color'
  143. } );
  144. expect( dropdown.colorTableView ).to.be.instanceOf( ColorTableView );
  145. expect( dropdown.panelView.children.length ).to.equal( 1 );
  146. expect( dropdown.colorTableView.element ).to.equal( dropdown.panelView.children.first.element );
  147. } );
  148. } );
  149. describe( 'renderDowncastElement()', () => {
  150. it( 'should create function executes viewWriter with proper arguments', () => {
  151. const downcastViewConverterFn = renderDowncastElement( 'color' );
  152. const fake = testUtils.sinon.fake();
  153. const fakeViewWriter = { createAttributeElement: fake };
  154. downcastViewConverterFn( 'blue', fakeViewWriter );
  155. sinon.assert.calledWithExactly( fake, 'span', { style: 'color:blue' }, { priority: 7 } );
  156. } );
  157. } );
  158. } );