Browse Source

Fixed #34696 -- Updated selection counter in admin changelist on Chrome.

Cauê Thenório 1 year ago
parent
commit
f7cfa48283
2 changed files with 38 additions and 0 deletions
  1. 3 0
      django/contrib/admin/static/admin/js/actions.js
  2. 35 0
      tests/admin_changelist/tests.py

+ 3 - 0
django/contrib/admin/static/admin/js/actions.js

@@ -179,6 +179,9 @@
                 }
             });
         }
+        // Sync counter when navigating to the page, such as through the back
+        // button.
+        window.addEventListener('pageshow', (event) => updateCounter(actionCheckboxes, options));
     };
 
     // Call function fn when the DOM is loaded and ready. If it is already

+ 35 - 0
tests/admin_changelist/tests.py

@@ -1741,6 +1741,41 @@ class SeleniumTests(AdminSeleniumTestCase):
             self.assertIs(c.get_property("checked"), True)
         self.assertIs(checkboxes[-1].get_property("checked"), False)
 
+    def test_selection_counter_is_synced_when_page_is_shown(self):
+        from selenium.webdriver.common.by import By
+
+        self.admin_login(username="super", password="secret")
+        self.selenium.get(self.live_server_url + reverse("admin:auth_user_changelist"))
+
+        form_id = "#changelist-form"
+        first_row_checkbox_selector = (
+            f"{form_id} #result_list tbody tr:first-child .action-select"
+        )
+        selection_indicator_selector = f"{form_id} .action-counter"
+        selection_indicator = self.selenium.find_element(
+            By.CSS_SELECTOR, selection_indicator_selector
+        )
+        row_checkbox = self.selenium.find_element(
+            By.CSS_SELECTOR, first_row_checkbox_selector
+        )
+        # Select a row.
+        row_checkbox.click()
+        self.assertEqual(selection_indicator.text, "1 of 1 selected")
+        # Go to another page and get back.
+        self.selenium.get(
+            self.live_server_url + reverse("admin:admin_changelist_parent_changelist")
+        )
+        self.selenium.back()
+        # The selection indicator is synced with the selected checkboxes.
+        selection_indicator = self.selenium.find_element(
+            By.CSS_SELECTOR, selection_indicator_selector
+        )
+        row_checkbox = self.selenium.find_element(
+            By.CSS_SELECTOR, first_row_checkbox_selector
+        )
+        selected_rows = 1 if row_checkbox.is_selected() else 0
+        self.assertEqual(selection_indicator.text, f"{selected_rows} of 1 selected")
+
     def test_select_all_across_pages(self):
         from selenium.webdriver.common.by import By