customize-controls-utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* global wp */
  2. jQuery( function( $ ) {
  3. var trueMockEvent, falseMockEvent, mockElementLists, $firstMockElement, $secondMockElement, $thirdMockElement,
  4. BubbleTester, BubbleTesterTwoValues, bubbleTesterParent, firstBubbleTester, secondBubbleTester;
  5. QUnit.module( 'Customizer Model Utility functions' );
  6. trueMockEvent = {
  7. type : 'keydown',
  8. which : 14
  9. };
  10. falseMockEvent = {
  11. type : 'keydown',
  12. which : 13
  13. };
  14. QUnit.test( 'isKeydownButNotEnterEvent returns true', function( assert ) {
  15. assert.ok( wp.customize.utils.isKeydownButNotEnterEvent( trueMockEvent ) );
  16. });
  17. QUnit.test( 'isKeydownButNotEnterEvent returns false', function( assert ) {
  18. assert.equal( wp.customize.utils.isKeydownButNotEnterEvent( falseMockEvent ) , false );
  19. });
  20. $firstMockElement = $( '<div id="foo"></div>' );
  21. $secondMockElement = $( '<li id="bar"></li>' );
  22. $thirdMockElement = $( '<div id="thirdElement"></div>' );
  23. mockElementLists = {
  24. first : [ $firstMockElement , $secondMockElement ],
  25. second : [ $secondMockElement ],
  26. firstInReverseOrder : [ $secondMockElement , $firstMockElement ],
  27. third : [ $firstMockElement, $secondMockElement ],
  28. thirdButLonger : [ $firstMockElement, $secondMockElement, $thirdMockElement ]
  29. };
  30. QUnit.test( 'areElementListsEqual returns true', function( assert ) {
  31. assert.ok( wp.customize.utils.areElementListsEqual( mockElementLists.first , mockElementLists.first ) );
  32. });
  33. QUnit.test( 'areElementListsEqual returns false', function( assert ) {
  34. assert.equal( wp.customize.utils.areElementListsEqual( mockElementLists.first , mockElementLists.second ) , false );
  35. });
  36. QUnit.test( 'areElementListsEqual: lists have same values, but in reverse order', function( assert ) {
  37. assert.equal( wp.customize.utils.areElementListsEqual( mockElementLists.first , mockElementLists.firstInReverseOrder ) , false );
  38. });
  39. QUnit.test( 'areElementListsEqual: lists have same values, but one is longer', function( assert ) {
  40. assert.equal( wp.customize.utils.areElementListsEqual( mockElementLists.third , mockElementLists.thirdButLonger ) , false );
  41. });
  42. bubbleTesterParent = function() {
  43. this.trigger = function( event , instance ) {
  44. this.wasChangeTriggered = true;
  45. this.instancePassedInTrigger = instance;
  46. };
  47. this.wasChangeTriggered = false;
  48. this.instancePassedInTrigger = {};
  49. };
  50. BubbleTester = wp.customize.Class.extend(
  51. {
  52. parent : new bubbleTesterParent(),
  53. fooValue : new wp.customize.Value()
  54. },
  55. {
  56. staticProperty : 'propertyValue'
  57. }
  58. );
  59. QUnit.test( 'bubbleChildValueChanges notifies parent of change', function( assert ) {
  60. firstBubbleTester = new BubbleTester();
  61. wp.customize.utils.bubbleChildValueChanges( firstBubbleTester , [ 'fooValue' ] );
  62. firstBubbleTester.fooValue.set( 'new value' );
  63. assert.ok( firstBubbleTester.parent.wasChangeTriggered );
  64. });
  65. QUnit.test( 'bubbleChildValueChanges passes a reference to its instance', function( assert ) {
  66. assert.ok( firstBubbleTester.parent.instancePassedInTrigger instanceof BubbleTester );
  67. });
  68. BubbleTesterTwoValues = wp.customize.Class.extend(
  69. {
  70. parent : new bubbleTesterParent(),
  71. exampleValue : new wp.customize.Value(),
  72. barValue : new wp.customize.Value()
  73. },
  74. {
  75. staticProperty : 'propertyValue'
  76. }
  77. );
  78. secondBubbleTester = new BubbleTesterTwoValues();
  79. wp.customize.utils.bubbleChildValueChanges( secondBubbleTester , [ 'exampleValue' , 'barValue' ] );
  80. secondBubbleTester.barValue.set( 'new value' );
  81. QUnit.test( 'bubbleChildValueChanges notifies parent of change when two values are bound', function( assert ) {
  82. assert.ok( secondBubbleTester.parent.wasChangeTriggered );
  83. });
  84. QUnit.test( 'bubbleChildValueChanges passes a reference to its instance when two values are bound', function( assert ) {
  85. assert.ok( secondBubbleTester.parent.instancePassedInTrigger instanceof BubbleTesterTwoValues );
  86. });
  87. });