44from subprocess import call
55from threading import Thread
66
7+ import django
78from django .conf import settings
89from django .test import TestCase , RequestFactory
9- from webpack_loader . utils import get_assets
10+ from django_jinja . builtins import DEFAULT_EXTENSIONS
1011from django .views .generic .base import TemplateView
1112
12-
13- view = TemplateView .as_view (template_name = 'home.html' )
13+ from webpack_loader .utils import get_assets , get_bundle , WebpackException
1414
1515
1616BUNDLE_PATH = os .path .join (settings .BASE_DIR , 'assets/bundles/' )
@@ -30,38 +30,7 @@ def compile_bundles(self, config, wait=None):
3030 time .sleep (wait )
3131 call (['./node_modules/.bin/webpack' , '--config' , os .path .join ('assets/' , config )])
3232
33- def test_request_blocking (self ):
34- # FIXME: This will work 99% time but there is no garauntee with the
35- # 4 second thing. Need a better way to detect if request was blocked on
36- # not.
37- wait_for = 3
38-
39- with self .settings (DEBUG = True ):
40- open (settings .WEBPACK_LOADER ['STATS_FILE' ], 'w' ).write (json .dumps ({'status' : 'compiling' }))
41- then = time .time ()
42- request = self .factory .get ('/' )
43- result = view (request )
44- t = Thread (target = self .compile_bundles , args = ('webpack.config.simple.js' , wait_for ))
45- t .start ()
46- result .rendered_content
47- elapsed = time .time () - then
48- t .join ()
49- self .assertTrue (elapsed > wait_for )
50-
51- with self .settings (DEBUG = False ):
52- self .compile_bundles ('webpack.config.simple.js' )
53- then = time .time ()
54- request = self .factory .get ('/' )
55- result = view (request )
56- result .rendered_content
57- elapsed = time .time () - then
58- self .assertTrue (elapsed < wait_for )
59-
60- def test_reporting_errors (self ):
61- #TODO:
62- pass
63-
64- def test_simple (self ):
33+ def test_simple_and_css_extract (self ):
6534 self .compile_bundles ('webpack.config.simple.js' )
6635 assets = get_assets ()
6736 self .assertEqual (assets ['status' ], 'done' )
@@ -75,7 +44,7 @@ def test_simple(self):
7544 self .assertEqual (main [0 ]['path' ], os .path .join (settings .BASE_DIR , 'assets/bundles/main.js' ))
7645 self .assertEqual (main [1 ]['path' ], os .path .join (settings .BASE_DIR , 'assets/bundles/styles.css' ))
7746
78- def test_multiple_files (self ):
47+ def test_code_spliting (self ):
7948 self .compile_bundles ('webpack.config.split.js' )
8049 assets = get_assets ()
8150 self .assertEqual (assets ['status' ], 'done' )
@@ -90,3 +59,80 @@ def test_multiple_files(self):
9059
9160 vendor = chunks ['vendor' ]
9261 self .assertEqual (vendor [0 ]['path' ], os .path .join (settings .BASE_DIR , 'assets/bundles/vendor.js' ))
62+
63+ def test_jinja2 (self ):
64+ self .compile_bundles ('webpack.config.simple.js' )
65+ view = TemplateView .as_view (template_name = 'home.jinja' )
66+
67+ if django .VERSION >= (1 , 8 ):
68+ settings = {
69+ 'TEMPLATES' : [
70+ {
71+ "BACKEND" : "django_jinja.backend.Jinja2" ,
72+ "APP_DIRS" : True ,
73+ "OPTIONS" : {
74+ "match_extension" : ".jinja" ,
75+ "extensions" : DEFAULT_EXTENSIONS + [
76+ "webpack_loader.contrib.jinja2ext.WebpackExtension" ,
77+ ]
78+
79+ }
80+ },
81+ ]
82+ }
83+ else :
84+ settings = {
85+ 'TEMPLATE_LOADERS' : (
86+ 'django_jinja.loaders.FileSystemLoader' ,
87+ 'django_jinja.loaders.AppLoader' ,
88+ ),
89+ }
90+ with self .settings (** settings ):
91+ request = self .factory .get ('/' )
92+ result = view (request )
93+ self .assertIn ('<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet">' , result .rendered_content )
94+ self .assertIn ('<script type="text/javascript" src="/static/bundles/main.js"></script>' , result .rendered_content )
95+
96+ def test_reporting_errors (self ):
97+ #TODO:
98+ self .compile_bundles ('webpack.config.error.js' )
99+ try :
100+ get_bundle ('main' )
101+ except WebpackException as e :
102+ self .assertIn ("Cannot resolve module 'the-library-that-did-not-exist'" , e .message )
103+
104+ def test_missing_stats_file (self ):
105+ os .remove (settings .WEBPACK_LOADER ['STATS_FILE' ])
106+ try :
107+ get_assets ()
108+ except IOError as e :
109+ expected = 'Error reading {}. Are you sure webpack has generated the file and the path is correct?' .format (settings .WEBPACK_LOADER ['STATS_FILE' ])
110+ self .assertIn (expected , str (e ))
111+
112+ def test_request_blocking (self ):
113+ # FIXME: This will work 99% time but there is no garauntee with the
114+ # 4 second thing. Need a better way to detect if request was blocked on
115+ # not.
116+ wait_for = 3
117+ view = TemplateView .as_view (template_name = 'home.html' )
118+
119+ with self .settings (DEBUG = True ):
120+ open (settings .WEBPACK_LOADER ['STATS_FILE' ], 'w' ).write (json .dumps ({'status' : 'compiling' }))
121+ then = time .time ()
122+ request = self .factory .get ('/' )
123+ result = view (request )
124+ t = Thread (target = self .compile_bundles , args = ('webpack.config.simple.js' , wait_for ))
125+ t .start ()
126+ result .rendered_content
127+ elapsed = time .time () - then
128+ t .join ()
129+ self .assertTrue (elapsed > wait_for )
130+
131+ with self .settings (DEBUG = False ):
132+ self .compile_bundles ('webpack.config.simple.js' )
133+ then = time .time ()
134+ request = self .factory .get ('/' )
135+ result = view (request )
136+ result .rendered_content
137+ elapsed = time .time () - then
138+ self .assertTrue (elapsed < wait_for )
0 commit comments