Skip to content

Commit

Permalink
add tests for multi-app loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Niicck committed Oct 24, 2023
1 parent ab8eb5e commit 95439ad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
14 changes: 14 additions & 0 deletions tests/data/external_vue_app/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"src/entry.js": {
"css": [
"assets/entry-0ed1a6fd.css"
],
"file": "assets/entry-5c085aac.js",
"isEntry": true,
"src": "entry.js"
},
"src/entry.css": {
"file": "assets/entry-0ed1a6fd.css",
"src": "entry.css"
}
}
93 changes: 81 additions & 12 deletions tests/tests/templatetags/test_vite_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@


@pytest.mark.usefixtures("dev_mode_true")
def test_vite_asset_returns_dev_tags():
template = Template(
@pytest.mark.parametrize("as_default", [True, False])
def test_vite_asset_returns_dev_tags(as_default):
if as_default:
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" app="default" %}
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" %}
"""
)
)
else:
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
Expand All @@ -23,13 +32,22 @@ def test_vite_asset_returns_dev_tags():


@pytest.mark.usefixtures("dev_mode_false")
def test_vite_asset_returns_production_tags():
template = Template(
@pytest.mark.parametrize("as_default", [True, False])
def test_vite_asset_returns_production_tags(as_default):
if as_default:
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" app="default" %}
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" %}
"""
)
)
else:
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.ts" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
Expand Down Expand Up @@ -215,3 +233,54 @@ def test_vite_asset_nonexistent_app(dev_mode_true):
template.render(Context({}))

assert "Cannot find bad_app in DJANGO_VITE settings" in str(error)


@pytest.fixture()
def external_vue_app(patch_settings, settings):
def _wrapper(dev_mode: bool):
return patch_settings(
{
"DJANGO_VITE": {
"external_vue_app": {
"dev_mode": dev_mode,
"static_url_prefix": "custom/prefix",
"dev_server_port": 5555,
"manifest_path": settings.STATIC_ROOT.parent
/ "external_vue_app"
/ "manifest.json",
}
}
}
)

return _wrapper


def test_vite_asset_external_app_dev(external_vue_app):
external_vue_app(dev_mode=True)
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.js" app="external_vue_app" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
assert (
script_tag["src"] == "http://localhost:5555/static/custom/prefix/src/entry.js"
)


def test_vite_asset_external_app_production(external_vue_app):
external_vue_app(dev_mode=False)
template = Template(
"""
{% load django_vite %}
{% vite_asset "src/entry.js" app="external_vue_app" %}
"""
)
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
assert script_tag["src"] == "custom/prefix/assets/entry-5c085aac.js"

0 comments on commit 95439ad

Please sign in to comment.