Skip to content

Commit 3ab8e39

Browse files
committed
Added test cases
1 parent 9d9444e commit 3ab8e39

File tree

23 files changed

+41294
-0
lines changed

23 files changed

+41294
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ examples/**/ve/
6363
examples/**/node_modules/
6464
examples/**/assets/bundles/
6565
examples/**/assets/webpack-stats.json
66+
67+
tests/ve/
68+
tests/node_modules/

examples/code-splitting/assets/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
output: {
1111
path: path.resolve('./assets/bundles/'),
1212
filename: "[name]-[hash].js",
13+
chunkFilename: "[name]-[hash].js"
1314
},
1415

1516
plugins: [

tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

tests/app/__init__.py

Whitespace-only changes.

tests/app/settings.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""
2+
Django settings for app project.
3+
4+
Generated by 'django-admin startproject' using Django 1.8.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.8/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.8/ref/settings/
11+
"""
12+
13+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14+
import os
15+
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '+g25&y9i+-6_z$$z!ov$l2s%b#0kcmnx)n7y*2_ehy-w011p#k'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = (
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'app',
41+
'webpack_loader',
42+
)
43+
44+
MIDDLEWARE_CLASSES = (
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
50+
'django.contrib.messages.middleware.MessageMiddleware',
51+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
52+
'django.middleware.security.SecurityMiddleware',
53+
)
54+
55+
ROOT_URLCONF = 'app.urls'
56+
57+
TEMPLATES = [
58+
{
59+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
60+
'DIRS': [],
61+
'APP_DIRS': True,
62+
'OPTIONS': {
63+
'context_processors': [
64+
'django.template.context_processors.debug',
65+
'django.template.context_processors.request',
66+
'django.contrib.auth.context_processors.auth',
67+
'django.contrib.messages.context_processors.messages',
68+
],
69+
},
70+
},
71+
]
72+
73+
WSGI_APPLICATION = 'app.wsgi.application'
74+
75+
76+
# Database
77+
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
78+
79+
DATABASES = {
80+
'default': {
81+
'ENGINE': 'django.db.backends.sqlite3',
82+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
83+
}
84+
}
85+
86+
87+
# Internationalization
88+
# https://docs.djangoproject.com/en/1.8/topics/i18n/
89+
90+
LANGUAGE_CODE = 'en-us'
91+
92+
TIME_ZONE = 'UTC'
93+
94+
USE_I18N = True
95+
96+
USE_L10N = True
97+
98+
USE_TZ = True
99+
100+
101+
# Static files (CSS, JavaScript, Images)
102+
# https://docs.djangoproject.com/en/1.8/howto/static-files/
103+
104+
STATIC_URL = '/static/'
105+
106+
STATICFILES_DIRS = (
107+
os.path.join(BASE_DIR, 'assets'),
108+
)
109+
110+
WEBPACK_LOADER = {
111+
'BUNDLE_DIR_NAME': 'bundles/',
112+
'STATS_FILE': os.path.join(BASE_DIR, 'assets/webpack-stats.json'),
113+
}

tests/app/templates/home.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% load render_bundle from webpack_loader %}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Example</title>
7+
</head>
8+
9+
<body>
10+
<div id="react-app"></div>
11+
{% render_bundle 'main' %}
12+
</body>
13+
</html>

tests/app/tests/__init__.py

Whitespace-only changes.

tests/app/tests/test_webpack.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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'))

tests/app/urls.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""app URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/1.8/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Add an import: from blog import urls as blog_urls
14+
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
15+
"""
16+
from django.conf.urls import include, url
17+
from django.contrib import admin
18+
from django.views.generic import TemplateView
19+
20+
21+
22+
urlpatterns = [
23+
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
24+
url(r'^admin/', include(admin.site.urls)),
25+
]

tests/app/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from django.views.generic.base import TemplateView

0 commit comments

Comments
 (0)