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

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

        soup = self.get_soup(response.content)
        shortcuts_dialog = soup.select_one("#keyboard-shortcuts-dialog")
        self.assertIsNotNone(shortcuts_dialog)

        account_link = shortcuts_dialog.select_one("a[href$='account/']")
        self.assertIsNotNone(account_link)
        self.assertEqual(account_link.text.strip(), "Account")
        self.assertIn("w-underline", account_link.get("class", []))

    def test_modal_shows_disabled_info_when_keyboard_shortcuts_disabled(self):
        """
        Modal should open and show warning if keyboard shortcuts are disabled.
        """
        html = self.render_modal(keyboard_shortcuts_enabled=False)
        self.assertIn("Keyboard shortcuts are currently disabled", html)

    def test_modal_shows_enabled_info_when_shortcuts_enabled(self):
        """
        Modal should show normal info when keyboard shortcuts are enabled.
        """
        html = self.render_modal(keyboard_shortcuts_enabled=True)
        self.assertIn("Keyboard shortcuts are currently enabled", html)

    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",
                    "data-a11y-dialog-show": "keyboard-shortcuts-dialog",
                    "data-action": "w-action#noop:prevent:stop",
                    "data-controller": "w-kbd w-action",
                    "data-w-kbd-key-value": "?",
                }
            ),
            sidebar_data,
        )

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

Review comment (suggestion):
This tests that the template renders the right content. 

However, we'll also need a test for the template being given the right values based on the user profile. 

Maybe have a look at some of the tests that were added in https://github.com/wagtail/wagtail/pull/13258/files - specifically the test_account_management file. This is where you can see an example of a mock of the profile change. 

https://github.com/wagtail/wagtail/blob/bb5b11128b27453c172d65bc2e8f10457bd5a8ad/wagtail/admin/tests/test_account_management.py#L712

Code after:
                    "data-w-kbd-key-value": "?",
                }
            ),
            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())

    @override_settings(WAGTAILADMIN_COMMENTS_ENABLED=True)
    def test_keyboard_shortcuts_with_comments_enabled(self):
        """
        Test the presence of a comments shortcut if Comments 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")
        ]

        self.assertIn("Add or show comments", shortcuts_dialog.prettify())
        self.assertIn("Ctrl + Alt + m", all_shortcuts_text)

    @override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False)
    def test_keyboard_shortcuts_with_comments_disabled(self):
        """
        Test the absence of a comments shortcut if Comments disabled