updates.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*global QUnit, wp, sinon */
  2. jQuery( function( $ ) {
  3. QUnit.module( 'wp.updates' );
  4. QUnit.test( 'Initially, the update lock should be false', function( assert ) {
  5. assert.strictEqual( wp.updates.ajaxLocked, false );
  6. });
  7. QUnit.test( 'The nonce should be set correctly', function( assert ) {
  8. assert.equal( wp.updates.ajaxNonce, window._wpUpdatesSettings.ajax_nonce );
  9. });
  10. QUnit.test( 'decrementCount correctly decreases the update number', function( assert ) {
  11. var menuItemCount = $( '#menu-plugins' ).find( '.plugin-count' ).eq( 0 ).text();
  12. var screenReaderItemCount = $( '#wp-admin-bar-updates' ).find( '.screen-reader-text' ).text();
  13. var adminItemCount = $( '#wp-admin-bar-updates' ).find( '.ab-label' ).text();
  14. assert.equal( menuItemCount, 2, 'Intial value is correct' );
  15. assert.equal( screenReaderItemCount, '2 Plugin Updates', 'Intial value is correct' );
  16. assert.equal( adminItemCount, 2, 'Intial value is correct' );
  17. wp.updates.decrementCount( 'plugin' );
  18. // Re-read these values.
  19. menuItemCount = $( '#menu-plugins' ).find( '.plugin-count' ).eq( 0 ).text();
  20. screenReaderItemCount = $( '#wp-admin-bar-updates' ).find( '.screen-reader-text' ).text();
  21. adminItemCount = $( '#wp-admin-bar-updates' ).find( '.ab-label' ).text();
  22. assert.equal( menuItemCount, 1 );
  23. // @todo: Update screen reader count.
  24. // Should the screenReader count change? Is that announced to the user?
  25. // assert.equal( screenReaderItemCount, '1 Plugin Update' );
  26. assert.equal( adminItemCount, 1 );
  27. });
  28. QUnit.test( '`beforeunload` should only fire when locked', function( assert ) {
  29. wp.updates.ajaxLocked = false;
  30. assert.notOk( wp.updates.beforeunload(), '`beforeunload` should not fire.' );
  31. wp.updates.ajaxLocked = true;
  32. assert.equal( wp.updates.beforeunload(), window._wpUpdatesSettings.l10n.beforeunload, '`beforeunload` should equal the localized `beforeunload` string.' );
  33. wp.updates.ajaxLocked = false;
  34. });
  35. // FTP creds... exist?
  36. // Admin notice?
  37. QUnit.module( 'wp.updates.plugins', {
  38. beforeEach: function() {
  39. this.oldPagenow = window.pagenow;
  40. window.pagenow = 'plugins';
  41. sinon.spy( jQuery, 'ajax' );
  42. },
  43. afterEach: function() {
  44. window.pagenow = this.oldPagenow;
  45. wp.updates.ajaxLocked = false;
  46. wp.updates.queue = [];
  47. jQuery.ajax.restore();
  48. }
  49. } );
  50. QUnit.test( 'Update lock is set when plugins are updating', function( assert ) {
  51. wp.updates.updatePlugin( {
  52. plugin: 'test/test.php',
  53. slug: 'test'
  54. } );
  55. assert.strictEqual( wp.updates.ajaxLocked, true );
  56. });
  57. QUnit.test( 'Plugins are queued when the lock is set', function( assert ) {
  58. var value = [
  59. {
  60. action: 'update-plugin',
  61. data: {
  62. plugin: 'test/test.php',
  63. slug: 'test',
  64. success: null,
  65. error: null
  66. }
  67. }
  68. ];
  69. wp.updates.ajaxLocked = true;
  70. wp.updates.updatePlugin( {
  71. plugin: 'test/test.php',
  72. slug: 'test',
  73. success: null,
  74. error: null
  75. } );
  76. assert.deepEqual( wp.updates.queue, value );
  77. });
  78. QUnit.test( 'If plugins are installing (lock is set), the beforeUnload function should fire', function( assert ) {
  79. wp.updates.updatePlugin( {
  80. plugin: 'test/test.php',
  81. slug: 'test'
  82. } );
  83. assert.equal( wp.updates.beforeunload(), window._wpUpdatesSettings.l10n.beforeunload );
  84. } );
  85. QUnit.test( 'Starting a plugin update should call the update API', function( assert ) {
  86. wp.updates.updatePlugin( {
  87. plugin: 'test/test.php',
  88. slug: 'test'
  89. } );
  90. assert.ok( jQuery.ajax.calledOnce );
  91. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  92. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'update-plugin' );
  93. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'test' );
  94. } );
  95. QUnit.test( 'Installing a plugin should call the API', function( assert ) {
  96. wp.updates.installPlugin( { slug: 'jetpack' } );
  97. assert.ok( jQuery.ajax.calledOnce );
  98. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  99. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'install-plugin' );
  100. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'jetpack' );
  101. } );
  102. QUnit.test( 'Deleting a plugin should call the API', function( assert ) {
  103. wp.updates.deletePlugin( { slug: 'jetpack', plugin: 'jetpack/jetpack.php' } );
  104. assert.ok( jQuery.ajax.calledOnce );
  105. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  106. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'delete-plugin' );
  107. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'jetpack' );
  108. } );
  109. // QUnit.test( 'A successful update changes the message?', function( assert ) {} );
  110. // QUnit.test( 'A failed update changes the message?', function( assert ) {} );
  111. QUnit.module( 'wp.updates.themes', {
  112. beforeEach: function() {
  113. this.oldPagenow = window.pagenow;
  114. window.pagenow = 'themes';
  115. sinon.spy( jQuery, 'ajax' );
  116. },
  117. afterEach: function() {
  118. window.pagenow = this.oldPagenow;
  119. wp.updates.ajaxLocked = false;
  120. wp.updates.queue = [];
  121. jQuery.ajax.restore();
  122. }
  123. } );
  124. QUnit.test( 'Update lock is set when themes are updating', function( assert ) {
  125. wp.updates.updateTheme( 'twentyeleven' );
  126. assert.strictEqual( wp.updates.ajaxLocked, true );
  127. });
  128. QUnit.test( 'If themes are installing (lock is set), the beforeUnload function should fire', function( assert ) {
  129. wp.updates.updateTheme( { slug: 'twentyeleven' } );
  130. assert.equal( wp.updates.beforeunload(), window._wpUpdatesSettings.l10n.beforeunload );
  131. } );
  132. QUnit.test( 'Starting a theme update should call the update API', function( assert ) {
  133. wp.updates.updateTheme( { slug: 'twentyeleven' } );
  134. assert.ok( jQuery.ajax.calledOnce );
  135. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  136. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'update-theme' );
  137. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'twentyeleven' );
  138. } );
  139. QUnit.test( 'Installing a theme should call the API', function( assert ) {
  140. wp.updates.installTheme( { slug: 'twentyeleven' } );
  141. assert.ok( jQuery.ajax.calledOnce );
  142. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  143. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'install-theme' );
  144. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'twentyeleven' );
  145. } );
  146. QUnit.test( 'Deleting a theme should call the API', function( assert ) {
  147. wp.updates.deleteTheme( { slug: 'twentyeleven' } );
  148. assert.ok( jQuery.ajax.calledOnce );
  149. assert.equal( jQuery.ajax.getCall( 0 ).args[0].url, '/wp-admin/admin-ajax.php' );
  150. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.action, 'delete-theme' );
  151. assert.equal( jQuery.ajax.getCall( 0 ).args[0].data.slug, 'twentyeleven' );
  152. } );
  153. // QUnit.test( 'A successful update changes the message?', function( assert ) {} );
  154. // QUnit.test( 'A failed update changes the message?', function( assert ) {} );
  155. });