diff --git a/tests/test_app/preload/components.py b/tests/test_app/preload/components.py index 7aee80da..abca35cf 100644 --- a/tests/test_app/preload/components.py +++ b/tests/test_app/preload/components.py @@ -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" @@ -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") @@ -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") diff --git a/tests/test_app/preload/urls.py b/tests/test_app/preload/urls.py new file mode 100644 index 00000000..50fcf956 --- /dev/null +++ b/tests/test_app/preload/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from .views import preload + +urlpatterns = [ + path("preload/", preload), +] diff --git a/tests/test_app/preload/views.py b/tests/test_app/preload/views.py new file mode 100644 index 00000000..16f4f32e --- /dev/null +++ b/tests/test_app/preload/views.py @@ -0,0 +1,5 @@ +from django.shortcuts import render + + +def preload(request): + return render(request, "preload.html", {}) diff --git a/tests/test_app/settings.py b/tests/test_app/settings.py index 2979b050..6d4574c3 100644 --- a/tests/test_app/settings.py +++ b/tests/test_app/settings.py @@ -159,6 +159,7 @@ "django.contrib.staticfiles.finders.AppDirectoriesFinder", ] + # Logging LOG_LEVEL = "WARNING" if DEBUG and ("test" not in sys.argv): diff --git a/tests/test_app/templates/preload.html b/tests/test_app/templates/preload.html index 4a172974..c23d85b7 100644 --- a/tests/test_app/templates/preload.html +++ b/tests/test_app/templates/preload.html @@ -21,7 +21,7 @@

ReactPy Preload Test Page

{% component "test_app.preload.components.preload_vdom" class="preload-vdom" preload="true" %}
-
+
{% component "test_app.preload.components.preload_component" class="preload-component" preload="true" %}

diff --git a/tests/test_app/tests/test_components.py b/tests/test_app/tests/test_components.py index d05ab46c..a7a9be98 100644 --- a/tests/test_app/tests/test_components.py +++ b/tests/test_app/tests/test_components.py @@ -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 @@ -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() diff --git a/tests/test_app/urls.py b/tests/test_app/urls.py index c40105ca..33f4eb38 100644 --- a/tests/test_app/urls.py +++ b/tests/test_app/urls.py @@ -36,7 +36,7 @@ class AccessUser: "roundrobin////", 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), diff --git a/tests/test_app/views.py b/tests/test_app/views.py index d240d8ba..689d8f8c 100644 --- a/tests/test_app/views.py +++ b/tests/test_app/views.py @@ -174,7 +174,3 @@ def view_to_component_kwargs(request, success="false"): "status": success, }, ) - - -def preload(request): - return render(request, "preload.html", {})