Skip to content

Commit 9d9dbf6

Browse files
committed
Restored search hotkey for desktop.
1 parent e81dd71 commit 9d9dbf6

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
run: |
4040
python -m pip install --upgrade pip setuptools
4141
python -m pip install -r requirements/tests.txt
42+
- name: Install playwright browsers
43+
run: |
44+
python -m playwright install --with-deps
4245
- name: Set up databases
4346
run: |
4447
PGPASSWORD="postgres" createuser -U postgres -d djangoproject --superuser -h localhost

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ COPY ./requirements ./requirements
3131
RUN apt-get update \
3232
&& apt-get install --assume-yes --no-install-recommends ${BUILD_DEPENDENCIES} \
3333
&& python3 -m pip install --no-cache-dir -r ${REQ_FILE} \
34+
&& if [ "${REQ_FILE}" = "requirements/tests.txt" ]; then \
35+
echo "Installing Playwright browsers..."; \
36+
playwright install --with-deps; \
37+
fi \
3438
&& apt-get purge --assume-yes --auto-remove ${BUILD_DEPENDENCIES} \
3539
&& apt-get distclean
3640

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ APP_LIST ?= accounts aggregator blog contact dashboard djangoproject docs founda
44
SCSS = djangoproject/scss
55
STATIC = djangoproject/static
66

7-
ci: compilemessages test
7+
ci: compilemessages collectstatics test
88
@python -m coverage report
99

1010
compilemessages:

djangoproject/static/js/djangoproject.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ document.querySelector('.menu-button').addEventListener('click', function () {
6565
menu_el.classList.toggle('active');
6666
});
6767

68+
function querySelectorVisible(selector) {
69+
return [...document.querySelectorAll(selector)].find(
70+
(el) => el.offsetParent !== null,
71+
);
72+
}
73+
6874
// Update search input placeholder text based on the user's operating system
6975
(function () {
70-
const el = document.getElementById('id_q');
76+
const el = querySelectorVisible('#id_q');
7177

7278
if (!el) {
7379
return;
@@ -94,7 +100,7 @@ window.addEventListener('keydown', function (e) {
94100

95101
e.preventDefault();
96102

97-
const el = document.querySelector('#id_q');
103+
const el = querySelectorVisible('#id_q');
98104

99105
el.select();
100106
el.focus();

djangoproject/tests.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import os
12
from http import HTTPStatus
23
from io import StringIO
34

45
from django.conf import settings
6+
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
57
from django.core.management import call_command
68
from django.test import TestCase
79
from django.urls import NoReverseMatch, get_resolver
810
from django.utils.translation import activate, gettext as _
911
from django_hosts.resolvers import reverse
12+
from playwright.sync_api import sync_playwright
1013

1114
from docs.models import DocumentRelease, Release
1215

@@ -211,3 +214,59 @@ def test_single_h1_per_page(self):
211214
response = self.client.get(url)
212215
self.assertEqual(response.status_code, 200)
213216
self.assertContains(response, "<h1", count=1)
217+
218+
219+
class EndToEndTests(ReleaseMixin, StaticLiveServerTestCase):
220+
@classmethod
221+
def setUpClass(cls):
222+
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
223+
super().setUpClass()
224+
cls.playwright = sync_playwright().start()
225+
cls.browser = cls.playwright.chromium.launch()
226+
227+
@classmethod
228+
def tearDownClass(cls):
229+
super().tearDownClass()
230+
cls.browser.close()
231+
cls.playwright.stop()
232+
233+
def setUp(self):
234+
self.setUpTestData()
235+
236+
def test_search_ctrl_k_hotkey_desktop(self):
237+
page = self.browser.new_page()
238+
page.goto(self.live_server_url)
239+
240+
mobile_search_bar, desktop_search_bar = page.query_selector_all("#id_q")
241+
self.assertFalse(mobile_search_bar.is_visible())
242+
self.assertTrue(desktop_search_bar.is_visible())
243+
is_focused = page.evaluate(
244+
"el => document.activeElement === el", desktop_search_bar
245+
)
246+
self.assertFalse(is_focused)
247+
248+
page.keyboard.press("Control+KeyK")
249+
is_focused = page.evaluate(
250+
"el => document.activeElement === el", desktop_search_bar
251+
)
252+
self.assertTrue(is_focused)
253+
page.close()
254+
255+
def test_search_ctrl_k_hotkey_mobile(self):
256+
page = self.browser.new_page(viewport={"width": 375, "height": 812})
257+
page.goto(self.live_server_url)
258+
259+
mobile_search_bar, desktop_search_bar = page.query_selector_all("#id_q")
260+
self.assertTrue(mobile_search_bar.is_visible())
261+
self.assertFalse(desktop_search_bar.is_visible())
262+
is_focused = page.evaluate(
263+
"el => document.activeElement === el", mobile_search_bar
264+
)
265+
self.assertFalse(is_focused)
266+
267+
page.keyboard.press("Control+KeyK")
268+
is_focused = page.evaluate(
269+
"el => document.activeElement === el", mobile_search_bar
270+
)
271+
self.assertTrue(is_focused)
272+
page.close()

requirements/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-r dev.txt
22
coverage==7.11.3
3+
playwright==1.56.0
34
requests-mock==1.12.1
45
tblib>=3.0.0

0 commit comments

Comments
 (0)