Repository: wagtail/wagtail
File: wagtail/admin/tests/test_keyboard_shortcuts.py (Python)

Code before:
                }
            ),
            sidebar_data,
        )

    def test_keyboard_shortcuts_dialog(self):
        response = self.client.get(reverse("wagtailadmin_home"))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(
            response, "wagtailadmin/shared/keyboard_shortcuts_dialog.html"
        )

        soup = self.get_soup(response.content)

        # Check that the keyboard shortcuts dialog is present
        shortcuts_dialog = soup.select_one("#keyboard-shortcuts-dialog")
        self.assertIsNotNone(shortcuts_dialog)

        # Check that the keyboard shortcuts dialog has basic accessible content
        self.assertIn(
            "All keyboard shortcuts", shortcuts_dialog.find("caption").prettify()
        )
        self.assertIn("Keyboard shortcut", shortcuts_dialog.find("thead").prettify())

    @patch("wagtail.admin.templatetags.wagtailadmin_tags.get_comments_enabled")
    def test_keyboard_shortcuts_comments_visibility(self, mock_comments_enabled):
        """
        Test the presence or absence of the 'Comments' shortcut based on settings.
        """

        for is_enabled in [True, False]:
            with self.subTest(comments_enabled=is_enabled):
                mock_comments_enabled.return_value = is_enabled

                response = self.client.get(reverse("wagtailadmin_home"))
                self.assertEqual(response.status_code, 200)
                self.assertTemplateUsed(
                    response, "wagtailadmin/shared/keyboard_shortcuts_dialog.html"
                )

                soup = self.get_soup(response.content)

                shortcuts_dialog = soup.select_one("#keyboard-shortcuts-dialog")
                all_shortcuts_text = [
                    kbd.string.strip() for kbd in shortcuts_dialog.select("kbd")
                ]

                if is_enabled:
                    self.assertIn("Ctrl + Alt + m", all_shortcuts_text)
                else:

Review comment (refactor):
I understand what you are doing here, but we have an existing approach to mocking when comments are enabled or not.

Instead of mocking the function, let's mock the root setting. Django has a great way to do this


```
from django.test import override_settings
# ...
@override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False)
```

If you search for that you will see this being used a lot of places throughout the tests.

It should also allow you to simplify this test without the nested `with`.

Finally, it means you will have to split the two(true/false) scenarios into two tests, this is also a good practice as it allows for explicit tests without too much 'smarts' (avoiding `for`). While this can mean a bit of duplication, we find that for unit tests generally it makes them easier to understand and maintain.

Code after:
        """
        self.client.defaults["HTTP_USER_AGENT"] = (
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
        )
        response = self.client.get(reverse("wagtailadmin_home"))

        key_labels = get_keyboard_key_labels_from_request(response.wsgi_request)

        self.assertEqual(key_labels.ALT, "⌥")
        self.assertEqual(key_labels.CMD, "⌘")
        self.assertEqual(key_labels.CTRL, "^")
        self.assertEqual(key_labels.ENTER, "Return")
        self.assertEqual(key_labels.MOD, "⌘")

    def test_get_keyboard_key_labels_for_windows(self):
        """
        Test the keyboard key labels with a Windows user agent.
        """
        self.client.defaults["HTTP_USER_AGENT"] = (
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
        )
        response = self.client.get(reverse("wagtailadmin_home"))

        key_labels = get_keyboard_key_labels_from_request(response.wsgi_request)

        self.assertEqual(key_labels.ALT, "Alt")
        self.assertEqual(key_labels.CMD, "Ctrl")
        self.assertEqual(key_labels.CTRL, "Ctrl")
        self.assertEqual(key_labels.MOD, "Ctrl")


class TestKeyboardShortcutsDialog(WagtailTestUtils, TestCase):
    def setUp(self):
        self.login()

    def test_keyboard_shortcuts_trigger_in_sidebar(self):
        response = self.client.get(reverse("wagtailadmin_home"))
        self.assertEqual(response.status_code, 200)

        sidebar_data = (
            self.get_soup(response.content)
            .select_one("#wagtail-sidebar-props")
            .contents[0]
        )

        self.assertIn(
            json.dumps(
                {
                    "role": "button",