config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. options: {
  21. foo: [
  22. { bar: 'b' },
  23. { bar: 'a' },
  24. { bar: 'z' }
  25. ]
  26. }
  27. } );
  28. } );
  29. describe( 'constructor()', () => {
  30. it( 'should set configurations', () => {
  31. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  32. expect( config.get( 'language' ) ).to.equal( 'pl' );
  33. expect( config.get( 'resize' ) ).to.deep.equal( {
  34. minHeight: 300,
  35. maxHeight: 800,
  36. icon: {
  37. path: 'xyz'
  38. }
  39. } );
  40. expect( config.get( 'toolbar' ) ).to.equal( 'top' );
  41. } );
  42. it( 'should work with no parameters', () => {
  43. // No error should be thrown.
  44. config = new Config();
  45. } );
  46. it( 'should set default parameters', () => {
  47. const defaultConfig = {
  48. foo: 1,
  49. bar: 2,
  50. };
  51. config = new Config( {}, defaultConfig );
  52. expect( config.get( 'foo' ) ).to.equal( 1 );
  53. expect( config.get( 'bar' ) ).to.equal( 2 );
  54. } );
  55. it( 'passed parameters should override default parameters', () => {
  56. const defaultConfig = {
  57. foo: 1,
  58. bar: 2,
  59. };
  60. config = new Config( {
  61. foo: 10,
  62. }, defaultConfig );
  63. expect( config.get( 'foo' ) ).to.equal( 10 );
  64. expect( config.get( 'bar' ) ).to.equal( 2 );
  65. } );
  66. it( 'should work with deeper objects', () => {
  67. const defaultConfig = {
  68. a: {
  69. first: 1,
  70. second: 2
  71. },
  72. b: {
  73. foo: {
  74. first: 1,
  75. second: 2,
  76. }
  77. }
  78. };
  79. const parameters = {
  80. a: {
  81. third: 3
  82. },
  83. b: {
  84. foo: {
  85. first: 3,
  86. third: 1
  87. }
  88. },
  89. custom: 'foo'
  90. };
  91. config = new Config( parameters, defaultConfig );
  92. expect( config.get( 'a' ) ).to.deep.equal( {
  93. first: 1,
  94. second: 2,
  95. third: 3
  96. } );
  97. expect( config.get( 'b' ) ).to.have.key( 'foo' );
  98. expect( config.get( 'b.foo' ) ).to.deep.equal( {
  99. first: 3,
  100. second: 2,
  101. third: 1
  102. } );
  103. expect( config.get( 'custom' ) ).to.equal( 'foo' );
  104. } );
  105. } );
  106. describe( 'set()', () => {
  107. it( 'should set configurations when passing objects', () => {
  108. config.set( {
  109. option1: 1,
  110. option2: {
  111. subOption21: 21
  112. }
  113. } );
  114. expect( config.get( 'option1' ) ).to.equal( 1 );
  115. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  116. } );
  117. it( 'should set configurations when passing name and value', () => {
  118. config.set( 'something', 'anything' );
  119. expect( config.get( 'something' ) ).to.equal( 'anything' );
  120. } );
  121. it( 'should set configurations when passing name.with.deep and value', () => {
  122. config.set( 'color.red', 'f00' );
  123. config.set( 'background.color.blue', '00f' );
  124. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  125. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  126. } );
  127. it( 'should replace a simple entry with an object', () => {
  128. config.set( 'test', 1 );
  129. config.set( 'test', {
  130. prop: 1
  131. } );
  132. expect( config.get( 'test' ) ).to.be.an( 'object' );
  133. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  134. } );
  135. it( 'should replace a simple entry with an object when passing only object', () => {
  136. config.set( 'test', 1 );
  137. config.set( {
  138. test: {
  139. prop: 1
  140. }
  141. } );
  142. expect( config.get( 'test' ) ).to.be.an( 'object' );
  143. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  144. } );
  145. it( 'should replace a simple entry with an object when passing a name.with.deep', () => {
  146. config.set( 'test.prop', 1 );
  147. config.set( 'test.prop.value', 1 );
  148. expect( config.get( 'test' ) ).to.be.an( 'object' );
  149. expect( config.get( 'test.prop' ) ).to.be.an( 'object' );
  150. expect( config.get( 'test.prop.value' ) ).to.equal( 1 );
  151. } );
  152. it( 'should override and expand deep configurations', () => {
  153. config.set( {
  154. resize: {
  155. minHeight: 400, // Override
  156. hidden: true, // Expand
  157. icon: {
  158. path: 'abc', // Override
  159. url: true // Expand
  160. }
  161. }
  162. } );
  163. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  164. minHeight: 400, // Overridden
  165. maxHeight: 800, // The same
  166. hidden: true, // Expanded
  167. icon: {
  168. path: 'abc', // Overridden
  169. url: true // Expanded
  170. }
  171. } );
  172. } );
  173. it( 'should override and expand object when passing an object', () => {
  174. config.set( 'resize', {
  175. minHeight: 400, // Override
  176. hidden: true, // Expand
  177. icon: {
  178. path: 'abc', // Override
  179. url: true // Expand
  180. }
  181. } );
  182. expect( config.get( 'resize' ) ).to.deep.equal( {
  183. minHeight: 400, // Overridden
  184. maxHeight: 800, // The same
  185. hidden: true, // Expanded
  186. icon: {
  187. path: 'abc', // Overridden
  188. url: true // Expanded
  189. }
  190. } );
  191. } );
  192. it( 'should not create object for non-pure objects', () => {
  193. function SomeClass() {}
  194. config.set( 'date', new Date() );
  195. config.set( {
  196. instance: new SomeClass()
  197. } );
  198. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  199. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  200. } );
  201. } );
  202. describe( 'define()', () => {
  203. it( 'should set configurations when passing objects', () => {
  204. config.set( {
  205. option1: 1,
  206. option2: {
  207. subOption21: 21
  208. }
  209. } );
  210. expect( config.get( 'option1' ) ).to.equal( 1 );
  211. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  212. } );
  213. it( 'should set configurations when passing name and value', () => {
  214. config.set( 'something', 'anything' );
  215. expect( config.get( 'something' ) ).to.equal( 'anything' );
  216. } );
  217. it( 'should set configurations when passing name.with.deep and value', () => {
  218. config.set( 'color.red', 'f00' );
  219. config.set( 'background.color.blue', '00f' );
  220. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  221. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  222. } );
  223. it( 'should not replace already defined values', () => {
  224. config.define( 'language', 'en' );
  225. config.define( 'resize.minHeight', 400 );
  226. config.define( 'resize.icon', 'some value' );
  227. expect( config.get( 'language' ) ).to.equal( 'pl' );
  228. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  229. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  230. } );
  231. it( 'should expand but not override deep configurations', () => {
  232. config.define( {
  233. resize: {
  234. minHeight: 400, // No override
  235. hidden: true, // Expand
  236. icon: {
  237. path: 'abc', // No override
  238. url: true // Expand
  239. }
  240. }
  241. } );
  242. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  243. minHeight: 300, // The same
  244. maxHeight: 800, // The same
  245. hidden: true, // Expanded
  246. icon: {
  247. path: 'xyz', // The same
  248. url: true // Expanded
  249. }
  250. } );
  251. } );
  252. it( 'should expand but not override when passing an object', () => {
  253. config.define( 'resize', {
  254. minHeight: 400, // No override
  255. hidden: true, // Expand
  256. icon: {
  257. path: 'abc', // No override
  258. url: true // Expand
  259. }
  260. } );
  261. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  262. minHeight: 300, // The same
  263. maxHeight: 800, // The same
  264. hidden: true, // Expanded
  265. icon: {
  266. path: 'xyz', // The same
  267. url: true // Expanded
  268. }
  269. } );
  270. } );
  271. it( 'should not create an object for non-pure objects', () => {
  272. function SomeClass() {}
  273. config.define( 'date', new Date() );
  274. config.define( {
  275. instance: new SomeClass()
  276. } );
  277. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  278. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  279. } );
  280. } );
  281. describe( 'get()', () => {
  282. it( 'should retrieve a configuration', () => {
  283. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  284. } );
  285. it( 'should retrieve a deep configuration', () => {
  286. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  287. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  288. } );
  289. it( 'should retrieve an object of the configuration', () => {
  290. const resize = config.get( 'resize' );
  291. expect( resize ).to.be.an( 'object' );
  292. expect( resize.minHeight ).equal( 300 );
  293. expect( resize.maxHeight ).to.equal( 800 );
  294. expect( resize.icon ).to.be.an( 'object' );
  295. expect( resize.icon ).to.be.an( 'object' );
  296. } );
  297. it( 'should not retrieve values case-insensitively', () => {
  298. expect( config.get( 'Creator' ) ).to.be.undefined;
  299. expect( config.get( 'resize.MINHEIGHT' ) ).to.be.undefined;
  300. } );
  301. it( 'should return undefined for non existing configuration', () => {
  302. expect( config.get( 'invalid' ) ).to.be.undefined;
  303. } );
  304. it( 'should return undefined for empty configuration', () => {
  305. config = new Config();
  306. expect( config.get( 'invalid' ) ).to.be.undefined;
  307. expect( config.get( 'deep.invalid' ) ).to.be.undefined;
  308. } );
  309. it( 'should return undefined for non existing deep configuration', () => {
  310. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined;
  311. } );
  312. it( 'should not be possible to retrieve value directly from config object', () => {
  313. expect( config.creator ).to.be.undefined;
  314. expect( () => {
  315. config.resize.maxHeight;
  316. } ).to.throw();
  317. } );
  318. it( 'should not be possible to alter config object by altering returned value', () => {
  319. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  320. const icon = config.get( 'resize.icon' );
  321. icon.path = 'foo/bar';
  322. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  323. const resize = config.get( 'resize' );
  324. resize.icon.path = 'foo/baz';
  325. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  326. } );
  327. it( 'should not be possible to alter array in config by altering returned value', () => {
  328. expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
  329. const fooOptions = config.get( 'options.foo' );
  330. fooOptions.pop();
  331. expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
  332. const options = config.get( 'options' );
  333. options.foo.pop();
  334. expect( config.get( 'options.foo' ) ).to.deep.equal( [ { bar: 'b' }, { bar: 'a' }, { bar: 'z' } ] );
  335. } );
  336. it( 'should return class & functions references from config array', () => {
  337. class Foo {}
  338. function bar() {
  339. return 'bar';
  340. }
  341. const baz = () => 'baz';
  342. config.set( 'plugins', [ Foo, bar, baz ] );
  343. expect( config.get( 'plugins' ) ).to.deep.equal( [ Foo, bar, baz ] );
  344. const plugins = config.get( 'plugins' );
  345. expect( plugins[ 0 ] ).to.equal( Foo );
  346. expect( plugins[ 1 ] ).to.equal( bar );
  347. expect( plugins[ 2 ] ).to.equal( baz );
  348. const pluginsAgain = config.get( 'plugins' );
  349. // The returned array should be a new instance:
  350. expect( pluginsAgain ).to.not.equal( plugins );
  351. // But array members should remain the same contents should be equal:
  352. expect( pluginsAgain ).to.deep.equal( plugins );
  353. } );
  354. } );
  355. } );