config.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Config from 'ckeditor5/utils/config.js';
  6. describe( 'Config', () => {
  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.get( 'creator' ) ).to.equal( 'inline' );
  25. expect( config.get( 'language' ) ).to.equal( 'pl' );
  26. expect( config.get( 'resize' ) ).to.deep.equal( {
  27. minHeight: 300,
  28. maxHeight: 800,
  29. icon: {
  30. path: 'xyz'
  31. }
  32. } );
  33. expect( config.get( 'toolbar' ) ).to.equal( 'top' );
  34. } );
  35. it( 'should work with no parameters', () => {
  36. // No error should be thrown.
  37. config = new Config();
  38. } );
  39. } );
  40. describe( 'set', () => {
  41. it( 'should set configurations when passing objects', () => {
  42. config.set( {
  43. option1: 1,
  44. option2: {
  45. subOption21: 21
  46. }
  47. } );
  48. expect( config.get( 'option1' ) ).to.equal( 1 );
  49. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  50. } );
  51. it( 'should set configurations when passing name and value', () => {
  52. config.set( 'something', 'anything' );
  53. expect( config.get( 'something' ) ).to.equal( 'anything' );
  54. } );
  55. it( 'should set configurations when passing name.with.deep and value', () => {
  56. config.set( 'color.red', 'f00' );
  57. config.set( 'background.color.blue', '00f' );
  58. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  59. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  60. } );
  61. it( 'should replace a simple entry with an object', () => {
  62. config.set( 'test', 1 );
  63. config.set( 'test', {
  64. prop: 1
  65. } );
  66. expect( config.get( 'test' ) ).to.be.an( 'object' );
  67. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  68. } );
  69. it( 'should replace a simple entry with an object when passing only object', () => {
  70. config.set( 'test', 1 );
  71. config.set( {
  72. test: {
  73. prop: 1
  74. }
  75. } );
  76. expect( config.get( 'test' ) ).to.be.an( 'object' );
  77. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  78. } );
  79. it( 'should replace a simple entry with an object when passing a name.with.deep', () => {
  80. config.set( 'test.prop', 1 );
  81. config.set( 'test.prop.value', 1 );
  82. expect( config.get( 'test' ) ).to.be.an( 'object' );
  83. expect( config.get( 'test.prop' ) ).to.be.an( 'object' );
  84. expect( config.get( 'test.prop.value' ) ).to.equal( 1 );
  85. } );
  86. it( 'should override and expand deep configurations', () => {
  87. config.set( {
  88. resize: {
  89. minHeight: 400, // Override
  90. hidden: true, // Expand
  91. icon: {
  92. path: 'abc', // Override
  93. url: true // Expand
  94. }
  95. }
  96. } );
  97. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  98. minHeight: 400, // Overridden
  99. maxHeight: 800, // The same
  100. hidden: true, // Expanded
  101. icon: {
  102. path: 'abc', // Overridden
  103. url: true // Expanded
  104. }
  105. } );
  106. } );
  107. it( 'should override and expand object when passing an object', () => {
  108. config.set( 'resize', {
  109. minHeight: 400, // Override
  110. hidden: true, // Expand
  111. icon: {
  112. path: 'abc', // Override
  113. url: true // Expand
  114. }
  115. } );
  116. expect( config.get( 'resize' ) ).to.deep.equal( {
  117. minHeight: 400, // Overridden
  118. maxHeight: 800, // The same
  119. hidden: true, // Expanded
  120. icon: {
  121. path: 'abc', // Overridden
  122. url: true // Expanded
  123. }
  124. } );
  125. } );
  126. it( 'should not create object for non-pure objects', () => {
  127. function SomeClass() {}
  128. config.set( 'date', new Date() );
  129. config.set( {
  130. instance: new SomeClass()
  131. } );
  132. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  133. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  134. } );
  135. } );
  136. describe( 'define', () => {
  137. it( 'should set configurations when passing objects', () => {
  138. config.set( {
  139. option1: 1,
  140. option2: {
  141. subOption21: 21
  142. }
  143. } );
  144. expect( config.get( 'option1' ) ).to.equal( 1 );
  145. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  146. } );
  147. it( 'should set configurations when passing name and value', () => {
  148. config.set( 'something', 'anything' );
  149. expect( config.get( 'something' ) ).to.equal( 'anything' );
  150. } );
  151. it( 'should set configurations when passing name.with.deep and value', () => {
  152. config.set( 'color.red', 'f00' );
  153. config.set( 'background.color.blue', '00f' );
  154. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  155. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  156. } );
  157. it( 'should not replace already defined values', () => {
  158. config.define( 'language', 'en' );
  159. config.define( 'resize.minHeight', 400 );
  160. config.define( 'resize.icon', 'some value' );
  161. expect( config.get( 'language' ) ).to.equal( 'pl' );
  162. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  163. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  164. } );
  165. it( 'should expand but not override deep configurations', () => {
  166. config.define( {
  167. resize: {
  168. minHeight: 400, // No override
  169. hidden: true, // Expand
  170. icon: {
  171. path: 'abc', // No override
  172. url: true // Expand
  173. }
  174. }
  175. } );
  176. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  177. minHeight: 300, // The same
  178. maxHeight: 800, // The same
  179. hidden: true, // Expanded
  180. icon: {
  181. path: 'xyz', // The same
  182. url: true // Expanded
  183. }
  184. } );
  185. } );
  186. it( 'should expand but not override when passing an object', () => {
  187. config.define( 'resize', {
  188. minHeight: 400, // No override
  189. hidden: true, // Expand
  190. icon: {
  191. path: 'abc', // No override
  192. url: true // Expand
  193. }
  194. } );
  195. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  196. minHeight: 300, // The same
  197. maxHeight: 800, // The same
  198. hidden: true, // Expanded
  199. icon: {
  200. path: 'xyz', // The same
  201. url: true // Expanded
  202. }
  203. } );
  204. } );
  205. it( 'should not create an object for non-pure objects', () => {
  206. function SomeClass() {}
  207. config.define( 'date', new Date() );
  208. config.define( {
  209. instance: new SomeClass()
  210. } );
  211. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  212. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  213. } );
  214. } );
  215. describe( 'get', () => {
  216. it( 'should retrieve a configuration', () => {
  217. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  218. } );
  219. it( 'should retrieve a deep configuration', () => {
  220. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  221. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  222. } );
  223. it( 'should retrieve a object of the configuration', () => {
  224. let resize = config.get( 'resize' );
  225. expect( resize ).to.be.an( 'object' );
  226. expect( resize.minHeight ).equal( 300 );
  227. expect( resize.maxHeight ).to.equal( 800 );
  228. expect( resize.icon ).to.be.an( 'object' );
  229. expect( resize.icon ).to.be.an( 'object' );
  230. } );
  231. it( 'should not retrieve values case-insensitively', () => {
  232. expect( config.get( 'Creator' ) ).to.be.undefined;
  233. expect( config.get( 'resize.MINHEIGHT' ) ).to.be.undefined;
  234. } );
  235. it( 'should return undefined for non existing configuration', () => {
  236. expect( config.get( 'invalid' ) ).to.be.undefined;
  237. } );
  238. it( 'should return undefined for empty configuration', () => {
  239. config = new Config();
  240. expect( config.get( 'invalid' ) ).to.be.undefined;
  241. expect( config.get( 'deep.invalid' ) ).to.be.undefined;
  242. } );
  243. it( 'should return undefined for non existing deep configuration', () => {
  244. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined;
  245. } );
  246. it( 'should not be possible to retrieve value directly from config object', () => {
  247. expect( config.creator ).to.be.undefined;
  248. expect( () => {
  249. config.resize.maxHeight;
  250. } ).to.throw();
  251. } );
  252. } );
  253. } );