Skip to content

Commit

Permalink
fix icon path in wagtail hook registration (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas authored Mar 28, 2024
1 parent 277850c commit 473c708
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

## [Unreleased]

### Fixed

- Fixed a bug where the icons were registered incorrectly and thus not able to be rendered by Django's `render_to_string` function. Reported by @seb-b in #58.

## [0.2.1]

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion src/wagtail_heroicons/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
def register_icons(icons):
template_dir = Path(__file__).parent / "templates" / "wagtail_heroicons"
heroicons = [
f"{file.parent.name}/{file.stem}" for file in template_dir.rglob("*.svg")
f"wagtail_heroicons/{file.parent.name}/{file.name}"
for file in template_dir.rglob("*.svg")
]
return icons + heroicons
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

import django
from django.conf import settings

from .settings import DEFAULT_SETTINGS
Expand All @@ -16,6 +17,7 @@ def pytest_configure(config):
**DEFAULT_SETTINGS,
**TEST_SETTINGS,
)
django.setup()


TEST_SETTINGS = {
Expand All @@ -24,6 +26,7 @@ def pytest_configure(config):
"django.contrib.auth",
"django.contrib.contenttypes",
"wagtail",
"wagtail.admin",
],
"STATIC_URL": "/static/",
"TEMPLATES": [
Expand Down
45 changes: 45 additions & 0 deletions tests/test_icons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

import itertools
import re

import pytest
from django.template.loader import render_to_string
from django.template.utils import get_app_template_dirs
from wagtail import hooks
from wagtail.admin.views.home import icons as wagtail_icons


@pytest.fixture
def icons():
icon_hooks = hooks.get_hooks("register_icons")
all_icons = itertools.chain.from_iterable(hook([]) for hook in icon_hooks)
return sorted(all_icons)


def test_register_icons(icons):
app_template_dirs = get_app_template_dirs("templates")
template_files = [
str(filepath) for dir_ in app_template_dirs for filepath in dir_.rglob("*")
]

assert all(
any(icon in template_file for template_file in template_files) for icon in icons
)


def test_icon_rendering(icons):
for icon in icons:
assert render_to_string(icon)


def test_icon_rendering_in_admin(icons):
# regression test for wagtail-heroicons#58
svg_id = re.compile(r'<svg[^>]*\bid=["\'](.*?)["\']')

rendered_icon = render_to_string(icons[0])
rendered_svg_id = svg_id.search(rendered_icon).group(1)

rendered_wagtail_icons = wagtail_icons()

assert rendered_svg_id in rendered_wagtail_icons
21 changes: 0 additions & 21 deletions tests/test_wagtail_hooks.py

This file was deleted.

0 comments on commit 473c708

Please sign in to comment.