8
0

config.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/utils/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.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. it( 'should not transform keys to lowerCase', () => {
  136. config.set( 'Foo', 'Bar' );
  137. config.set( 'Example.First', 1 );
  138. config.set( 'Example.Second', 2 );
  139. expect( config.get( 'Foo' ) ).to.equal( 'Bar' );
  140. expect( config.get( 'Example' ) ).to.deep.equal( {
  141. First: 1,
  142. Second: 2
  143. } );
  144. } );
  145. } );
  146. describe( 'define', () => {
  147. it( 'should set configurations when passing objects', () => {
  148. config.set( {
  149. option1: 1,
  150. option2: {
  151. subOption21: 21
  152. }
  153. } );
  154. expect( config.get( 'option1' ) ).to.equal( 1 );
  155. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  156. } );
  157. it( 'should set configurations when passing name and value', () => {
  158. config.set( 'something', 'anything' );
  159. expect( config.get( 'something' ) ).to.equal( 'anything' );
  160. } );
  161. it( 'should set configurations when passing name.with.deep and value', () => {
  162. config.set( 'color.red', 'f00' );
  163. config.set( 'background.color.blue', '00f' );
  164. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  165. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  166. } );
  167. it( 'should not replace already defined values', () => {
  168. config.define( 'language', 'en' );
  169. config.define( 'resize.minHeight', 400 );
  170. config.define( 'resize.icon', 'some value' );
  171. expect( config.get( 'language' ) ).to.equal( 'pl' );
  172. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  173. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  174. } );
  175. it( 'should expand but not override deep configurations', () => {
  176. config.define( {
  177. resize: {
  178. minHeight: 400, // No override
  179. hidden: true, // Expand
  180. icon: {
  181. path: 'abc', // No override
  182. url: true // Expand
  183. }
  184. }
  185. } );
  186. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  187. minHeight: 300, // The same
  188. maxHeight: 800, // The same
  189. hidden: true, // Expanded
  190. icon: {
  191. path: 'xyz', // The same
  192. url: true // Expanded
  193. }
  194. } );
  195. } );
  196. it( 'should expand but not override when passing an object', () => {
  197. config.define( 'resize', {
  198. minHeight: 400, // No override
  199. hidden: true, // Expand
  200. icon: {
  201. path: 'abc', // No override
  202. url: true // Expand
  203. }
  204. } );
  205. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  206. minHeight: 300, // The same
  207. maxHeight: 800, // The same
  208. hidden: true, // Expanded
  209. icon: {
  210. path: 'xyz', // The same
  211. url: true // Expanded
  212. }
  213. } );
  214. } );
  215. it( 'should not create an object for non-pure objects', () => {
  216. function SomeClass() {}
  217. config.define( 'date', new Date() );
  218. config.define( {
  219. instance: new SomeClass()
  220. } );
  221. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  222. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  223. } );
  224. it( 'should not transform keys to lowerCase', () => {
  225. config.set( 'Foo', 'Bar' );
  226. config.set( 'Example.First', 1 );
  227. config.set( 'Example.Second', 2 );
  228. expect( config.get( 'Foo' ) ).to.equal( 'Bar' );
  229. expect( config.get( 'Example' ) ).to.deep.equal( {
  230. First: 1,
  231. Second: 2
  232. } );
  233. } );
  234. } );
  235. describe( 'get', () => {
  236. it( 'should retrieve a configuration', () => {
  237. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  238. } );
  239. it( 'should retrieve a deep configuration', () => {
  240. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  241. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  242. } );
  243. it( 'should retrieve a object of the configuration', () => {
  244. let resize = config.get( 'resize' );
  245. expect( resize ).to.be.an( 'object' );
  246. expect( resize.minHeight ).equal( 300 );
  247. expect( resize.maxHeight ).to.equal( 800 );
  248. expect( resize.icon ).to.be.an( 'object' );
  249. expect( resize.icon ).to.be.an( 'object' );
  250. } );
  251. it( 'should not retrieve values case-insensitively', () => {
  252. expect( config.get( 'Creator' ) ).to.be.undefined;
  253. expect( config.get( 'resize.MINHEIGHT' ) ).to.be.undefined;
  254. } );
  255. it( 'should return undefined for non existing configuration', () => {
  256. expect( config.get( 'invalid' ) ).to.be.undefined;
  257. } );
  258. it( 'should return undefined for empty configuration', () => {
  259. config = new Config();
  260. expect( config.get( 'invalid' ) ).to.be.undefined;
  261. expect( config.get( 'deep.invalid' ) ).to.be.undefined;
  262. } );
  263. it( 'should return undefined for non existing deep configuration', () => {
  264. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined;
  265. } );
  266. it( 'should not be possible to retrieve value directly from config object', () => {
  267. expect( config.creator ).to.be.undefined;
  268. expect( () => {
  269. config.resize.maxHeight;
  270. } ).to.throw();
  271. } );
  272. } );