customize-base.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* global wp */
  2. jQuery( function( $ ) {
  3. var FooSuperClass, BarSubClass, foo, bar, ConstructorTestClass, newConstructor, constructorTest, $mockElement, mockString,
  4. firstInitialValue, firstValueInstance, valuesInstance, wasCallbackFired, mockValueCallback;
  5. QUnit.module( 'Customize Base: Class' );
  6. FooSuperClass = wp.customize.Class.extend(
  7. {
  8. initialize: function( instanceProps ) {
  9. $.extend( this, instanceProps || {} );
  10. },
  11. protoProp: 'protoPropValue'
  12. },
  13. {
  14. staticProp: 'staticPropValue'
  15. }
  16. );
  17. QUnit.test( 'FooSuperClass is a function', function( assert ) {
  18. assert.equal( typeof FooSuperClass, 'function' );
  19. });
  20. QUnit.test( 'FooSuperClass prototype has protoProp', function( assert ) {
  21. assert.equal( FooSuperClass.prototype.protoProp, 'protoPropValue' );
  22. });
  23. QUnit.test( 'FooSuperClass does not have protoProp', function( assert ) {
  24. assert.equal( typeof FooSuperClass.protoProp, 'undefined' );
  25. });
  26. QUnit.test( 'FooSuperClass has staticProp', function( assert ) {
  27. assert.equal( FooSuperClass.staticProp, 'staticPropValue' );
  28. });
  29. QUnit.test( 'FooSuperClass prototype does not have staticProp', function( assert ) {
  30. assert.equal( typeof FooSuperClass.prototype.staticProp, 'undefined' );
  31. });
  32. foo = new FooSuperClass( { instanceProp: 'instancePropValue' } );
  33. QUnit.test( 'FooSuperClass instance foo extended Class', function( assert ) {
  34. assert.equal( foo.extended( wp.customize.Class ), true );
  35. });
  36. QUnit.test( 'foo instance has protoProp', function( assert ) {
  37. assert.equal( foo.protoProp, 'protoPropValue' );
  38. });
  39. QUnit.test( 'foo instance does not have staticProp', function( assert ) {
  40. assert.equal( typeof foo.staticProp, 'undefined' );
  41. });
  42. QUnit.test( 'FooSuperClass instance foo ran initialize() and has supplied instanceProp', function( assert ) {
  43. assert.equal( foo.instanceProp, 'instancePropValue' );
  44. });
  45. // @todo Test Class.applicator?
  46. // @todo Do we test object.instance?
  47. QUnit.module( 'Customize Base: Subclass' );
  48. BarSubClass = FooSuperClass.extend(
  49. {
  50. initialize: function ( instanceProps ) {
  51. FooSuperClass.prototype.initialize.call( this, instanceProps );
  52. this.subInstanceProp = 'subInstancePropValue';
  53. },
  54. subProtoProp: 'subProtoPropValue'
  55. },
  56. {
  57. subStaticProp: 'subStaticPropValue'
  58. }
  59. );
  60. QUnit.test( 'BarSubClass prototype has subProtoProp', function( assert ) {
  61. assert.equal( BarSubClass.prototype.subProtoProp, 'subProtoPropValue' );
  62. });
  63. QUnit.test( 'BarSubClass prototype has parent FooSuperClass protoProp', function( assert ) {
  64. assert.equal( BarSubClass.prototype.protoProp, 'protoPropValue' );
  65. });
  66. bar = new BarSubClass( { instanceProp: 'instancePropValue' } );
  67. QUnit.test( 'BarSubClass instance bar its initialize() and parent initialize() run', function( assert ) {
  68. assert.equal( bar.instanceProp, 'instancePropValue' );
  69. assert.equal( bar.subInstanceProp, 'subInstancePropValue' );
  70. });
  71. QUnit.test( 'BarSubClass instance bar extended FooSuperClass', function( assert ) {
  72. assert.equal( bar.extended( FooSuperClass ), true );
  73. });
  74. // Implements todo: Test Class.constructor() manipulation.
  75. QUnit.module( 'Customize Base: Constructor Manipulation' );
  76. newConstructor = function ( instanceProps ) {
  77. $.extend( this , instanceProps || {} );
  78. };
  79. ConstructorTestClass = wp.customize.Class.extend(
  80. {
  81. constructor : newConstructor,
  82. protoProp: 'protoPropValue'
  83. },
  84. {
  85. staticProp: 'staticPropValue'
  86. }
  87. );
  88. QUnit.test( 'New constructor added to class', function( assert ) {
  89. assert.equal( ConstructorTestClass.prototype.constructor , newConstructor );
  90. });
  91. QUnit.test( 'Class with new constructor has protoPropValue', function( assert ) {
  92. assert.equal( ConstructorTestClass.prototype.protoProp , 'protoPropValue' );
  93. });
  94. constructorTest = new ConstructorTestClass( { instanceProp: 'instancePropValue' } );
  95. QUnit.test( 'ConstructorTestClass instance constructorTest has the new constructor', function( assert ) {
  96. assert.equal( constructorTest.constructor, newConstructor );
  97. });
  98. QUnit.test( 'ConstructorTestClass instance constructorTest extended Class', function( assert ) {
  99. assert.equal( constructorTest.extended( wp.customize.Class ), true );
  100. });
  101. QUnit.test( 'ConstructorTestClass instance constructorTest has the added instance property', function( assert ) {
  102. assert.equal( constructorTest.instanceProp , 'instancePropValue' );
  103. });
  104. QUnit.module( 'Customize Base: wp.customizer.ensure' );
  105. $mockElement = $( '<div id="mockElement"></div>' );
  106. QUnit.test( 'Handles jQuery argument', function( assert ) {
  107. assert.equal( wp.customize.ensure( $mockElement ) , $mockElement );
  108. });
  109. mockString = '<div class="mockString"></div>';
  110. QUnit.test( 'Handles string argument', function( assert ) {
  111. assert.ok( wp.customize.ensure( mockString ) instanceof jQuery );
  112. });
  113. QUnit.module( 'Customize Base: Value Class' );
  114. firstInitialValue = true;
  115. firstValueInstance = new wp.customize.Value( firstInitialValue );
  116. QUnit.test( 'Initialized with the right value', function( assert ) {
  117. assert.equal( firstValueInstance.get() , firstInitialValue );
  118. });
  119. QUnit.test( '.set() works', function( assert ) {
  120. firstValueInstance.set( false );
  121. assert.equal( firstValueInstance.get() , false );
  122. });
  123. QUnit.test( '.bind() adds new callback that fires on set()', function( assert ) {
  124. wasCallbackFired = false;
  125. mockValueCallback = function() {
  126. wasCallbackFired = true;
  127. };
  128. firstValueInstance.bind( mockValueCallback );
  129. firstValueInstance.set( 'newValue' );
  130. assert.ok( wasCallbackFired );
  131. });
  132. QUnit.module( 'Customize Base: Values Class' );
  133. valuesInstance = new wp.customize.Values();
  134. QUnit.test( 'Correct events are triggered when adding to or removing from Values collection', function( assert ) {
  135. var hasFooOnAdd = false,
  136. hasFooOnRemove = false,
  137. hasFooOnRemoved = true,
  138. valuePassedToAdd = false,
  139. valuePassedToRemove = false,
  140. valuePassedToRemoved = false,
  141. wasEventFiredOnRemoval = false,
  142. fooValue = new wp.customize.Value( 'foo' );
  143. // Test events when adding new value.
  144. valuesInstance.bind( 'add', function( value ) {
  145. hasFooOnAdd = valuesInstance.has( 'foo' );
  146. valuePassedToAdd = value;
  147. } );
  148. valuesInstance.add( 'foo', fooValue );
  149. assert.ok( hasFooOnAdd );
  150. assert.equal( valuePassedToAdd.get(), fooValue.get() );
  151. // Test events when removing the value.
  152. valuesInstance.bind( 'remove', function( value ) {
  153. hasFooOnRemove = valuesInstance.has( 'foo' );
  154. valuePassedToRemove = value;
  155. wasEventFiredOnRemoval = true;
  156. } );
  157. valuesInstance.bind( 'removed', function( value ) {
  158. hasFooOnRemoved = valuesInstance.has( 'foo' );
  159. valuePassedToRemoved = value;
  160. wasEventFiredOnRemoval = true;
  161. } );
  162. valuesInstance.remove( 'foo' );
  163. assert.ok( hasFooOnRemove );
  164. assert.equal( valuePassedToRemove.get(), fooValue.get() );
  165. assert.ok( ! hasFooOnRemoved );
  166. assert.equal( valuePassedToRemoved.get(), fooValue.get() );
  167. // Confirm no events are fired when nonexistent value is removed.
  168. wasEventFiredOnRemoval = false;
  169. valuesInstance.remove( 'bar' );
  170. assert.ok( ! wasEventFiredOnRemoval );
  171. });
  172. QUnit.module( 'Customize Base: Notification' );
  173. QUnit.test( 'Notification object exists and has expected properties', function ( assert ) {
  174. var notification = new wp.customize.Notification( 'mycode', {
  175. 'message': 'Hello World',
  176. 'type': 'update',
  177. 'setting': 'blogname',
  178. 'fromServer': true,
  179. 'data': { 'foo': 'bar' }
  180. } );
  181. assert.equal( 'mycode', notification.code );
  182. assert.equal( 'Hello World', notification.message );
  183. assert.equal( 'update', notification.type );
  184. assert.equal( 'blogname', notification.setting );
  185. assert.equal( true, notification.fromServer );
  186. assert.deepEqual( { 'foo': 'bar' }, notification.data );
  187. notification = new wp.customize.Notification( 'mycode2', {
  188. 'message': 'Hello Space'
  189. } );
  190. assert.equal( 'mycode2', notification.code );
  191. assert.equal( 'Hello Space', notification.message );
  192. assert.equal( 'error', notification.type );
  193. assert.equal( null, notification.data );
  194. } );
  195. QUnit.module( 'Customize Base: utils.parseQueryString' );
  196. QUnit.test( 'wp.customize.utils.parseQueryString works', function( assert ) {
  197. var queryParams;
  198. queryParams = wp.customize.utils.parseQueryString( 'a=1&b=2' );
  199. assert.ok( _.isEqual( queryParams, { a: '1', b: '2' } ) );
  200. queryParams = wp.customize.utils.parseQueryString( 'a+b=1&b=Hello%20World' );
  201. assert.ok( _.isEqual( queryParams, { 'a_b': '1', b: 'Hello World' } ) );
  202. queryParams = wp.customize.utils.parseQueryString( 'a%20b=1&b=Hello+World' );
  203. assert.ok( _.isEqual( queryParams, { 'a_b': '1', b: 'Hello World' } ) );
  204. queryParams = wp.customize.utils.parseQueryString( 'a=1&b' );
  205. assert.ok( _.isEqual( queryParams, { 'a': '1', b: null } ) );
  206. queryParams = wp.customize.utils.parseQueryString( 'a=1&b=' );
  207. assert.ok( _.isEqual( queryParams, { 'a': '1', b: '' } ) );
  208. } );
  209. });