|
| 1 | +from imp import reload |
| 2 | +from django.test import TestCase |
| 3 | +from webpack_loader import utils, config, loader |
| 4 | + |
| 5 | + |
| 6 | +DEFAULT_CONFIG = 'DEFAULT' |
| 7 | +LOADER_PAYLOAD = {'status': 'done', 'chunks': []} |
| 8 | + |
| 9 | + |
| 10 | +class ValidCustomLoader(loader.WebpackLoader): |
| 11 | + |
| 12 | + def load_assets(self): |
| 13 | + return LOADER_PAYLOAD |
| 14 | + |
| 15 | + |
| 16 | +class CustomLoadersTestCase(TestCase): |
| 17 | + def tearDown(self): |
| 18 | + self.reload_webpack() |
| 19 | + |
| 20 | + def reload_webpack(self): |
| 21 | + ''' |
| 22 | + Reloads webpack loader modules that have cached values to avoid polluting certain tests |
| 23 | + ''' |
| 24 | + reload(utils) |
| 25 | + reload(config) |
| 26 | + |
| 27 | + def test_bad_custom_loader(self): |
| 28 | + ''' |
| 29 | + Tests that a bad custom loader path will raise an error |
| 30 | + ''' |
| 31 | + loader_class = 'app.tests.bad_loader_path.BadCustomLoader' |
| 32 | + with self.settings(WEBPACK_LOADER={ |
| 33 | + 'DEFAULT': { |
| 34 | + 'CACHE': False, |
| 35 | + 'BUNDLE_DIR_NAME': 'bundles/', |
| 36 | + 'LOADER_CLASS': loader_class |
| 37 | + } |
| 38 | + }): |
| 39 | + self.reload_webpack() |
| 40 | + try: |
| 41 | + loader = utils.get_loader(DEFAULT_CONFIG) |
| 42 | + self.fail('The loader should fail to load with a bad LOADER_CLASS') |
| 43 | + except ImportError as e: |
| 44 | + self.assertIn( |
| 45 | + '{} doesn\'t look like a valid module path'.format(loader_class), |
| 46 | + str(e) |
| 47 | + ) |
| 48 | + |
| 49 | + def test_good_custom_loader(self): |
| 50 | + ''' |
| 51 | + Tests that a good custom loader will return the correct assets |
| 52 | + ''' |
| 53 | + loader_class = 'app.tests.test_custom_loaders.ValidCustomLoader' |
| 54 | + with self.settings(WEBPACK_LOADER={ |
| 55 | + 'DEFAULT': { |
| 56 | + 'CACHE': False, |
| 57 | + 'BUNDLE_DIR_NAME': 'bundles/', |
| 58 | + 'LOADER_CLASS': loader_class, |
| 59 | + } |
| 60 | + }): |
| 61 | + self.reload_webpack() |
| 62 | + assets = utils.get_loader(DEFAULT_CONFIG).load_assets() |
| 63 | + self.assertEqual(assets, LOADER_PAYLOAD) |
0 commit comments