|
9 | 9 | from django.conf import settings |
10 | 10 | from django.test import RequestFactory, TestCase |
11 | 11 | from django.views.generic.base import TemplateView |
| 12 | +from django.template import Context, Template |
12 | 13 | from django_jinja.builtins import DEFAULT_EXTENSIONS |
13 | 14 | from unittest2 import skipIf |
14 | 15 | from webpack_loader.exceptions import ( |
@@ -180,7 +181,6 @@ def test_jinja2(self): |
180 | 181 | "extensions": DEFAULT_EXTENSIONS + [ |
181 | 182 | "webpack_loader.contrib.jinja2ext.WebpackExtension", |
182 | 183 | ] |
183 | | - |
184 | 184 | } |
185 | 185 | }, |
186 | 186 | ] |
@@ -283,3 +283,46 @@ def test_request_blocking(self): |
283 | 283 | result.rendered_content |
284 | 284 | elapsed = time.time() - then |
285 | 285 | self.assertTrue(elapsed < wait_for) |
| 286 | + |
| 287 | + def test_takes_context(self): |
| 288 | + self.compile_bundles('webpack.config.simple.js') |
| 289 | + self.compile_bundles('webpack.config.app2.js') |
| 290 | + template_to_test = Template( |
| 291 | + "{% load render_bundle from webpack_loader %}" |
| 292 | + "{% render_bundle 'main' 'css' %}" |
| 293 | + "{% render_bundle 'main' 'js' %}" |
| 294 | + ) |
| 295 | + context = Context({}) |
| 296 | + rendered_template = template_to_test.render(context) |
| 297 | + self.assertIn("webpack_loader_used_tags", context) |
| 298 | + used_tags = context["webpack_loader_used_tags"] |
| 299 | + self.assertIn('<link href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', used_tags) |
| 300 | + self.assertIn('<script src="/static/django_webpack_loader_bundles/main.js" ></script>', used_tags) |
| 301 | + |
| 302 | + |
| 303 | + |
| 304 | + def test_skip_common_chunks(self): |
| 305 | + self.compile_bundles('webpack.config.skipCommon.js') |
| 306 | + # Test default case where duplicates will be generated |
| 307 | + template_to_test_duplicates = Template( |
| 308 | + "{% load render_bundle from webpack_loader %}" |
| 309 | + "{% render_bundle 'app1' %}" |
| 310 | + "{% render_bundle 'app2' %}" |
| 311 | + ) |
| 312 | + context = Context({}) |
| 313 | + rendered_template = template_to_test_duplicates.render(context) |
| 314 | + self.assertIn("webpack_loader_used_tags", context) |
| 315 | + used_tags = context["webpack_loader_used_tags"] |
| 316 | + self.assertEqual(rendered_template.count('<script src="/static/django_webpack_loader_bundles/vendors.js" ></script>'), 2) |
| 317 | + |
| 318 | + template_to_test_skipped_chunks = Template( |
| 319 | + "{% load render_bundle from webpack_loader %}" |
| 320 | + "{% render_bundle 'app1' %}" |
| 321 | + "{% render_bundle 'app2' skip_common_chunks=True %}" |
| 322 | + ) |
| 323 | + context = Context({}) |
| 324 | + rendered_template = template_to_test_skipped_chunks.render(context) |
| 325 | + self.assertEqual(rendered_template.count('<script src="/static/django_webpack_loader_bundles/vendors.js" ></script>'), 1) |
| 326 | + |
| 327 | + |
| 328 | + |
0 commit comments