config.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'config' );
  7. let config;
  8. beforeEach( function() {
  9. const Config = modules.config;
  10. config = new Config( {
  11. creator: 'inline',
  12. language: 'pl',
  13. resize: {
  14. minHeight: 300,
  15. maxHeight: 800,
  16. icon: {
  17. path: 'xyz'
  18. }
  19. },
  20. toolbar: 'top'
  21. } );
  22. } );
  23. describe( 'constructor', function() {
  24. it( 'should set configurations', function() {
  25. expect( config ).to.have.property( 'creator' ).to.equal( 'inline' );
  26. expect( config ).to.have.property( 'language' ).to.equal( 'pl' );
  27. expect( config ).to.have.property( 'resize' ).to.have.property( 'minheight' ).to.equal( 300 );
  28. expect( config ).to.have.property( 'resize' ).to.have.property( 'maxheight' ).to.equal( 800 );
  29. expect( config ).to.have.property( 'resize' ).to.have.property( 'icon' )
  30. .to.have.property( 'path' ).to.equal( 'xyz' );
  31. expect( config ).to.have.property( 'toolbar' ).to.equal( 'top' );
  32. } );
  33. it( 'should work with no parameters', function() {
  34. const Config = modules.config;
  35. // No error should be thrown.
  36. config = new Config();
  37. } );
  38. } );
  39. describe( 'set', function() {
  40. it( 'should create Config instances for objects', function() {
  41. const Config = modules.config;
  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', function() {
  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', function() {
  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', function() {
  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', function() {
  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', function() {
  93. const Config = modules.config;
  94. config.set( 'test', 1 );
  95. config.set( 'test', {
  96. prop: 1
  97. } );
  98. expect( config.test ).to.be.an.instanceof( Config );
  99. expect( config.test.prop ).to.equal( 1 );
  100. } );
  101. it( 'should replace a simple entry with a Config instance when passing an object', function() {
  102. const Config = modules.config;
  103. config.set( 'test', 1 );
  104. config.set( {
  105. test: {
  106. prop: 1
  107. }
  108. } );
  109. expect( config.test ).to.be.an.instanceof( Config );
  110. expect( config.test.prop ).to.equal( 1 );
  111. } );
  112. it( 'should replace a simple entry with a Config instance when passing a name.with.deep', function() {
  113. const Config = modules.config;
  114. config.set( 'test.prop', 1 );
  115. config.set( 'test.prop.value', 1 );
  116. expect( config.test ).to.be.an.instanceof( Config );
  117. expect( config.test.prop ).to.be.an.instanceof( Config );
  118. expect( config.test.prop.value ).to.equal( 1 );
  119. } );
  120. it( 'should not create Config instances for non-pure objects', function() {
  121. function SomeClass() {}
  122. config.set( 'date', new Date() );
  123. config.set( {
  124. instance: new SomeClass()
  125. } );
  126. expect( config.date ).to.be.an.instanceof( Date );
  127. expect( config.instance ).to.be.an.instanceof( SomeClass );
  128. } );
  129. it( 'should set `null` for undefined value', function() {
  130. config.set( 'test' );
  131. expect( config.test ).to.be.null();
  132. expect( config.get( 'test' ) ).to.be.null();
  133. } );
  134. } );
  135. describe( 'get', function() {
  136. it( 'should retrieve a configuration', function() {
  137. expect( config.get( 'creator' ) ).to.equal( 'inline' );
  138. } );
  139. it( 'should retrieve a deep configuration', function() {
  140. expect( config.get( 'resize.minheight' ) ).to.equal( 300 );
  141. expect( config.get( 'resize.icon.path' ) ).to.equal( 'xyz' );
  142. } );
  143. it( 'should retrieve a subset of the configuration', function() {
  144. let resizeConfig = config.get( 'resize' );
  145. expect( resizeConfig ).to.have.property( 'minheight' ).to.equal( 300 );
  146. expect( resizeConfig ).to.have.property( 'maxheight' ).to.equal( 800 );
  147. expect( resizeConfig ).to.have.property( 'icon' ).to.have.property( 'path' ).to.equal( 'xyz' );
  148. let iconConfig = resizeConfig.get( 'icon' );
  149. expect( iconConfig ).to.have.property( 'path' ).to.equal( 'xyz' );
  150. } );
  151. it( 'should retrieve values case-insensitively', function() {
  152. expect( config.get( 'Creator' ) ).to.equal( 'inline' );
  153. expect( config.get( 'CREATOR' ) ).to.equal( 'inline' );
  154. expect( config.get( 'resize.minHeight' ) ).to.equal( 300 );
  155. expect( config.get( 'resize.MINHEIGHT' ) ).to.equal( 300 );
  156. } );
  157. it( 'should return undefined for non existing configuration', function() {
  158. expect( config.get( 'invalid' ) ).to.be.undefined();
  159. } );
  160. it( 'should return undefined for non existing deep configuration', function() {
  161. expect( config.get( 'resize.invalid.value' ) ).to.be.undefined();
  162. } );
  163. } );
  164. describe( 'define', function() {
  165. it( 'should create the definition property', function() {
  166. expect( config ).to.not.have.property( 'definition' );
  167. config.define( 'test', 1 );
  168. expect( config ).to.have.property( 'definition' );
  169. } );
  170. it( 'should set configurations in the definition property', function() {
  171. config.define( 'test1', 1 );
  172. // This is for Code Coverage to ensure that it works when `definition` is already defined.
  173. config.define( 'test2', 2 );
  174. expect( config.definition ).to.have.property( 'test1' ).to.equal( 1 );
  175. expect( config.definition ).to.have.property( 'test2' ).to.equal( 2 );
  176. } );
  177. it( 'should set configurations passed as object in the definition property', function() {
  178. config.define( {
  179. test: 1
  180. } );
  181. expect( config.definition ).to.have.property( 'test' ).to.equal( 1 );
  182. } );
  183. it( 'should not define main config properties but still be retrieved with get()', function() {
  184. config.define( 'test', 1 );
  185. expect( config ).to.not.have.property( 'test' );
  186. expect( config.get( 'test' ) ).to.equal( 1 );
  187. } );
  188. it( 'should be overridden by set()', function() {
  189. config.define( 'test', 1 );
  190. config.set( 'test', 2 );
  191. expect( config ).to.have.property( 'test' ).to.equal( 2 );
  192. expect( config.get( 'test' ) ).to.equal( 2 );
  193. } );
  194. } );