Skip to content

Commit

Permalink
added tests for the new templatetag render_entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alihazemfarouk committed Nov 21, 2018
1 parent bad972b commit 83df98b
Show file tree
Hide file tree
Showing 10 changed files with 1,392 additions and 1,206 deletions.
14 changes: 14 additions & 0 deletions tests/app/templates/another_entrypoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% load render_entrypoint from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
{% render_entrypoint 'another_entrypoint' 'css' %}
</head>

<body>
<div id="react-app"></div>
{% render_entrypoint 'another_entrypoint' 'js' %}
</body>
</html>
14 changes: 14 additions & 0 deletions tests/app/templates/main_entrypoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% load render_entrypoint from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
{% render_entrypoint 'main' 'css' %}
</head>

<body>
<div id="react-app"></div>
{% render_entrypoint 'main' 'js' %}
</body>
</html>
13 changes: 13 additions & 0 deletions tests/app/templates/main_entrypoint.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
{{ render_entrypoint('main', 'css') }}
</head>

<body>
<div id="react-app"></div>
{{ render_entrypoint('main', 'js', attrs='async charset="UTF-8"') }}
</body>
</html>
39 changes: 34 additions & 5 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ def test_config_check(self):
from webpack_loader.errors import BAD_CONFIG_ERROR

with self.settings(WEBPACK_LOADER={
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': 'webpack-stats.json',
}):
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': 'webpack-stats.json',
}):
errors = webpack_cfg_check(None)
expected_errors = [BAD_CONFIG_ERROR]
self.assertEqual(errors, expected_errors)

with self.settings(WEBPACK_LOADER={
'DEFAULT': {}
}):
'DEFAULT': {}
}):
errors = webpack_cfg_check(None)
expected_errors = []
self.assertEqual(errors, expected_errors)
Expand Down Expand Up @@ -111,6 +111,19 @@ def test_templatetags(self):
result = view(request)
self.assertIn('<img src="http://custom-static-host.com/my-image.png"/>', result.rendered_content)

self.compile_bundles('webpack.config.multipleEntrypoints.js')
view = TemplateView.as_view(template_name='main_entrypoint.html')
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" ></script>', result.rendered_content)

view = TemplateView.as_view(template_name='another_entrypoint.html')
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/another_entrypoint.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/another_entrypoint.js" ></script>', result.rendered_content)

def test_jinja2(self):
self.compile_bundles('webpack.config.simple.js')
self.compile_bundles('webpack.config.app2.js')
Expand Down Expand Up @@ -145,6 +158,14 @@ def test_jinja2(self):
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)

self.compile_bundles('webpack.config.multipleEntrypoints.js')
view = TemplateView.as_view(template_name='main_entrypoint.jinja')
with self.settings(**settings):
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)

def test_reporting_errors(self):
self.compile_bundles('webpack.config.error.js')
try:
Expand All @@ -160,6 +181,14 @@ def test_missing_bundle(self):
except WebpackBundleLookupError as e:
self.assertIn('Cannot resolve bundle {0}'.format(missing_bundle_name), str(e))

def test_missing_entrypoint(self):
missing_bundle_name = 'missing_entrypoint'
self.compile_bundles('webpack.config.multipleEntrypoints.js')
try:
get_loader(DEFAULT_CONFIG).get_entry(missing_bundle_name)
except WebpackBundleLookupError as e:
self.assertIn('Cannot resolve entry {0}'.format(missing_bundle_name), str(e))

def test_missing_stats_file(self):
stats_file = settings.WEBPACK_LOADER[DEFAULT_CONFIG]['STATS_FILE']
if os.path.exists(stats_file):
Expand Down
5 changes: 5 additions & 0 deletions tests/assets/js/index2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var React = require('react');
var App = require('./app');
var style = require('./style.css');

React.render(<App/>, document.getElementById('react-app'));
Loading

0 comments on commit 83df98b

Please sign in to comment.