8
0

config.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Config from '../src/config';
  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. it( 'should set default parameters', () => {
  40. const defaultConfig = {
  41. foo: 1,
  42. bar: 2,
  43. };
  44. config = new Config( {}, defaultConfig );
  45. expect( config.get( 'foo' ) ).to.equal( 1 );
  46. expect( config.get( 'bar' ) ).to.equal( 2 );
  47. } );
  48. it( 'passed parameters should override default parameters', () => {
  49. const defaultConfig = {
  50. foo: 1,
  51. bar: 2,
  52. };
  53. config = new Config( {
  54. foo: 10,
  55. }, defaultConfig );
  56. expect( config.get( 'foo' ) ).to.equal( 10 );
  57. expect( config.get( 'bar' ) ).to.equal( 2 );
  58. } );
  59. it( 'should work with deeper objects', () => {
  60. const defaultConfig = {
  61. a: {
  62. first: 1,
  63. second: 2
  64. },
  65. b: {
  66. foo: {
  67. first: 1,
  68. second: 2,
  69. }
  70. }
  71. };
  72. const parameters = {
  73. a: {
  74. third: 3
  75. },
  76. b: {
  77. foo: {
  78. first: 3,
  79. third: 1
  80. }
  81. },
  82. custom: 'foo'
  83. };
  84. config = new Config( parameters, defaultConfig );
  85. expect( config.get( 'a' ) ).to.deep.equal( {
  86. first: 1,
  87. second: 2,
  88. third: 3
  89. } );
  90. expect( config.get( 'b' ) ).to.have.key( 'foo' );
  91. expect( config.get( 'b.foo' ) ).to.deep.equal( {
  92. first: 3,
  93. second: 2,
  94. third: 1
  95. } );
  96. expect( config.get( 'custom' ) ).to.equal( 'foo' );
  97. } );
  98. } );
  99. describe( 'set()', () => {
  100. it( 'should set configurations when passing objects', () => {
  101. config.set( {
  102. option1: 1,
  103. option2: {
  104. subOption21: 21
  105. }
  106. } );
  107. expect( config.get( 'option1' ) ).to.equal( 1 );
  108. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  109. } );
  110. it( 'should set configurations when passing name and value', () => {
  111. config.set( 'something', 'anything' );
  112. expect( config.get( 'something' ) ).to.equal( 'anything' );
  113. } );
  114. it( 'should set configurations when passing name.with.deep and value', () => {
  115. config.set( 'color.red', 'f00' );
  116. config.set( 'background.color.blue', '00f' );
  117. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  118. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  119. } );
  120. it( 'should replace a simple entry with an object', () => {
  121. config.set( 'test', 1 );
  122. config.set( 'test', {
  123. prop: 1
  124. } );
  125. expect( config.get( 'test' ) ).to.be.an( 'object' );
  126. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  127. } );
  128. it( 'should replace a simple entry with an object when passing only object', () => {
  129. config.set( 'test', 1 );
  130. config.set( {
  131. test: {
  132. prop: 1
  133. }
  134. } );
  135. expect( config.get( 'test' ) ).to.be.an( 'object' );
  136. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  137. } );
  138. it( 'should replace a simple entry with an object when passing a name.with.deep', () => {
  139. config.set( 'test.prop', 1 );
  140. config.set( 'test.prop.value', 1 );
  141. expect( config.get( 'test' ) ).to.be.an( 'object' );
  142. expect( config.get( 'test.prop' ) ).to.be.an( 'object' );
  143. expect( config.get( 'test.prop.value' ) ).to.equal( 1 );
  144. } );
  145. it( 'should override and expand deep configurations', () => {
  146. config.set( {
  147. resize: {
  148. minHeight: 400, // Override
  149. hidden: true, // Expand
  150. icon: {
  151. path: 'abc', // Override
  152. url: true // Expand
  153. }
  154. }
  155. } );
  156. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  157. minHeight: 400, // Overridden
  158. maxHeight: 800, // The same
  159. hidden: true, // Expanded
  160. icon: {
  161. path: 'abc', // Overridden
  162. url: true // Expanded
  163. }
  164. } );
  165. } );
  166. it( 'should override and expand object when passing an object', () => {
  167. config.set( 'resize', {
  168. minHeight: 400, // Override
  169. hidden: true, // Expand
  170. icon: {
  171. path: 'abc', // Override
  172. url: true // Expand
  173. }
  174. } );
  175. expect( config.get( 'resize' ) ).to.deep.equal( {
  176. minHeight: 400, // Overridden
  177. maxHeight: 800, // The same
  178. hidden: true, // Expanded
  179. icon: {
  180. path: 'abc', // Overridden
  181. url: true // Expanded
  182. }
  183. } );
  184. } );
  185. it( 'should not create object for non-pure objects', () => {
  186. function SomeClass() {}
  187. config.set( 'date', new Date() );
  188. config.set( {
  189. instance: new SomeClass()
  190. } );
  191. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  192. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  193. } );
  194. } );
  195. describe( 'define()', () => {
  196. it( 'should set configurations when passing objects', () => {
  197. config.set( {
  198. option1: 1,
  199. option2: {
  200. subOption21: 21
  201. }
  202. } );
  203. expect( config.get( 'option1' ) ).to.equal( 1 );
  204. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  205. } );
  206. it( 'should set configurations when passing name and value', () => {
  207. config.set( 'something', 'anything' );
  208. expect( config.get( 'something' ) ).to.equal( 'anything' );
  209. } );
  210. it( 'should set configurations when passing name.with.deep and value', () => {
  211. config.set( 'color.red', 'f00' );
  212. config.set( 'background.color.blue', '00f' );
  213. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  214. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  215. } );
  216. it( 'should not replace already defined values', () => {
  217. config.define( 'language', 'en' );
  218. config.define( 'resize.minHeight', 400 );
  219. config.define( 'resize.icon', 'some value' );
  220. expect( config.get( 'language' ) ).to.equal( 'pl' );
  221. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  222. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  223. } );
  224. it( 'should expand but not override deep configurations', () => {
  225. config.define( {
  226. resize: {
  227. minHeight: 400, // No override
  228. hidden: true, // Expand
  229. icon: {
  230. path: 'abc', // No override
  231. url: true // Expand
  232. }
  233. }
  234. } );
  235. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  236. minHeight: 300, // The same
  237. maxHeight: 800, // The same
  238. hidden: true, // Expanded
  239. icon: {
  240. path: 'xyz', // The same
  241. url: true // Expanded
  242. }
  243. } );
  244. } );
  245. it( 'should expand but not override when passing an object', () => {
  246. config.define( 'resize', {
  247. minHeight: 400, // No override
  248. hidden: true, // Expand
  249. icon: {
  250. path: 'abc', // No override
  251. url: true // Expand
  252. }
  253. } );
  254. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  255. minHeight: 300, // The same
  256. maxHeight: 800, // The same
  257. hidden: true, // Expanded
  258. icon: {
  259. path: 'xyz', // The same
  260. url: true // Expanded
  261. }
  262. } );
  263. } );
  264. it( 'should not create an object for non-pure objects', () => {
  265. function SomeClass() {}
  266. config.define( 'date', new Date() );
  267. config.define( {
  268. instance: new SomeClass()
  269. } );
  270. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  271. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  272. } );
  273. } );
  274. describe( 'get()', () => {
  275. it( 'should retrieve a configuration', () => {
  276. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  277. } );
  278. it( 'should retrieve a deep configuration', () => {
  279. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  280. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  281. } );
  282. it( 'should retrieve an object of the configuration', () => {
  283. const resize = config.get( 'resize' );
  284. expect( resize ).to.be.an( 'object' );
  285. expect( resize.minHeight ).equal( 300 );
  286. expect( resize.maxHeight ).to.equal( 800 );
  287. expect( resize.icon ).to.be.an( 'object' );
  288. expect( resize.icon ).to.be.an( 'object' );
  289. } );
  290. it( 'should not retrieve values case-insensitively', () => {
  291. expect( config.get( 'Creator' ) ).to.be.undefined;
  292. expect( config.get( 'resize.MINHEIGHT' ) ).to.be.undefined;
  293. } );
  294. it( 'should return undefined for non existing configuration', () => {
  295. expect( config.get( 'invalid' ) ).to.be.undefined;
  296. } );
  297. it( 'should return undefined for empty configuration', () => {
  298. config = new Config();
  299. expect( config.get( 'invalid' ) ).to.be.undefined;
  300. expect( config.get( 'deep.invalid' ) ).to.be.undefined;
  301. } );
  302. it( 'should return undefined for non existing deep configuration', () => {
  303. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined;
  304. } );
  305. it( 'should not be possible to retrieve value directly from config object', () => {
  306. expect( config.creator ).to.be.undefined;
  307. expect( () => {
  308. config.resize.maxHeight;
  309. } ).to.throw();
  310. } );
  311. } );
  312. } );