A platform for community discussion. Free, open, simple.
FIX: Preserve search terms when opening advanced search (#43904)
Renato Atilioa64dc185f042
diff --git a/frontend/discourse/app/controllers/full-page-search.js b/frontend/discourse/app/controllers/full-page-search.jsindex c18c1050ca4..2750440e813 100644--- a/frontend/discourse/app/controllers/full-page-search.js+++ b/frontend/discourse/app/controllers/full-page-search.js@@ -117,6 +117,7 @@ export default class FullPageSearchController extends Controller {/** @type {string | null | undefined} */error = null;+ #syncedQ;_searchOnSortChange = true;init() {@@ -413,6 +414,7 @@ export default class FullPageSearchController extends Controller {}setSearchTerm(term) {+ this.#syncedQ = term;this._searchOnSortChange = false;term = this.cleanTerm(term);this._searchOnSortChange = true;@@ -453,6 +455,12 @@ export default class FullPageSearchController extends Controller {@observes("search_type")triggerSearchOnTypeChange() {+ // Runs before the `q` observer, so a URL change to both would otherwise+ // search with the previous term.+ if (this.q !== this.#syncedQ) {+ this.setSearchTerm(this.q);+ }+if (this.searchActive) {this.set("page", 1);this._search();@@ -472,6 +480,8 @@ export default class FullPageSearchController extends Controller {if (model && this.get("model.q") !== this.q) {this.setSearchTerm(this.q);this.send("search");+ } else {+ this.#syncedQ = this.q;}}diff --git a/frontend/discourse/tests/acceptance/search-full-test.js b/frontend/discourse/tests/acceptance/search-full-test.jsindex d1b004bec32..25ee48fd827 100644--- a/frontend/discourse/tests/acceptance/search-full-test.js+++ b/frontend/discourse/tests/acceptance/search-full-test.js@@ -157,6 +157,25 @@ acceptance("Search - Full Page", function (needs) {.hasClass("active", "shows that the post search type is in effect");});+ test("a URL change to both the term and the type searches the new term", async function (assert) {+ await visit(`/search?q=dev&search_type=${SEARCH_TYPE_USERS}`);+ await visit("/search?q=consectetur");++ assert.dom(".search-query").hasValue("consectetur", "shows the new term");+ assert+ .dom(".fps-topic")+ .exists({ count: 1 }, "shows results for the new term");+ });++ test("picking a type after the URL drops the term keeps what was typed", async function (assert) {+ await visit("/search?q=dev");+ await visit("/search");+ await fillIn(".search-query", "consectetur");+ await click(`.search-types__type[data-search-type="${SEARCH_TYPE_USERS}"]`);++ assert.dom(".search-query").hasValue("consectetur");+ });+test("search for personal messages", async function (assert) {await visit("/search");diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.jsindex c3df0da4fca..27c1bb3666b 100644--- a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js@@ -6,6 +6,7 @@ import {find,findAll,focus,+ getSettledState,settled,triggerKeyEvent,visit,@@ -1014,6 +1015,48 @@ acceptance("AI Discoveries - header search", function (needs) {);});+ [+ ["on the first search", null],+ ["after an empty search", ""],+ ["after a previous search", "previous"],+ ].forEach(([scenario, previousTerm]) => {+ test(`opening advanced search preserves a pending answer ${scenario}`, async function (assert) {+ if (previousTerm !== null) {+ await visit(`/search?q=${encodeURIComponent(previousTerm)}`);+ }+ await visit("/");+ await click("#search-button");+ await fillIn("#icon-search-input", "my term");+ find(".ai-discoveries-search-options__option.--ask").dispatchEvent(+ new MouseEvent("click", { bubbles: true })+ );+ await waitUntil(() => submittedRequestId);+ find(".ai-discoveries-search-options__option.--advanced").dispatchEvent(+ new MouseEvent("click", { bubbles: true })+ );+ await waitFor(".full-page-search");+ // The pending answer keeps a timeout scheduled while the route settles.+ await waitUntil(() => {+ const state = getSettledState();+ return (+ !state.hasRunLoop &&+ !state.hasPendingRequests &&+ !state.hasPendingTransitions &&+ !state.isRenderPending+ );+ });++ assert+ .dom(".full-page-search")+ .hasValue("my term", "preserves the query while the answer is loading");+ assert.strictEqual(+ new URL(currentURL(), "https://discourse.test").searchParams.get("q"),+ "my term",+ "preserves the query in the URL"+ );+ });+ });+test("an indexed search carries through to the full page unchanged", async function (assert) {await visit("/");await click("#search-button");