From 34a83fe1c8da417aeed81a2cc0a90cc7fc69841a Mon Sep 17 00:00:00 2001 From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com> Date: Thu, 1 Feb 2024 09:44:40 -0700 Subject: [PATCH] DEV: Soft load topic results after closing then opening search menu (#25504) # Context Meta report: https://meta.discourse.org/t/dropdown-search-box-results-now-cleared-after-clicking-on-1-result/291240/1 # Demo https://github.com/discourse/discourse/assets/50783505/3b258b2c-b346-4c81-a7b9-0c1bb6bd4b28 # Plugin Test Fix - https://github.com/discourse/discourse-encrypt/pull/304 --- .../discourse/app/components/search-menu.hbs | 2 - .../discourse/app/components/search-menu.js | 14 +++--- .../app/components/search-menu/results.js | 2 +- .../discourse/app/services/search.js | 1 + .../tests/acceptance/glimmer-search-test.js | 46 +++++++++++++++++-- .../components/search-menu-test.gjs | 20 +++++++- 6 files changed, 72 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/search-menu.hbs b/app/assets/javascripts/discourse/app/components/search-menu.hbs index 3a98b0c2b08..c10cfe633c2 100644 --- a/app/assets/javascripts/discourse/app/components/search-menu.hbs +++ b/app/assets/javascripts/discourse/app/components/search-menu.hbs @@ -55,7 +55,6 @@ { + this.search.results.resultTypes?.map((resultType) => { content.push({ ...resultType, component: SEARCH_RESULTS_COMPONENT_TYPE[resultType.componentName], diff --git a/app/assets/javascripts/discourse/app/services/search.js b/app/assets/javascripts/discourse/app/services/search.js index 0b9a3419d7e..d50a5b5b4ee 100644 --- a/app/assets/javascripts/discourse/app/services/search.js +++ b/app/assets/javascripts/discourse/app/services/search.js @@ -13,6 +13,7 @@ export default class Search extends Service { @tracked highlightTerm; @tracked inTopicContext = false; @tracked visible = false; + @tracked results = {}; // only relative for the widget search menu searchContextEnabled = false; // checkbox to scope search diff --git a/app/assets/javascripts/discourse/tests/acceptance/glimmer-search-test.js b/app/assets/javascripts/discourse/tests/acceptance/glimmer-search-test.js index ba80be06a20..9cd352d6744 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/glimmer-search-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/glimmer-search-test.js @@ -440,7 +440,16 @@ acceptance("Search - Glimmer - Authenticated", function (needs) { needs.pretender((server, helper) => { server.get("/search/query", (request) => { - if (request.queryParams.term.includes("empty")) { + if (request.queryParams.type_filter === DEFAULT_TYPE_FILTER) { + // posts/topics are not present in the payload by default + return helper.response({ + users: searchFixtures["search/query"]["users"], + categories: searchFixtures["search/query"]["categories"], + groups: searchFixtures["search/query"]["groups"], + grouped_search_result: + searchFixtures["search/query"]["grouped_search_result"], + }); + } else if (request.queryParams.term.includes("empty")) { return helper.response({ posts: [], users: [], @@ -464,9 +473,9 @@ acceptance("Search - Glimmer - Authenticated", function (needs) { group_ids: [], }, }); + } else { + return helper.response(searchFixtures["search/query"]); } - - return helper.response(searchFixtures["search/query"]); }); server.get("/inline-onebox", () => @@ -736,6 +745,20 @@ acceptance("Search - Glimmer - with tagging enabled", function (needs) { needs.user(); needs.settings({ tagging_enabled: true }); needs.pretender((server, helper) => { + server.get("/search/query", (request) => { + if (request.queryParams.type_filter === DEFAULT_TYPE_FILTER) { + // posts/topics are not present in the payload by default + return helper.response({ + users: searchFixtures["search/query"]["users"], + categories: searchFixtures["search/query"]["categories"], + groups: searchFixtures["search/query"]["groups"], + grouped_search_result: + searchFixtures["search/query"]["grouped_search_result"], + }); + } + return helper.response(searchFixtures["search/query"]); + }); + server.get("/tag/dev/notifications", () => { return helper.response({ tag_notification: { id: "dev", notification_level: 2 }, @@ -903,6 +926,9 @@ acceptance("Search - Glimmer - with tagging enabled", function (needs) { acceptance("Search - Glimmer - assistant", function (needs) { needs.user(); needs.pretender((server, helper) => { + server.get("/t/2179.json", () => { + return helper.response({}); + }); server.get("/search/query", (request) => { if (request.queryParams["search_context[type]"] === "private_messages") { // return only one result for PM search @@ -1185,4 +1211,18 @@ acceptance("Search - Glimmer - assistant", function (needs) { `sam #${firstCategoryName}` ); }); + + test("topic results - soft loads the topic results after closing then search menu", async function (assert) { + await visit("/"); + await click("#search-button"); + await fillIn("#search-term", "Development mode"); + + // navigate to topic and close search menu + const firstTopicResult = ".search-menu .results .search-result-topic a"; + await click(firstTopicResult); + + // reopen search menu and previous search results are present + await click("#search-button"); + assert.dom(firstTopicResult).exists(); + }); }); diff --git a/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs index 7939548f3c6..b03c92fb9f3 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs +++ b/app/assets/javascripts/discourse/tests/integration/components/search-menu-test.gjs @@ -1,7 +1,11 @@ import { click, fillIn, render, triggerKeyEvent } from "@ember/test-helpers"; import { module, test } from "qunit"; -import SearchMenu from "discourse/components/search-menu"; +import SearchMenu, { + DEFAULT_TYPE_FILTER, +} from "discourse/components/search-menu"; +import searchFixtures from "discourse/tests/fixtures/search-fixtures"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; +import pretender, { response } from "discourse/tests/helpers/create-pretender"; import { exists, query } from "discourse/tests/helpers/qunit-helpers"; import I18n from "discourse-i18n"; @@ -12,6 +16,20 @@ module("Integration | Component | search-menu", function (hooks) { setupRenderingTest(hooks); test("rendering standalone", async function (assert) { + pretender.get("/search/query", (request) => { + if (request.queryParams.type_filter === DEFAULT_TYPE_FILTER) { + // posts/topics are not present in the payload by default + return response({ + users: searchFixtures["search/query"]["users"], + categories: searchFixtures["search/query"]["categories"], + groups: searchFixtures["search/query"]["groups"], + grouped_search_result: + searchFixtures["search/query"]["grouped_search_result"], + }); + } + return response(searchFixtures["search/query"]); + }); + await render(); assert.ok(