config.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Config from 'ckeditor5/utils/config.js';
  6. let config;
  7. beforeEach( () => {
  8. config = new Config( {
  9. creator: 'inline',
  10. language: 'pl',
  11. resize: {
  12. minHeight: 300,
  13. maxHeight: 800,
  14. icon: {
  15. path: 'xyz'
  16. }
  17. },
  18. toolbar: 'top'
  19. } );
  20. } );
  21. describe( 'constructor', () => {
  22. it( 'should set configurations', () => {
  23. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  24. expect( config.get( 'language' ) ).to.equal( 'pl' );
  25. expect( config.get( 'resize' ) ).to.deep.equal( {
  26. minHeight: 300,
  27. maxHeight: 800,
  28. icon: {
  29. path: 'xyz'
  30. }
  31. } );
  32. expect( config.get( 'toolbar' ) ).to.equal( 'top' );
  33. } );
  34. it( 'should work with no parameters', () => {
  35. // No error should be thrown.
  36. config = new Config();
  37. } );
  38. } );
  39. describe( 'set', () => {
  40. it( 'should set configurations when passing objects', () => {
  41. config.set( {
  42. option1: 1,
  43. option2: {
  44. subOption21: 21
  45. }
  46. } );
  47. expect( config.get( 'option1' ) ).to.equal( 1 );
  48. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  49. } );
  50. it( 'should set configurations when passing name and value', () => {
  51. config.set( 'something', 'anything' );
  52. expect( config.get( 'something' ) ).to.equal( 'anything' );
  53. } );
  54. it( 'should set configurations when passing name.with.deep and value', () => {
  55. config.set( 'color.red', 'f00' );
  56. config.set( 'background.color.blue', '00f' );
  57. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  58. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  59. } );
  60. it( 'should replace a simple entry with an object', () => {
  61. config.set( 'test', 1 );
  62. config.set( 'test', {
  63. prop: 1
  64. } );
  65. expect( config.get( 'test' ) ).to.be.an( 'object' );
  66. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  67. } );
  68. it( 'should replace a simple entry with an object when passing only object', () => {
  69. config.set( 'test', 1 );
  70. config.set( {
  71. test: {
  72. prop: 1
  73. }
  74. } );
  75. expect( config.get( 'test' ) ).to.be.an( 'object' );
  76. expect( config.get( 'test.prop' ) ).to.equal( 1 );
  77. } );
  78. it( 'should replace a simple entry with an object when passing a name.with.deep', () => {
  79. config.set( 'test.prop', 1 );
  80. config.set( 'test.prop.value', 1 );
  81. expect( config.get( 'test' ) ).to.be.an( 'object' );
  82. expect( config.get( 'test.prop' ) ).to.be.an( 'object' );
  83. expect( config.get( 'test.prop.value' ) ).to.equal( 1 );
  84. } );
  85. it( 'should override and expand deep configurations', () => {
  86. config.set( {
  87. resize: {
  88. minHeight: 400, // Override
  89. hidden: true, // Expand
  90. icon: {
  91. path: 'abc', // Override
  92. url: true // Expand
  93. }
  94. }
  95. } );
  96. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  97. minHeight: 400, // Overridden
  98. maxHeight: 800, // The same
  99. hidden: true, // Expanded
  100. icon: {
  101. path: 'abc', // Overridden
  102. url: true // Expanded
  103. }
  104. } );
  105. } );
  106. it( 'should override and expand object when passing an object', () => {
  107. config.set( 'resize', {
  108. minHeight: 400, // Override
  109. hidden: true, // Expand
  110. icon: {
  111. path: 'abc', // Override
  112. url: true // Expand
  113. }
  114. } );
  115. expect( config.get( 'resize' ) ).to.deep.equal( {
  116. minHeight: 400, // Overridden
  117. maxHeight: 800, // The same
  118. hidden: true, // Expanded
  119. icon: {
  120. path: 'abc', // Overridden
  121. url: true // Expanded
  122. }
  123. } );
  124. } );
  125. it( 'should not create object for non-pure objects', () => {
  126. function SomeClass() {}
  127. config.set( 'date', new Date() );
  128. config.set( {
  129. instance: new SomeClass()
  130. } );
  131. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  132. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  133. } );
  134. } );
  135. describe( 'define', () => {
  136. it( 'should set configurations when passing objects', () => {
  137. config.set( {
  138. option1: 1,
  139. option2: {
  140. subOption21: 21
  141. }
  142. } );
  143. expect( config.get( 'option1' ) ).to.equal( 1 );
  144. expect( config.get( 'option2.subOption21' ) ).to.equal( 21 );
  145. } );
  146. it( 'should set configurations when passing name and value', () => {
  147. config.set( 'something', 'anything' );
  148. expect( config.get( 'something' ) ).to.equal( 'anything' );
  149. } );
  150. it( 'should set configurations when passing name.with.deep and value', () => {
  151. config.set( 'color.red', 'f00' );
  152. config.set( 'background.color.blue', '00f' );
  153. expect( config.get( 'color.red' ) ).to.equal( 'f00' );
  154. expect( config.get( 'background.color.blue' ) ).to.equal( '00f' );
  155. } );
  156. it( 'should not replace already defined values', () => {
  157. config.define( 'language', 'en' );
  158. config.define( 'resize.minHeight', 400 );
  159. config.define( 'resize.icon', 'some value' );
  160. expect( config.get( 'language' ) ).to.equal( 'pl' );
  161. expect( config.get( 'resize.icon' ) ).to.be.an( 'object' );
  162. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  163. } );
  164. it( 'should expand but not override deep configurations', () => {
  165. config.define( {
  166. resize: {
  167. minHeight: 400, // No override
  168. hidden: true, // Expand
  169. icon: {
  170. path: 'abc', // No override
  171. url: true // Expand
  172. }
  173. }
  174. } );
  175. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  176. minHeight: 300, // The same
  177. maxHeight: 800, // The same
  178. hidden: true, // Expanded
  179. icon: {
  180. path: 'xyz', // The same
  181. url: true // Expanded
  182. }
  183. } );
  184. } );
  185. it( 'should expand but not override when passing an object', () => {
  186. config.define( 'resize', {
  187. minHeight: 400, // No override
  188. hidden: true, // Expand
  189. icon: {
  190. path: 'abc', // No override
  191. url: true // Expand
  192. }
  193. } );
  194. expect( config.get( 'resize' ) ).to.be.deep.equal( {
  195. minHeight: 300, // The same
  196. maxHeight: 800, // The same
  197. hidden: true, // Expanded
  198. icon: {
  199. path: 'xyz', // The same
  200. url: true // Expanded
  201. }
  202. } );
  203. } );
  204. it( 'should not create an object for non-pure objects', () => {
  205. function SomeClass() {}
  206. config.define( 'date', new Date() );
  207. config.define( {
  208. instance: new SomeClass()
  209. } );
  210. expect( config.get( 'date' ) ).to.be.an.instanceof( Date );
  211. expect( config.get( 'instance' ) ).to.be.an.instanceof( SomeClass );
  212. } );
  213. } );
  214. describe( 'get', () => {
  215. it( 'should retrieve a configuration', () => {
  216. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  217. } );
  218. it( 'should retrieve a deep configuration', () => {
  219. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  220. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  221. } );
  222. it( 'should retrieve a object of the configuration', () => {
  223. let resize = config.get( 'resize' );
  224. expect( resize ).to.be.an( 'object' );
  225. expect( resize.minHeight ).equal( 300 );
  226. expect( resize.maxHeight ).to.equal( 800 );
  227. expect( resize.icon ).to.be.an( 'object' );
  228. expect( resize.icon ).to.be.an( 'object' );
  229. } );
  230. it( 'should not retrieve values case-insensitively', () => {
  231. expect( config.get( 'Creator' ) ).to.be.undefined;
  232. expect( config.get( 'resize.MINHEIGHT' ) ).to.be.undefined;
  233. } );
  234. it( 'should return undefined for non existing configuration', () => {
  235. expect( config.get( 'invalid' ) ).to.be.undefined;
  236. } );
  237. it( 'should return undefined for empty configuration', () => {
  238. config = new Config();
  239. expect( config.get( 'invalid' ) ).to.be.undefined;
  240. expect( config.get( 'deep.invalid' ) ).to.be.undefined;
  241. } );
  242. it( 'should return undefined for non existing deep configuration', () => {
  243. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined;
  244. } );
  245. it( 'should not be possible to retrieve value directly from config object', () => {
  246. expect( config.creator ).to.be.undefined;
  247. expect( () => {
  248. config.resize.maxHeight;
  249. } ).to.throw();
  250. } );
  251. } );