2222 WebpackLoaderBadStatsError ,
2323 WebpackLoaderTimeoutError )
2424from webpack_loader .templatetags .webpack_loader import _WARNING_MESSAGE
25- from webpack_loader .utils import get_loader
25+ from webpack_loader .utils import get_as_tags , get_loader
2626
2727BUNDLE_PATH = os .path .join (
2828 settings .BASE_DIR , 'assets/django_webpack_loader_bundles/' )
2929DEFAULT_CONFIG = 'DEFAULT'
3030_OUR_EXTENSION = 'webpack_loader.contrib.jinja2ext.WebpackExtension'
3131
32- _warn_mock = Mock ()
33-
3432
3533class LoaderTestCase (TestCase ):
3634 def setUp (self ):
@@ -402,10 +400,8 @@ def test_request_blocking(self):
402400 elapsed = time .time () - then
403401 self .assertTrue (elapsed < wait_for )
404402
405- @patch (
406- target = 'webpack_loader.templatetags.webpack_loader.warn' ,
407- new = _warn_mock )
408- def test_emits_warning_on_no_request_in_djangoengine (self ):
403+ @patch (target = 'webpack_loader.templatetags.webpack_loader.warn' )
404+ def test_emits_warning_on_no_request_in_djangoengine (self , warn_mock ):
409405 """
410406 Should emit warnings on having no request in context (django
411407 template).
@@ -427,7 +423,7 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
427423 r'{% render_bundle "app1" %}'
428424 r'{% render_bundle "app2" %}' )) # type: Template
429425 output = dups_template .render (context = Context ())
430- _warn_mock .assert_not_called ()
426+ warn_mock .assert_not_called ()
431427 self .assertEqual (output .count (asset_app1 ), 1 )
432428 self .assertEqual (output .count (asset_app2 ), 1 )
433429 self .assertEqual (output .count (asset_vendor ), 2 )
@@ -442,11 +438,11 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
442438 self .assertEqual (output .count (asset_app1 ), 1 )
443439 self .assertEqual (output .count (asset_app2 ), 1 )
444440 self .assertEqual (output .count (asset_vendor ), 2 )
445- _warn_mock .assert_called_once_with (
441+ warn_mock .assert_called_once_with (
446442 message = _WARNING_MESSAGE , category = RuntimeWarning )
447443
448444 # Should NOT call `warn()` here
449- _warn_mock .reset_mock ()
445+ warn_mock .reset_mock ()
450446 nodups_template = Template (template_string = (
451447 r'{% load render_bundle from webpack_loader %}'
452448 r'{% render_bundle "app1" %}'
@@ -460,13 +456,11 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
460456 self .assertEqual (output .count (asset_app1 ), 1 )
461457 self .assertEqual (output .count (asset_app2 ), 1 )
462458 self .assertEqual (output .count (asset_vendor ), 1 )
463- _warn_mock .assert_not_called ()
464- _warn_mock .reset_mock ()
459+ warn_mock .assert_not_called ()
460+ warn_mock .reset_mock ()
465461
466- @patch (
467- target = 'webpack_loader.templatetags.webpack_loader.warn' ,
468- new = _warn_mock )
469- def test_emits_warning_on_no_request_in_jinja2engine (self ):
462+ @patch (target = 'webpack_loader.templatetags.webpack_loader.warn' )
463+ def test_emits_warning_on_no_request_in_jinja2engine (self , warn_mock ):
470464 'Should emit warnings on having no request in context (Jinja2).'
471465 self .compile_bundles ('webpack.config.skipCommon.js' )
472466 settings = {
@@ -500,7 +494,7 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
500494 dups_template = jinja2_engine .get_template (
501495 template_name = 'home-duplicated.jinja' ) # type: Jinja2Template
502496 output = dups_template .render ()
503- _warn_mock .assert_not_called ()
497+ warn_mock .assert_not_called ()
504498 self .assertEqual (output .count (asset_app1 ), 2 )
505499 self .assertEqual (output .count (asset_app2 ), 2 )
506500 self .assertEqual (output .count (asset_vendor ), 4 )
@@ -515,13 +509,13 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
515509 self .assertEqual (output .count (asset_app1 ), 2 )
516510 self .assertEqual (output .count (asset_app2 ), 2 )
517511 self .assertEqual (output .count (asset_vendor ), 4 )
518- self .assertEqual (_warn_mock .call_count , 3 )
512+ self .assertEqual (warn_mock .call_count , 3 )
519513 self .assertListEqual (
520- _warn_mock .call_args_list ,
514+ warn_mock .call_args_list ,
521515 [warning_call , warning_call , warning_call ])
522516
523517 # Should NOT call `warn()` here
524- _warn_mock .reset_mock ()
518+ warn_mock .reset_mock ()
525519 request = self .factory .get (path = '/' )
526520 with self .settings (** settings ):
527521 jinja2_engine = engines ['jinja2' ] # type: Jinja2
@@ -535,17 +529,15 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
535529 self .assertEqual (output .count (asset_app1 ), 1 )
536530 self .assertEqual (output .count (asset_app2 ), 1 )
537531 self .assertEqual (output .count (asset_vendor ), 1 )
538- _warn_mock .assert_not_called ()
539- _warn_mock .reset_mock ()
532+ warn_mock .assert_not_called ()
533+ warn_mock .reset_mock ()
540534
541- @patch (
542- target = 'webpack_loader.templatetags.webpack_loader.warn' ,
543- new = _warn_mock )
544- def test_get_files_emits_warning_on_no_request (self ):
535+ @patch (target = 'webpack_loader.templatetags.webpack_loader.warn' )
536+ def test_get_files_emits_warning_on_no_request_in_djangoengine (self , warn_mock ):
545537 self .compile_bundles ('webpack.config.skipCommon.js' )
546- asset_vendor = '" /static/django_webpack_loader_bundles/vendors.js" '
547- asset_app1 = '" /static/django_webpack_loader_bundles/app1.js" '
548- asset_app2 = '" /static/django_webpack_loader_bundles/app2.js" '
538+ asset_vendor = '/static/django_webpack_loader_bundles/vendors.js'
539+ asset_app1 = '/static/django_webpack_loader_bundles/app1.js'
540+ asset_app2 = '/static/django_webpack_loader_bundles/app2.js'
549541
550542 template = Template (template_string = (
551543 '{% load render_bundle get_files from webpack_loader %}'
@@ -559,7 +551,43 @@ def test_get_files_emits_warning_on_no_request(self):
559551 self .assertEqual (output .count (asset_app1 ), 1 )
560552 self .assertEqual (output .count (asset_app2 ), 1 )
561553 self .assertEqual (output .count (asset_vendor ), 2 )
562- _warn_mock .assert_called_once_with (
554+ warn_mock .assert_called_once_with (
555+ message = _WARNING_MESSAGE , category = RuntimeWarning )
556+
557+ @patch (target = 'webpack_loader.templatetags.webpack_loader.warn' )
558+ def test_get_files_emits_warning_on_no_request_in_jinja2engine (self , warn_mock ):
559+ self .compile_bundles ('webpack.config.skipCommon.js' )
560+ asset_vendor = '/static/django_webpack_loader_bundles/vendors.js'
561+ asset_app1 = '/static/django_webpack_loader_bundles/app1.js'
562+ asset_app2 = '/static/django_webpack_loader_bundles/app2.js'
563+ settings = {
564+ 'TEMPLATES' : [
565+ {
566+ 'NAME' : 'jinja2' ,
567+ 'BACKEND' : 'django_jinja.backend.Jinja2' ,
568+ 'APP_DIRS' : True ,
569+ 'OPTIONS' : {
570+ 'match_extension' : '.jinja' ,
571+ 'extensions' : DEFAULT_EXTENSIONS + [_OUR_EXTENSION ],
572+ }
573+ },
574+ ]
575+ }
576+
577+ with self .settings (** settings ):
578+ jinja2_engine = engines ['jinja2' ] # type: Jinja2
579+ template = jinja2_engine .from_string (
580+ "{{ render_bundle('app1', 'js') }}"
581+ "{% set app2_files = webpack_get_files('app2', skip_common_chunks=True) %}"
582+ "{% for f in app2_files %}"
583+ " <link rel='prefetch' href='{{ f.url }}' />"
584+ "{% endfor %}"
585+ )
586+ output = template .render (context = Context ())
587+ self .assertEqual (output .count (asset_app1 ), 1 )
588+ self .assertEqual (output .count (asset_app2 ), 1 )
589+ self .assertEqual (output .count (asset_vendor ), 2 )
590+ warn_mock .assert_called_once_with (
563591 message = _WARNING_MESSAGE , category = RuntimeWarning )
564592
565593 def _assert_common_chunks_duplicated_djangoengine (self , template ):
@@ -803,3 +831,22 @@ def test_skip_common_chunks_missing_config(self):
803831
804832 # return removed key
805833 loader .config ['SKIP_COMMON_CHUNKS' ] = skip_common_chunks
834+
835+ def test_get_as_tags_direct_usage (self ):
836+ self .compile_bundles ('webpack.config.skipCommon.js' )
837+
838+ asset_vendor = (
839+ '<script src="/static/django_webpack_loader_bundles/vendors.js" >'
840+ '</script>' )
841+ asset_app1 = (
842+ '<link href="/static/django_webpack_loader_bundles/app1.css" rel="stylesheet" />' )
843+ asset_app2 = (
844+ '<script src="/static/django_webpack_loader_bundles/app1.js" >'
845+ '</script>' )
846+
847+ tags = get_as_tags ('app1' )
848+
849+ self .assertEqual (len (tags ), 3 )
850+ self .assertEqual (tags [0 ], asset_vendor )
851+ self .assertEqual (tags [1 ], asset_app1 )
852+ self .assertEqual (tags [2 ], asset_app2 )
0 commit comments