config.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'core/config' );
  7. let Config, config;
  8. before( () => {
  9. Config = modules[ 'core/config' ];
  10. } );
  11. beforeEach( () => {
  12. config = new Config( {
  13. creator: 'inline',
  14. language: 'pl',
  15. resize: {
  16. minHeight: 300,
  17. maxHeight: 800,
  18. icon: {
  19. path: 'xyz'
  20. }
  21. },
  22. toolbar: 'top'
  23. } );
  24. } );
  25. describe( 'constructor', () => {
  26. it( 'should set configurations', () => {
  27. expect( config ).to.have.property( 'creator' ).to.equal( 'inline' );
  28. expect( config ).to.have.property( 'language' ).to.equal( 'pl' );
  29. expect( config ).to.have.property( 'resize' ).to.have.property( 'minheight' ).to.equal( 300 );
  30. expect( config ).to.have.property( 'resize' ).to.have.property( 'maxheight' ).to.equal( 800 );
  31. expect( config ).to.have.property( 'resize' ).to.have.property( 'icon' )
  32. .to.have.property( 'path' ).to.equal( 'xyz' );
  33. expect( config ).to.have.property( 'toolbar' ).to.equal( 'top' );
  34. } );
  35. it( 'should work with no parameters', () => {
  36. // No error should be thrown.
  37. config = new Config();
  38. } );
  39. } );
  40. describe( 'set', () => {
  41. it( 'should create Config instances for objects', () => {
  42. expect( config.resize ).to.be.an.instanceof( Config );
  43. expect( config.resize.icon ).to.be.an.instanceof( Config );
  44. } );
  45. it( 'should set configurations when passing objects', () => {
  46. config.set( {
  47. option1: 1,
  48. option2: {
  49. subOption21: 21
  50. }
  51. } );
  52. expect( config )
  53. .to.have.property( 'option1' ).to.equal( 1 );
  54. expect( config )
  55. .to.have.property( 'option2' )
  56. .to.have.property( 'suboption21' ).to.equal( 21 );
  57. } );
  58. it( 'should set configurations when passing name and value', () => {
  59. config.set( 'something', 'anything' );
  60. expect( config ).to.have.property( 'something' ).to.equal( 'anything' );
  61. } );
  62. it( 'should set configurations when passing name.with.deep and value', () => {
  63. config.set( 'color.red', 'f00' );
  64. config.set( 'background.color.blue', '00f' );
  65. expect( config )
  66. .to.have.property( 'color' )
  67. .to.have.property( 'red' ).to.equal( 'f00' );
  68. expect( config )
  69. .to.have.property( 'background' )
  70. .to.have.property( 'color' )
  71. .to.have.property( 'blue' ).to.equal( '00f' );
  72. } );
  73. it( 'should override and expand deep configurations', () => {
  74. config.set( {
  75. resize: {
  76. minHeight: 400, // Override
  77. hidden: true, // Expand
  78. icon: {
  79. path: 'abc', // Override
  80. url: true // Expand
  81. }
  82. }
  83. } );
  84. expect( config ).to.have.property( 'resize' );
  85. expect( config.resize ).to.have.property( 'minheight' ).to.equal( 400 );
  86. expect( config.resize ).to.have.property( 'maxheight' ).to.equal( 800 ); // Not touched
  87. expect( config.resize ).to.have.property( 'hidden' ).to.equal( true );
  88. expect( config.resize ).to.have.property( 'icon' );
  89. expect( config.resize.icon ).to.have.property( 'path' ).to.equal( 'abc' );
  90. expect( config.resize.icon ).to.have.property( 'url' ).to.equal( true );
  91. } );
  92. it( 'should replace a simple entry with a Config instance', () => {
  93. config.set( 'test', 1 );
  94. config.set( 'test', {
  95. prop: 1
  96. } );
  97. expect( config.test ).to.be.an.instanceof( Config );
  98. expect( config.test.prop ).to.equal( 1 );
  99. } );
  100. it( 'should replace a simple entry with a Config instance when passing an object', () => {
  101. config.set( 'test', 1 );
  102. config.set( {
  103. test: {
  104. prop: 1
  105. }
  106. } );
  107. expect( config.test ).to.be.an.instanceof( Config );
  108. expect( config.test.prop ).to.equal( 1 );
  109. } );
  110. it( 'should replace a simple entry with a Config instance when passing a name.with.deep', () => {
  111. config.set( 'test.prop', 1 );
  112. config.set( 'test.prop.value', 1 );
  113. expect( config.test ).to.be.an.instanceof( Config );
  114. expect( config.test.prop ).to.be.an.instanceof( Config );
  115. expect( config.test.prop.value ).to.equal( 1 );
  116. } );
  117. it( 'should not create Config instances for non-pure objects', () => {
  118. function SomeClass() {}
  119. config.set( 'date', new Date() );
  120. config.set( {
  121. instance: new SomeClass()
  122. } );
  123. expect( config.date ).to.be.an.instanceof( Date );
  124. expect( config.instance ).to.be.an.instanceof( SomeClass );
  125. } );
  126. it( 'should set `null` for undefined value', () => {
  127. config.set( 'test' );
  128. expect( config.test ).to.be.null();
  129. expect( config.get( 'test' ) ).to.be.null();
  130. } );
  131. } );
  132. describe( 'get', () => {
  133. it( 'should retrieve a configuration', () => {
  134. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  135. } );
  136. it( 'should retrieve a deep configuration', () => {
  137. expect( config.get( 'resize.minheight' ) ).to.equal( 300 );
  138. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  139. } );
  140. it( 'should retrieve a subset of the configuration', () => {
  141. let resizeConfig = config.get( 'resize' );
  142. expect( resizeConfig ).to.have.property( 'minheight' ).to.equal( 300 );
  143. expect( resizeConfig ).to.have.property( 'maxheight' ).to.equal( 800 );
  144. expect( resizeConfig ).to.have.property( 'icon' ).to.have.property( 'path' ).to.equal( 'xyz' );
  145. let iconConfig = resizeConfig.get( 'icon' );
  146. expect( iconConfig ).to.have.property( 'path' ).to.equal( 'xyz' );
  147. } );
  148. it( 'should retrieve values case-insensitively', () => {
  149. expect( config.get( 'Creator' ) ).to.equal( 'inline' );
  150. expect( config.get( 'CREATOR' ) ).to.equal( 'inline' );
  151. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  152. expect( config.get( 'resize.MINHEIGHT' ) ).to.equal( 300 );
  153. } );
  154. it( 'should return undefined for non existing configuration', () => {
  155. expect( config.get( 'invalid' ) ).to.be.undefined();
  156. } );
  157. it( 'should return undefined for non existing deep configuration', () => {
  158. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined();
  159. } );
  160. } );
  161. describe( 'define', () => {
  162. it( 'should create the definition property', () => {
  163. expect( config ).to.not.have.property( 'definition' );
  164. config.define( 'test', 1 );
  165. expect( config ).to.have.property( 'definition' );
  166. } );
  167. it( 'should set configurations in the definition property', () => {
  168. config.define( 'test1', 1 );
  169. // This is for Code Coverage to ensure that it works when `definition` is already defined.
  170. config.define( 'test2', 2 );
  171. expect( config.definition ).to.have.property( 'test1' ).to.equal( 1 );
  172. expect( config.definition ).to.have.property( 'test2' ).to.equal( 2 );
  173. } );
  174. it( 'should set configurations passed as object in the definition property', () => {
  175. config.define( {
  176. test: 1
  177. } );
  178. expect( config.definition ).to.have.property( 'test' ).to.equal( 1 );
  179. } );
  180. it( 'should not define main config properties but still be retrieved with get()', () => {
  181. config.define( 'test', 1 );
  182. expect( config ).to.not.have.property( 'test' );
  183. expect( config.get( 'test' ) ).to.equal( 1 );
  184. } );
  185. it( 'should be overridden by set()', () => {
  186. config.define( 'test', 1 );
  187. config.set( 'test', 2 );
  188. expect( config ).to.have.property( 'test' ).to.equal( 2 );
  189. expect( config.get( 'test' ) ).to.equal( 2 );
  190. } );
  191. } );