config.js 9.1 KB

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