From 05d47b6f3b286eaeaad56acd59d37d427f21c558 Mon Sep 17 00:00:00 2001 From: umesh Date: Tue, 24 Nov 2026 17:53:24 +0530 Subject: [PATCH] Themes: Restore the browse URL when re-selecting the active tab. On Appearance > Add Themes, opening the feature filter drawer from a browse tab such as Favorites strips `?browse=` from the URL. Clicking the still-active tab afterward closed the drawer but returned early, leaving the URL stuck at `theme-install.php`. Update the URL to match the active tab when `onSort()` bails because the tab is already active. Add a QUnit test, and load the real `theme.js` in the QUnit suite, with its screen initialization disabled, so it can be tested. --- src/js/_enqueues/wp/theme.js | 3 +- tests/qunit/index.html | 8 ++++++ tests/qunit/wp-admin/js/theme.js | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/js/_enqueues/wp/theme.js b/src/js/_enqueues/wp/theme.js index dabd64cc6c6ba..7c1a3138ecd6c 100644 --- a/src/js/_enqueues/wp/theme.js +++ b/src/js/_enqueues/wp/theme.js @@ -1781,8 +1781,9 @@ themes.view.Installer = themes.view.Appearance.extend({ $( 'body' ).removeClass( 'filters-applied show-filters' ); $( '.drawer-toggle' ).attr( 'aria-expanded', 'false' ); - // Bail if this is already active. + // Bail if this is already active, restoring the URL if the filter drawer changed it. if ( $el.hasClass( this.activeClass ) ) { + themes.router.navigate( themes.router.baseUrl( themes.router.browsePath + sort ) ); return; } diff --git a/tests/qunit/index.html b/tests/qunit/index.html index f9033a776151c..e622566001f73 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -92,6 +92,14 @@ + + + diff --git a/tests/qunit/wp-admin/js/theme.js b/tests/qunit/wp-admin/js/theme.js index d82b26e1521db..cd82eecef5a95 100644 --- a/tests/qunit/wp-admin/js/theme.js +++ b/tests/qunit/wp-admin/js/theme.js @@ -135,4 +135,51 @@ assert.equal( prevCalled, 0, 'Ctrl+Left does nothing' ); } ); } ); + QUnit.module( 'Theme Installer sorting', function( hooks ) { + var originalRouter, originalSort, originalClearSearch, navigated, view; + + hooks.beforeEach( function() { + navigated = []; + originalRouter = _.clone( wp.themes.router ); + wp.themes.router.browsePath = '?browse='; + wp.themes.router.baseUrl = function( url ) { + return 'theme-install.php' + url; + }; + wp.themes.router.navigate = function( url ) { + navigated.push( url ); + }; + + $( '#qunit-fixture' ).html( + '
' + + '' + + '' + + '
' + ); + $( 'body' ).addClass( 'show-filters' ); + + view = Object.create( wp.themes.view.Installer.prototype ); + originalSort = view.sort; + originalClearSearch = view.clearSearch; + view.activeClass = 'current'; + view.sort = function() {}; + view.clearSearch = function() {}; + } ); + + hooks.afterEach( function() { + wp.themes.router = originalRouter; + view.sort = originalSort; + view.clearSearch = originalClearSearch; + $( 'body' ).removeClass( 'show-filters filters-applied' ); + } ); + + QUnit.test( 'Clicking the active tab restores its URL and closes the filter drawer', function( assert ) { + var $link = $( '.filter-links a' ); + + view.onSort( $.Event( 'click', { target: $link[ 0 ] } ) ); + + assert.strictEqual( navigated.length, 1, 'The router is updated once.' ); + assert.ok( /\?browse=favorites$/.test( navigated[ 0 ] ), 'The URL is restored to the active tab.' ); + assert.notOk( $( 'body' ).hasClass( 'show-filters' ), 'The filter drawer is closed.' ); + } ); + } ); })( jQuery );