Skip to content

Commit

Permalink
tests: add tests of webpack module
Browse files Browse the repository at this point in the history
* Adds test of WebpackThemeBundle, UniqueJinjaManifestEntry, and
  UniqueJinjaManifestLoader.
  • Loading branch information
lnielsen committed May 13, 2020
1 parent 3958811 commit 1bd5aaa
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions invenio_assets/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

"""Media asset management for Invenio."""


def collect_staticroot_removal(app, blueprints):
"""Remove collect's static root folder from list."""
collect_root = app.extensions['collect'].static_root
Expand Down
2 changes: 2 additions & 0 deletions invenio_assets/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def __html__(self):
for p in self._paths:
_, ext = os.path.splitext(p.lower())
# If we haven't come across the chunk yet, we add it to the output
if ext not in request._jinja_webpack_entries:
raise UnsupportedExtensionError(p)
if p not in request._jinja_webpack_entries[ext]:
tpl = self.templates.get(ext)
if tpl is None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_invenio_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

"""Test Invenio Assets module."""

from invenio_assets import InvenioAssets
from mock import patch
from pywebpack.storage import FileStorage, LinkStorage

from invenio_assets import InvenioAssets


def test_version():
"""Test version import."""
Expand Down
87 changes: 87 additions & 0 deletions tests/test_webpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2020 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Test Webpack module."""

import pytest
from flask_webpackext import WebpackBundle
from pywebpack import UnsupportedExtensionError

from invenio_assets.webpack import UniqueJinjaManifestEntry, \
UniqueJinjaManifestLoader, WebpackThemeBundle


def test_webpack_theme_bundle(app):
"""Test WebpackThemeBundle."""
themes = {
'bootstrap3': dict(
entry={
'theme': './theme-bootstrap3.css',
}
),
'semanticui': dict(
entry={
'theme': './theme-semanticui.css',
}
),
}

# Test that bundles are created for each theme.
bundle = WebpackThemeBundle(
'tests', 'assets', default='semanticui', themes=themes)
assert isinstance(bundle.themes['bootstrap3'], WebpackBundle)
assert isinstance(bundle.themes['semanticui'], WebpackBundle)

# Test that default theme is used.
with app.app_context():
assert bundle._active_theme_bundle == bundle.themes['semanticui']
assert bundle.path == bundle.themes['semanticui'].path
assert bundle.entry == bundle.themes['semanticui'].entry
assert bundle.dependencies == bundle.themes['semanticui'].dependencies

# Test that APP_THEME overrides default theme.
app.config['APP_THEME'] = ['bootstrap3']
with app.app_context():
assert bundle._active_theme_bundle == bundle.themes['bootstrap3']
assert bundle.path == bundle.themes['bootstrap3'].path
assert bundle.entry == bundle.themes['bootstrap3'].entry
assert bundle.dependencies == bundle.themes['bootstrap3'].dependencies

# Test that an invalid APP_THEME
app.config['APP_THEME'] = ['invalid']
with app.app_context():
assert bundle._active_theme_bundle is None


def test_unique_jinja_manifest_entry(app):
"""Test UniqueJinjaManifestEntry."""
# b.js is only output once, despite being twice in manifest
m = UniqueJinjaManifestEntry('manifestname', ['/a.js', '/b.js', '/b.js'])
with app.test_request_context():
assert m.__html__() == \
'<script src="/a.js"></script>\n' \
'<script src="/b.js"></script>'

# b.js is only output once, despite being twice in manifest
app.debug = True
with app.test_request_context():
assert m.__html__() == \
'<!-- manifestname -->\n' \
'<script src="/a.js"></script>\n' \
'<script src="/b.js"></script>'

# Unsupported file extension
m = UniqueJinjaManifestEntry('script', ['/a.less'])
with app.test_request_context():
pytest.raises(UnsupportedExtensionError, m.__html__)


def test_unique_jinja_manifest_loader(app):
"""Test UniqueJinjaManifestLoader."""
loader = UniqueJinjaManifestLoader()
assert loader.entry_cls == UniqueJinjaManifestEntry

0 comments on commit 1bd5aaa

Please sign in to comment.