22
22
WebpackLoaderBadStatsError ,
23
23
WebpackLoaderTimeoutError )
24
24
from 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
26
26
27
27
BUNDLE_PATH = os .path .join (
28
28
settings .BASE_DIR , 'assets/django_webpack_loader_bundles/' )
29
29
DEFAULT_CONFIG = 'DEFAULT'
30
30
_OUR_EXTENSION = 'webpack_loader.contrib.jinja2ext.WebpackExtension'
31
31
32
- _warn_mock = Mock ()
33
-
34
32
35
33
class LoaderTestCase (TestCase ):
36
34
def setUp (self ):
@@ -402,10 +400,8 @@ def test_request_blocking(self):
402
400
elapsed = time .time () - then
403
401
self .assertTrue (elapsed < wait_for )
404
402
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 ):
409
405
"""
410
406
Should emit warnings on having no request in context (django
411
407
template).
@@ -427,7 +423,7 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
427
423
r'{% render_bundle "app1" %}'
428
424
r'{% render_bundle "app2" %}' )) # type: Template
429
425
output = dups_template .render (context = Context ())
430
- _warn_mock .assert_not_called ()
426
+ warn_mock .assert_not_called ()
431
427
self .assertEqual (output .count (asset_app1 ), 1 )
432
428
self .assertEqual (output .count (asset_app2 ), 1 )
433
429
self .assertEqual (output .count (asset_vendor ), 2 )
@@ -442,11 +438,11 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
442
438
self .assertEqual (output .count (asset_app1 ), 1 )
443
439
self .assertEqual (output .count (asset_app2 ), 1 )
444
440
self .assertEqual (output .count (asset_vendor ), 2 )
445
- _warn_mock .assert_called_once_with (
441
+ warn_mock .assert_called_once_with (
446
442
message = _WARNING_MESSAGE , category = RuntimeWarning )
447
443
448
444
# Should NOT call `warn()` here
449
- _warn_mock .reset_mock ()
445
+ warn_mock .reset_mock ()
450
446
nodups_template = Template (template_string = (
451
447
r'{% load render_bundle from webpack_loader %}'
452
448
r'{% render_bundle "app1" %}'
@@ -460,13 +456,11 @@ def test_emits_warning_on_no_request_in_djangoengine(self):
460
456
self .assertEqual (output .count (asset_app1 ), 1 )
461
457
self .assertEqual (output .count (asset_app2 ), 1 )
462
458
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 ()
465
461
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 ):
470
464
'Should emit warnings on having no request in context (Jinja2).'
471
465
self .compile_bundles ('webpack.config.skipCommon.js' )
472
466
settings = {
@@ -500,7 +494,7 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
500
494
dups_template = jinja2_engine .get_template (
501
495
template_name = 'home-duplicated.jinja' ) # type: Jinja2Template
502
496
output = dups_template .render ()
503
- _warn_mock .assert_not_called ()
497
+ warn_mock .assert_not_called ()
504
498
self .assertEqual (output .count (asset_app1 ), 2 )
505
499
self .assertEqual (output .count (asset_app2 ), 2 )
506
500
self .assertEqual (output .count (asset_vendor ), 4 )
@@ -515,13 +509,13 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
515
509
self .assertEqual (output .count (asset_app1 ), 2 )
516
510
self .assertEqual (output .count (asset_app2 ), 2 )
517
511
self .assertEqual (output .count (asset_vendor ), 4 )
518
- self .assertEqual (_warn_mock .call_count , 3 )
512
+ self .assertEqual (warn_mock .call_count , 3 )
519
513
self .assertListEqual (
520
- _warn_mock .call_args_list ,
514
+ warn_mock .call_args_list ,
521
515
[warning_call , warning_call , warning_call ])
522
516
523
517
# Should NOT call `warn()` here
524
- _warn_mock .reset_mock ()
518
+ warn_mock .reset_mock ()
525
519
request = self .factory .get (path = '/' )
526
520
with self .settings (** settings ):
527
521
jinja2_engine = engines ['jinja2' ] # type: Jinja2
@@ -535,17 +529,15 @@ def test_emits_warning_on_no_request_in_jinja2engine(self):
535
529
self .assertEqual (output .count (asset_app1 ), 1 )
536
530
self .assertEqual (output .count (asset_app2 ), 1 )
537
531
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 ()
540
534
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 ):
545
537
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'
549
541
550
542
template = Template (template_string = (
551
543
'{% load render_bundle get_files from webpack_loader %}'
@@ -559,7 +551,43 @@ def test_get_files_emits_warning_on_no_request(self):
559
551
self .assertEqual (output .count (asset_app1 ), 1 )
560
552
self .assertEqual (output .count (asset_app2 ), 1 )
561
553
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 (
563
591
message = _WARNING_MESSAGE , category = RuntimeWarning )
564
592
565
593
def _assert_common_chunks_duplicated_djangoengine (self , template ):
@@ -803,3 +831,22 @@ def test_skip_common_chunks_missing_config(self):
803
831
804
832
# return removed key
805
833
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