config.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Config from '/ckeditor5/core/config.js';
  7. let config;
  8. beforeEach( () => {
  9. config = new Config( {
  10. creator: 'inline',
  11. language: 'pl',
  12. resize: {
  13. minHeight: 300,
  14. maxHeight: 800,
  15. icon: {
  16. path: 'xyz'
  17. }
  18. },
  19. toolbar: 'top'
  20. } );
  21. } );
  22. describe( 'constructor', () => {
  23. it( 'should set configurations', () => {
  24. expect( config ).to.have.property( 'creator' ).to.equal( 'inline' );
  25. expect( config ).to.have.property( 'language' ).to.equal( 'pl' );
  26. expect( config ).to.have.property( 'resize' ).to.have.property( 'minheight' ).to.equal( 300 );
  27. expect( config ).to.have.property( 'resize' ).to.have.property( 'maxheight' ).to.equal( 800 );
  28. expect( config ).to.have.property( 'resize' ).to.have.property( 'icon' )
  29. .to.have.property( 'path' ).to.equal( 'xyz' );
  30. expect( config ).to.have.property( 'toolbar' ).to.equal( 'top' );
  31. } );
  32. it( 'should work with no parameters', () => {
  33. // No error should be thrown.
  34. config = new Config();
  35. } );
  36. } );
  37. describe( 'set', () => {
  38. it( 'should create Config instances for objects', () => {
  39. expect( config.resize ).to.be.an.instanceof( Config );
  40. expect( config.resize.icon ).to.be.an.instanceof( Config );
  41. } );
  42. it( 'should set configurations when passing objects', () => {
  43. config.set( {
  44. option1: 1,
  45. option2: {
  46. subOption21: 21
  47. }
  48. } );
  49. expect( config )
  50. .to.have.property( 'option1' ).to.equal( 1 );
  51. expect( config )
  52. .to.have.property( 'option2' )
  53. .to.have.property( 'suboption21' ).to.equal( 21 );
  54. } );
  55. it( 'should set configurations when passing name and value', () => {
  56. config.set( 'something', 'anything' );
  57. expect( config ).to.have.property( 'something' ).to.equal( 'anything' );
  58. } );
  59. it( 'should set configurations when passing name.with.deep and value', () => {
  60. config.set( 'color.red', 'f00' );
  61. config.set( 'background.color.blue', '00f' );
  62. expect( config )
  63. .to.have.property( 'color' )
  64. .to.have.property( 'red' ).to.equal( 'f00' );
  65. expect( config )
  66. .to.have.property( 'background' )
  67. .to.have.property( 'color' )
  68. .to.have.property( 'blue' ).to.equal( '00f' );
  69. } );
  70. it( 'should override and expand deep configurations', () => {
  71. config.set( {
  72. resize: {
  73. minHeight: 400, // Override
  74. hidden: true, // Expand
  75. icon: {
  76. path: 'abc', // Override
  77. url: true // Expand
  78. }
  79. }
  80. } );
  81. expect( config ).to.have.property( 'resize' );
  82. expect( config.resize ).to.have.property( 'minheight' ).to.equal( 400 );
  83. expect( config.resize ).to.have.property( 'maxheight' ).to.equal( 800 ); // Not touched
  84. expect( config.resize ).to.have.property( 'hidden' ).to.equal( true );
  85. expect( config.resize ).to.have.property( 'icon' );
  86. expect( config.resize.icon ).to.have.property( 'path' ).to.equal( 'abc' );
  87. expect( config.resize.icon ).to.have.property( 'url' ).to.equal( true );
  88. } );
  89. it( 'should replace a simple entry with a Config instance', () => {
  90. config.set( 'test', 1 );
  91. config.set( 'test', {
  92. prop: 1
  93. } );
  94. expect( config.test ).to.be.an.instanceof( Config );
  95. expect( config.test.prop ).to.equal( 1 );
  96. } );
  97. it( 'should replace a simple entry with a Config instance when passing an object', () => {
  98. config.set( 'test', 1 );
  99. config.set( {
  100. test: {
  101. prop: 1
  102. }
  103. } );
  104. expect( config.test ).to.be.an.instanceof( Config );
  105. expect( config.test.prop ).to.equal( 1 );
  106. } );
  107. it( 'should replace a simple entry with a Config instance when passing a name.with.deep', () => {
  108. config.set( 'test.prop', 1 );
  109. config.set( 'test.prop.value', 1 );
  110. expect( config.test ).to.be.an.instanceof( Config );
  111. expect( config.test.prop ).to.be.an.instanceof( Config );
  112. expect( config.test.prop.value ).to.equal( 1 );
  113. } );
  114. it( 'should not create Config instances for non-pure objects', () => {
  115. function SomeClass() {}
  116. config.set( 'date', new Date() );
  117. config.set( {
  118. instance: new SomeClass()
  119. } );
  120. expect( config.date ).to.be.an.instanceof( Date );
  121. expect( config.instance ).to.be.an.instanceof( SomeClass );
  122. } );
  123. it( 'should set `null` for undefined value', () => {
  124. config.set( 'test' );
  125. expect( config.test ).to.be.null();
  126. expect( config.get( 'test' ) ).to.be.null();
  127. } );
  128. } );
  129. describe( 'get', () => {
  130. it( 'should retrieve a configuration', () => {
  131. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  132. } );
  133. it( 'should retrieve a deep configuration', () => {
  134. expect( config.get( 'resize.minheight' ) ).to.equal( 300 );
  135. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  136. } );
  137. it( 'should retrieve a subset of the configuration', () => {
  138. let resizeConfig = config.get( 'resize' );
  139. expect( resizeConfig ).to.have.property( 'minheight' ).to.equal( 300 );
  140. expect( resizeConfig ).to.have.property( 'maxheight' ).to.equal( 800 );
  141. expect( resizeConfig ).to.have.property( 'icon' ).to.have.property( 'path' ).to.equal( 'xyz' );
  142. let iconConfig = resizeConfig.get( 'icon' );
  143. expect( iconConfig ).to.have.property( 'path' ).to.equal( 'xyz' );
  144. } );
  145. it( 'should retrieve values case-insensitively', () => {
  146. expect( config.get( 'Creator' ) ).to.equal( 'inline' );
  147. expect( config.get( 'CREATOR' ) ).to.equal( 'inline' );
  148. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  149. expect( config.get( 'resize.MINHEIGHT' ) ).to.equal( 300 );
  150. } );
  151. it( 'should return undefined for non existing configuration', () => {
  152. expect( config.get( 'invalid' ) ).to.be.undefined();
  153. } );
  154. it( 'should return undefined for non existing deep configuration', () => {
  155. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined();
  156. } );
  157. } );
  158. describe( 'define', () => {
  159. it( 'should create the definition property', () => {
  160. expect( config ).to.not.have.property( 'definition' );
  161. config.define( 'test', 1 );
  162. expect( config ).to.have.property( 'definition' );
  163. } );
  164. it( 'should set configurations in the definition property', () => {
  165. config.define( 'test1', 1 );
  166. // This is for Code Coverage to ensure that it works when `definition` is already defined.
  167. config.define( 'test2', 2 );
  168. expect( config.definition ).to.have.property( 'test1' ).to.equal( 1 );
  169. expect( config.definition ).to.have.property( 'test2' ).to.equal( 2 );
  170. } );
  171. it( 'should set configurations passed as object in the definition property', () => {
  172. config.define( {
  173. test: 1
  174. } );
  175. expect( config.definition ).to.have.property( 'test' ).to.equal( 1 );
  176. } );
  177. it( 'should not define main config properties but still be retrieved with get()', () => {
  178. config.define( 'test', 1 );
  179. expect( config ).to.not.have.property( 'test' );
  180. expect( config.get( 'test' ) ).to.equal( 1 );
  181. } );
  182. it( 'should be overridden by set()', () => {
  183. config.define( 'test', 1 );
  184. config.set( 'test', 2 );
  185. expect( config ).to.have.property( 'test' ).to.equal( 2 );
  186. expect( config.get( 'test' ) ).to.equal( 2 );
  187. } );
  188. } );