config.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. } );
  60. describe( 'set()', () => {
  61. it( 'should set configurations when passing objects', () => {
  62. config.set( {
  63. option1: 1,
  64. option2: {
  65. subOption21: 21
  66. }
  67. } );
  68. expect( config.get( 'option1' ) ).to.equal( 1 );
  69. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  70. } );
  71. it( 'should set configurations when passing name and value', () => {
  72. config.set( 'something', 'anything' );
  73. expect( config.get( 'something' ) ).to.equal( 'anything' );
  74. } );
  75. it( 'should set configurations when passing name.with.deep and value', () => {
  76. config.set( 'color.red', 'f00' );
  77. config.set( 'background.color.blue', '00f' );
  78. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  79. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  80. } );
  81. it( 'should replace a simple entry with an object', () => {
  82. config.set( 'test', 1 );
  83. config.set( 'test', {
  84. prop: 1
  85. } );
  86. expect( config.get( 'test' ) ).to.be.an( 'object' );
  87. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  88. } );
  89. it( 'should replace a simple entry with an object when passing only object', () => {
  90. config.set( 'test', 1 );
  91. config.set( {
  92. test: {
  93. prop: 1
  94. }
  95. } );
  96. expect( config.get( 'test' ) ).to.be.an( 'object' );
  97. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  98. } );
  99. it( 'should replace a simple entry with an object when passing a name.with.deep', () => {
  100. config.set( 'test.prop', 1 );
  101. config.set( 'test.prop.value', 1 );
  102. expect( config.get( 'test' ) ).to.be.an( 'object' );
  103. expect( config.get( 'test.prop' ) ).to.be.an( 'object' );
  104. expect( config.get( 'test.prop.value' ) ).to.equal( 1 );
  105. } );
  106. it( 'should override and expand deep configurations', () => {
  107. config.set( {
  108. resize: {
  109. minHeight: 400, // Override
  110. hidden: true, // Expand
  111. icon: {
  112. path: 'abc', // Override
  113. url: true // Expand
  114. }
  115. }
  116. } );
  117. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  118. minHeight: 400, // Overridden
  119. maxHeight: 800, // The same
  120. hidden: true, // Expanded
  121. icon: {
  122. path: 'abc', // Overridden
  123. url: true // Expanded
  124. }
  125. } );
  126. } );
  127. it( 'should override and expand object when passing an object', () => {
  128. config.set( 'resize', {
  129. minHeight: 400, // Override
  130. hidden: true, // Expand
  131. icon: {
  132. path: 'abc', // Override
  133. url: true // Expand
  134. }
  135. } );
  136. expect( config.get( 'resize' ) ).to.deep.equal( {
  137. minHeight: 400, // Overridden
  138. maxHeight: 800, // The same
  139. hidden: true, // Expanded
  140. icon: {
  141. path: 'abc', // Overridden
  142. url: true // Expanded
  143. }
  144. } );
  145. } );
  146. it( 'should not create object for non-pure objects', () => {
  147. function SomeClass() {}
  148. config.set( 'date', new Date() );
  149. config.set( {
  150. instance: new SomeClass()
  151. } );
  152. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  153. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  154. } );
  155. } );
  156. describe( 'define()', () => {
  157. it( 'should set configurations when passing objects', () => {
  158. config.set( {
  159. option1: 1,
  160. option2: {
  161. subOption21: 21
  162. }
  163. } );
  164. expect( config.get( 'option1' ) ).to.equal( 1 );
  165. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  166. } );
  167. it( 'should set configurations when passing name and value', () => {
  168. config.set( 'something', 'anything' );
  169. expect( config.get( 'something' ) ).to.equal( 'anything' );
  170. } );
  171. it( 'should set configurations when passing name.with.deep and value', () => {
  172. config.set( 'color.red', 'f00' );
  173. config.set( 'background.color.blue', '00f' );
  174. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  175. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  176. } );
  177. it( 'should not replace already defined values', () => {
  178. config.define( 'language', 'en' );
  179. config.define( 'resize.minHeight', 400 );
  180. config.define( 'resize.icon', 'some value' );
  181. expect( config.get( 'language' ) ).to.equal( 'pl' );
  182. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  183. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  184. } );
  185. it( 'should expand but not override deep configurations', () => {
  186. config.define( {
  187. resize: {
  188. minHeight: 400, // No override
  189. hidden: true, // Expand
  190. icon: {
  191. path: 'abc', // No override
  192. url: true // Expand
  193. }
  194. }
  195. } );
  196. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  197. minHeight: 300, // The same
  198. maxHeight: 800, // The same
  199. hidden: true, // Expanded
  200. icon: {
  201. path: 'xyz', // The same
  202. url: true // Expanded
  203. }
  204. } );
  205. } );
  206. it( 'should expand but not override when passing an object', () => {
  207. config.define( 'resize', {
  208. minHeight: 400, // No override
  209. hidden: true, // Expand
  210. icon: {
  211. path: 'abc', // No override
  212. url: true // Expand
  213. }
  214. } );
  215. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  216. minHeight: 300, // The same
  217. maxHeight: 800, // The same
  218. hidden: true, // Expanded
  219. icon: {
  220. path: 'xyz', // The same
  221. url: true // Expanded
  222. }
  223. } );
  224. } );
  225. it( 'should not create an object for non-pure objects', () => {
  226. function SomeClass() {}
  227. config.define( 'date', new Date() );
  228. config.define( {
  229. instance: new SomeClass()
  230. } );
  231. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  232. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  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. } );
  273. } );