config.js 12 KB

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