Skip to content

Commit

Permalink
feat: keyboard shortcuts unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Karthik Ayangar <karthik.ayangar7118@gmail.com>
  • Loading branch information
kituuu committed Mar 13, 2024
1 parent 5626d4a commit 56e5d1b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
{% load wagtailadmin_tags i18n %}
{% dialog icon_name='regex' id='keyboard-shortcuts-dialog' title=_("Keyboard shortcuts") %}
{% if shortcuts%}
<table class="w-keyboard-shortcuts">
<caption class="w-sr-only">
{% trans "All keyboard shortcuts" %}
</caption>
<thead class="w-sr-only">
<tr>
<th scope="col">{% trans "Section" %}</th>
<th scope="col">{% trans "Keyboard shortcut" %}</th>
</tr>
</thead>
<table class="w-keyboard-shortcuts">
<caption class="w-sr-only">
{% trans "All keyboard shortcuts" %}
</caption>
<thead class="w-sr-only">
<tr>
<th scope="col">{% trans "Section" %}</th>
<th scope="col">{% trans "Keyboard shortcut" %}</th>
</tr>
</thead>

{% for category, shortcuts in shortcuts.items %}
<tbody class="w-w-full" id="keyboard-shortut-group-{{ category.0 }}">
<tr colspan="2">
<th class="w-keyboard-shortcuts__category" scope="rowgroup">{{ category.1 }}</th>
{% for category, shortcuts in shortcuts.items %}
<tbody class="w-w-full" id="keyboard-shortut-group-{{ category.0 }}">
<tr colspan="2">
<th class="w-keyboard-shortcuts__category" scope="rowgroup">{{ category.1 }}</th>
</tr>
{% for shortcut in shortcuts %}
<tr class="w-keyboard_shortcuts__key">
<th class="w-keyboard_shortcuts__label" scope="row">{{ shortcut.label }}</th>
<td class="w-keyboard_shortcuts__symbol"><kbd>{{ shortcut.shortcut }}</kbd></td>
</tr>
{% for shortcut in shortcuts %}
<tr class="w-keyboard_shortcuts__key ">
<th class="w-keyboard_shortcuts__label" scope="row">{{ shortcut.label }}</th>
<td class="w-keyboard_shortcuts__symbol"><kbd>{{ shortcut.shortcut }}</kbd></td>
</tr>
{% endfor %}
</tbody>
{% endfor %}
</table>
{% endif%}
{% endfor %}
</tbody>
{% endfor %}
</table>
{% enddialog %}
78 changes: 78 additions & 0 deletions wagtail/admin/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import re
import unittest

from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.core import mail
from django.core.management import call_command
from django.test import TestCase, override_settings
from django.test.client import Client
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
from taggit.models import Tag
Expand Down Expand Up @@ -532,3 +534,79 @@ def test_remove_stale_content_types_preserves_access_admin_permission(self):
content_type__app_label="wagtailadmin", codename="access_admin"
).exists()
)


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


class TestMacKeyboardShortcutsDialog(WagtailTestUtils, TestCase):
def setUp(self):
# Creates a client with a Mac user agent
self.client = Client(
headers={
"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"
}
)
self.login()

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

# Check that the user agent is a Mac
user_agent = response.context["request"].headers.get("User-Agent", "")
is_mac = re.search(r"Mac|iPod|iPhone|iPad", user_agent)

# Add assertions based on expected Mac behavior
self.assertTrue(is_mac)

# Check that the keyboard shortcuts dialog has Mac-specific content
soup = self.get_soup(response.content)
shortcuts_dialog = soup.select_one("#keyboard-shortcuts-dialog")
all_shortcuts = shortcuts_dialog.select("kbd")
for shortcut in all_shortcuts:
# All shortcuts should have the ⌘ symbol
self.assertIn("⌘", shortcut.prettify())
4 changes: 2 additions & 2 deletions wagtail/test/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,15 +1160,15 @@ class CountryCode(models.TextChoices):

some_attribute = "some value"

workflow_states = GenericRelation(
local_workflow_states = GenericRelation(
"wagtailcore.WorkflowState",
content_type_field="base_content_type",
object_id_field="object_id",
related_query_name="full_featured_snippet",
for_concrete_model=False,
)

revisions = GenericRelation(
local_revisions = GenericRelation(
"wagtailcore.Revision",
content_type_field="base_content_type",
object_id_field="object_id",
Expand Down

0 comments on commit 56e5d1b

Please sign in to comment.