Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Sep 15, 2023
1 parent b394e0a commit 36cd467
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 10 deletions.
6 changes: 2 additions & 4 deletions tests/test_app/preload/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def preload_string():
scope = reactpy_django.hooks.use_scope()

sleep(1)
sleep(0.5)
return (
"preload_string: Fully Rendered"
if scope.get("type") == "websocket"
Expand All @@ -23,7 +23,6 @@ def preload_vdom():
if scope.get("type") == "http":
return html.div("preload_vdom: Preloaded")

sleep(1)
return html.div("preload_vdom: Fully Rendered")


Expand All @@ -38,5 +37,4 @@ def inner(value):
if scope.get("type") == "http":
return inner("preload_component: Preloaded")

sleep(1)
return inner("preload_component: Fully Rendered ")
return inner("preload_component: Fully Rendered")
7 changes: 7 additions & 0 deletions tests/test_app/preload/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from .views import preload

urlpatterns = [
path("preload/", preload),
]
5 changes: 5 additions & 0 deletions tests/test_app/preload/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.shortcuts import render


def preload(request):
return render(request, "preload.html", {})
1 change: 1 addition & 0 deletions tests/test_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]


# Logging
LOG_LEVEL = "WARNING"
if DEBUG and ("test" not in sys.argv):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/templates/preload.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>ReactPy Preload Test Page</h1>
{% component "test_app.preload.components.preload_vdom" class="preload-vdom" preload="true" %}
</div>
<hr>
<div clas="preload_component">
<div id="preload_component">
{% component "test_app.preload.components.preload_component" class="preload-component" preload="true" %}
</div>
<hr>
Expand Down
32 changes: 32 additions & 0 deletions tests/test_app/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from distutils.util import strtobool
from functools import partial
from time import sleep

from channels.testing import ChannelsLiveServerTestCase
from channels.testing.live import make_application
Expand Down Expand Up @@ -375,3 +376,34 @@ def test_broken_postprocessor_query(self):
broken_component = self.page.locator("#broken_postprocessor_query pre")
broken_component.wait_for()
self.assertIn("SynchronousOnlyOperation:", broken_component.text_content())

def test_preload(self):
"""Verify if round-robin host selection is working."""
new_page = self.browser.new_page()
try:
new_page.goto(f"{self.live_server_url}/preload/")
string = new_page.locator("#preload_string")
vdom = new_page.locator("#preload_vdom")
component = new_page.locator("#preload_component")

string.wait_for()
vdom.wait_for()
component.wait_for()

# Check if the preload occurred
self.assertEqual(string.all_inner_texts(), ["preload_string: Preloaded"])
self.assertEqual(vdom.all_inner_texts(), ["preload_vdom: Preloaded"])
self.assertEqual(
component.all_inner_texts(), ["preload_component: Preloaded"]
)

sleep(1)
self.assertEqual(
string.all_inner_texts(), ["preload_string: Fully Rendered"]
)
self.assertEqual(vdom.all_inner_texts(), ["preload_vdom: Fully Rendered"])
self.assertEqual(
component.all_inner_texts(), ["preload_component: Fully Rendered"]
)
finally:
new_page.close()
2 changes: 1 addition & 1 deletion tests/test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AccessUser:
"roundrobin/<int:port1>/<int:port2>/<int:count>/",
views.host_port_roundrobin_template,
),
path("preload/", views.preload),
path("", include("test_app.preload.urls")),
path("", include("test_app.performance.urls")),
path("reactpy/", include("reactpy_django.http.urls")),
path("admin/", admin.site.urls),
Expand Down
4 changes: 0 additions & 4 deletions tests/test_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,3 @@ def view_to_component_kwargs(request, success="false"):
"status": success,
},
)


def preload(request):
return render(request, "preload.html", {})

0 comments on commit 36cd467

Please sign in to comment.