From f7cfa48283c94018861688f17ab3935d588fbcc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Then=C3=B3rio?= Date: Fri, 7 Jul 2023 10:58:07 -0300 Subject: [PATCH] Fixed #34696 -- Updated selection counter in admin changelist on Chrome. --- .../contrib/admin/static/admin/js/actions.js | 3 ++ tests/admin_changelist/tests.py | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/django/contrib/admin/static/admin/js/actions.js b/django/contrib/admin/static/admin/js/actions.js index 20a5c14353..6a2ae91a19 100644 --- a/django/contrib/admin/static/admin/js/actions.js +++ b/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 diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 8fdd343b18..a926f9d826 100644 --- a/tests/admin_changelist/tests.py +++ b/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