From 130f73f292ad6f0cb81e1c6628285b36ec450921 Mon Sep 17 00:00:00 2001
From: Anton Kulyk <kuliks.anton@gmail.com>
Date: Wed, 9 Jun 2021 12:04:34 +0300
Subject: [PATCH] Fix can't "Select All" collection items if all items are
 pinned (#16501)

* Add repro for #16497 (pinned items bulk selection)

* Fix pinned items bulk selection

* Remove redundant check

* Refactor existing "bulk actions" tests

* Extract test that is not related to bulk actions

* Add `pinAllRootItems()` helper function

* Unify "bulk selection" tests

Co-authored-by: Nemanja <31325167+nemanjaglumac@users.noreply.github.com>
---
 .../containers/CollectionContent.jsx          |  7 +-
 .../collections/collections.cy.spec.js        | 72 ++++++++++++-------
 2 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/frontend/src/metabase/collections/containers/CollectionContent.jsx b/frontend/src/metabase/collections/containers/CollectionContent.jsx
index f39657a082f..3252fd47463 100644
--- a/frontend/src/metabase/collections/containers/CollectionContent.jsx
+++ b/frontend/src/metabase/collections/containers/CollectionContent.jsx
@@ -166,16 +166,13 @@ function CollectionContent({
                   const showFilters =
                     filter || unpinnedItems.length >= MIN_ITEMS_TO_SHOW_FILTERS;
 
-                  const unselected = unpinnedItems.filter(
+                  const unselected = [...pinnedItems, ...unpinnedItems].filter(
                     item => !getIsSelected(item),
                   );
                   const hasUnselected = unselected.length > 0;
 
                   const handleSelectAll = () => {
-                    const pinnedUnselcted = pinnedItems.filter(
-                      item => !getIsSelected(item),
-                    );
-                    toggleAll([...unselected, ...pinnedUnselcted]);
+                    toggleAll(unselected);
                   };
 
                   return (
diff --git a/frontend/test/metabase/scenarios/collections/collections.cy.spec.js b/frontend/test/metabase/scenarios/collections/collections.cy.spec.js
index 591feb2598a..9581f062c6c 100644
--- a/frontend/test/metabase/scenarios/collections/collections.cy.spec.js
+++ b/frontend/test/metabase/scenarios/collections/collections.cy.spec.js
@@ -583,36 +583,44 @@ describe("scenarios > collection_defaults", () => {
       cy.findByText("First Collection");
     });
 
-    describe("bulk actions", () => {
-      beforeEach(() => {
-        cy.visit("/collection/root");
-        openEllipsisMenuFor("Orders in a dashboard");
-        cy.findByText("Pin this item").click();
-      });
+    it("should be possible to select pinned item using checkbox (metabase#15338)", () => {
+      cy.visit("/collection/root");
+      openEllipsisMenuFor("Orders in a dashboard");
+      cy.findByText("Pin this item").click();
 
-      it("should be possible to apply bulk selection to items (metabase#14705)", () => {
-        selectItemUsingCheckbox("Orders");
-        cy.findByText("1 item selected").should("be.visible");
-        selectItemUsingCheckbox("Orders in a dashboard", "dashboard");
-        cy.findByText("2 items selected").should("be.visible");
+      cy.findByText(/Pinned items/i);
+      selectItemUsingCheckbox("Orders in a dashboard", "dashboard");
+      cy.findByText("1 item selected");
+    });
 
-        // Select all
-        cy.icon("dash").click();
-        cy.icon("dash").should("not.exist");
-        cy.findByText("4 items selected");
+    describe("bulk actions", () => {
+      describe("selection", () => {
+        it("should be possible to apply bulk selection to all items (metabase#14705)", () => {
+          bulkSelectDeselectWorkflow();
+        });
 
-        // Deselect all
-        cy.findByTestId("bulk-action-bar").within(() => {
-          cy.icon("check").click();
+        it("should be possible to apply bulk selection when all items are pinned (metabase#16497)", () => {
+          pinAllRootItems();
+          bulkSelectDeselectWorkflow();
         });
-        cy.icon("check").should("not.exist");
-        cy.findByTestId("bulk-action-bar").should("not.be.visible");
-      });
 
-      it("should be possible to select pinned item using checkbox (metabase#15338)", () => {
-        cy.findByText(/Pinned items/i);
-        selectItemUsingCheckbox("Orders in a dashboard", "dashboard");
-        cy.findByText("1 item selected");
+        function bulkSelectDeselectWorkflow() {
+          cy.visit("/collection/root");
+          selectItemUsingCheckbox("Orders");
+          cy.findByText("1 item selected").should("be.visible");
+
+          // Select all
+          cy.icon("dash").click();
+          cy.icon("dash").should("not.exist");
+          cy.findByText("4 items selected");
+
+          // Deselect all
+          cy.findByTestId("bulk-action-bar").within(() => {
+            cy.icon("check").click();
+          });
+          cy.icon("check").should("not.exist");
+          cy.findByTestId("bulk-action-bar").should("not.be.visible");
+        }
       });
     });
   });
@@ -669,3 +677,17 @@ function getSidebarCollectionChildrenFor(item) {
     .parent()
     .parent();
 }
+
+function pinAllRootItems() {
+  cy.request("GET", "/api/collection/root/items").then(resp => {
+    const ALL_ITEMS = resp.body.data;
+
+    ALL_ITEMS.forEach(({ model, id }, index) => {
+      if (model !== "collection") {
+        cy.request("PUT", `/api/${model}/${id}`, {
+          collection_position: index++,
+        });
+      }
+    });
+  });
+}
-- 
GitLab