From 3f3c9552e2473684a5b14fec1f7578ef58942bdc Mon Sep 17 00:00:00 2001
From: github-automation-metabase
 <166700802+github-automation-metabase@users.noreply.github.com>
Date: Mon, 18 Nov 2024 11:16:40 -0500
Subject: [PATCH] search result order (#50048) (#50151)

Co-authored-by: Nick Fitzpatrick <nick@metabase.com>
---
 .../onboarding/command-palette.cy.spec.js     | 27 +++++++++++++++++--
 .../palette/hooks/useCommandPalette.tsx       |  2 +-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
index 979830c3037..a22fc6f4e07 100644
--- a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
+++ b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
@@ -87,12 +87,12 @@ describe("command palette", () => {
       // Check that we are not filtering search results by action name
       commandPaletteInput().clear().type("Company");
       cy.findByRole("option", { name: /View and filter/ }).should("exist");
-      cy.findByRole("option", { name: "PEOPLE" }).should(
+      cy.findByRole("option", { name: "REVIEWS" }).should(
         "have.attr",
         "aria-selected",
         "true",
       );
-      cy.findByRole("option", { name: "REVIEWS" }).should("exist");
+      cy.findByRole("option", { name: "PEOPLE" }).should("exist");
       cy.findByRole("option", { name: "PRODUCTS" }).should("exist");
       commandPaletteInput().clear();
 
@@ -139,6 +139,29 @@ describe("command palette", () => {
       .should("have.attr", "aria-selected", "true");
   });
 
+  it("should display search results in the order returned by the API", () => {
+    cy.visit("/");
+
+    cy.findByRole("button", { name: /Search/ }).click();
+    cy.intercept("/api/search?*").as("searchData");
+
+    commandPalette().within(() => {
+      commandPaletteInput().type("Cou");
+      cy.wait("@searchData");
+      cy.findByText("Loading...").should("not.exist");
+
+      cy.get("@searchData").then(({ response }) => {
+        const results = response.body.data;
+
+        results.forEach((result, index) => {
+          cy.findAllByRole("option")
+            .eq(index + 2)
+            .should("contain.text", result.name);
+        });
+      });
+    });
+  });
+
   it("should render links to site settings in settings pages", () => {
     cy.visit("/admin");
     cy.findByRole("heading", { name: "Getting set up" }).should("exist");
diff --git a/frontend/src/metabase/palette/hooks/useCommandPalette.tsx b/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
index 25f33551871..90c506f2261 100644
--- a/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
+++ b/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
@@ -188,7 +188,7 @@ export const useCommandPalette = ({
               icon: icon.name,
               section: "search",
               keywords: debouncedSearchText,
-              priority: Priority.NORMAL,
+              priority: Priority.NORMAL - index,
               perform: () => {
                 trackSearchClick("item", index, "command-palette");
               },
-- 
GitLab