8
0

config.js 7.2 KB

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