|
| 1 | +import os |
| 2 | +import time |
| 3 | +import json |
| 4 | +from subprocess import call |
| 5 | +from threading import Thread |
| 6 | + |
| 7 | +from django.conf import settings |
| 8 | +from django.test import TestCase, RequestFactory |
| 9 | +from webpack_loader.utils import get_assets |
| 10 | +from django.views.generic.base import TemplateView |
| 11 | + |
| 12 | + |
| 13 | +view = TemplateView.as_view(template_name='home.html') |
| 14 | + |
| 15 | + |
| 16 | +BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/') |
| 17 | + |
| 18 | + |
| 19 | +class LoaderTestCase(TestCase): |
| 20 | + def setUp(self): |
| 21 | + self.factory = RequestFactory() |
| 22 | + |
| 23 | + def clean_dir(self, directory): |
| 24 | + if os.path.exists(BUNDLE_PATH): |
| 25 | + [os.remove(os.path.join(BUNDLE_PATH, F)) for F in os.listdir(BUNDLE_PATH)] |
| 26 | + |
| 27 | + def compile_bundles(self, config, wait=None): |
| 28 | + if wait: |
| 29 | + time.sleep(wait) |
| 30 | + call(['./node_modules/.bin/webpack', '--config', os.path.join('assets/', config)]) |
| 31 | + |
| 32 | + def test_request_blocking(self): |
| 33 | + # FIXME: This will work 99% time but there is no garauntee with the |
| 34 | + # 4 second thing. Need a better way to detect if request was blocked on |
| 35 | + # not. |
| 36 | + wait_for = 3 |
| 37 | + |
| 38 | + with self.settings(DEBUG=True): |
| 39 | + open(settings.WEBPACK_LOADER['STATS_FILE'], 'w').write(json.dumps({'status': 'compiling'})) |
| 40 | + then = time.time() |
| 41 | + request = self.factory.get('/') |
| 42 | + result = view(request) |
| 43 | + t = Thread(target=self.compile_bundles, args=('webpack.config.simple.js', wait_for)) |
| 44 | + t.start() |
| 45 | + result.rendered_content |
| 46 | + elapsed = time.time() - then |
| 47 | + t.join() |
| 48 | + self.assertTrue(elapsed > wait_for) |
| 49 | + |
| 50 | + with self.settings(DEBUG=False): |
| 51 | + self.compile_bundles('webpack.config.simple.js') |
| 52 | + then = time.time() |
| 53 | + request = self.factory.get('/') |
| 54 | + result = view(request) |
| 55 | + result.rendered_content |
| 56 | + elapsed = time.time() - then |
| 57 | + self.assertTrue(elapsed < wait_for) |
| 58 | + |
| 59 | + def test_reporting_errors(self): |
| 60 | + #TODO: |
| 61 | + pass |
| 62 | + |
| 63 | + def test_simple(self): |
| 64 | + self.compile_bundles('webpack.config.simple.js') |
| 65 | + assets = get_assets() |
| 66 | + self.assertEqual(assets['status'], 'done') |
| 67 | + self.assertIn('chunks', assets) |
| 68 | + |
| 69 | + chunks = assets['chunks'] |
| 70 | + self.assertIn('main', chunks) |
| 71 | + self.assertEquals(len(chunks), 1) |
| 72 | + |
| 73 | + main = chunks['main'] |
| 74 | + self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js')) |
| 75 | + |
| 76 | + def test_multiple_files(self): |
| 77 | + self.compile_bundles('webpack.config.split.js') |
| 78 | + assets = get_assets() |
| 79 | + self.assertEqual(assets['status'], 'done') |
| 80 | + self.assertIn('chunks', assets) |
| 81 | + |
| 82 | + chunks = assets['chunks'] |
| 83 | + self.assertIn('main', chunks) |
| 84 | + self.assertEquals(len(chunks), 2) |
| 85 | + |
| 86 | + main = chunks['main'] |
| 87 | + self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js')) |
| 88 | + |
| 89 | + vendor = chunks['vendor'] |
| 90 | + self.assertEqual(vendor[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/vendor.js')) |
0 commit comments