Skip to content

Commit

Permalink
Fix pattern matching for templates using dashes (#230)
Browse files Browse the repository at this point in the history
* Fix pattern matching for templates using dashes

* Expand tests
  • Loading branch information
zerolab authored Oct 25, 2023
1 parent 34f3072 commit a294ccd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pattern_library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
# UI
re_path(r"^$", views.IndexView.as_view(), name="index"),
re_path(
r"^pattern/(?P<pattern_template_name>[\w./-\\]+%s)$"
r"^pattern/(?P<pattern_template_name>[\w./\-\\]+%s)$"
% (get_pattern_template_suffix()),
views.IndexView.as_view(),
name="display_pattern",
),
# iframe rendering
re_path(
r"^render-pattern/(?P<pattern_template_name>[\w./-\\]+%s)$"
r"^render-pattern/(?P<pattern_template_name>[\w./\-\\]+%s)$"
% (get_pattern_template_suffix()),
views.RenderPatternView.as_view(),
name="render_pattern",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template-with-dash
25 changes: 25 additions & 0 deletions tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ def test_pretty_names_from_filename(self):
self.assertEqual(display_link.text.strip(), "test_molecule_no_context.html")
self.assertEqual(render_link.text.strip(), pattern_path)

def test_pretty_names_from_filename_containing_dashes(self):
pattern_path = "patterns/molecules/test-molecule/test-molecule.html"
test_molecule_display_url = reverse(
"pattern_library:display_pattern",
kwargs={"pattern_template_name": pattern_path},
)
test_molecule_render_url = reverse(
"pattern_library:render_pattern",
kwargs={"pattern_template_name": pattern_path},
)

response = self.client.get(test_molecule_display_url)
self.assertEqual(response.status_code, 200)

soup = BeautifulSoup(response.content, features="html.parser")

display_link = soup.select_one(
f'.list__item>a[href="{test_molecule_display_url}"]'
)
render_link = soup.select_one(f'a[href="{test_molecule_render_url}"]')

self.assertEqual(display_link.text.strip(), "test-molecule.html")
self.assertEqual(render_link.text.strip(), pattern_path)

def test_includes(self):
pattern_path = "patterns/atoms/test_includes/test_includes.html"
display_url = reverse(
Expand Down Expand Up @@ -92,6 +116,7 @@ def test_fragments(self):
for template_name in [
"patterns/atoms/test_atom/test_atom.html",
"patterns/molecules/test_molecule/test_molecule.html",
"patterns/molecules/test-molecule/test-molecule.html",
]:
with self.subTest(template_name=template_name):
self.assertContains(
Expand Down

0 comments on commit a294ccd

Please sign in to comment.