Skip to content

Commit e09570b

Browse files
committed
More robust tests for router
1 parent 8b70e78 commit e09570b

File tree

2 files changed

+57
-43
lines changed

2 files changed

+57
-43
lines changed

tests/test_app/router/components.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
11
from reactpy import component, html, use_location
22
from reactpy_router import route, use_params, use_search_params
3+
from reactpy_router.types import Route
34

45
from reactpy_django.router import django_router
56

67

78
@component
89
def display_params(string: str):
910
location = use_location()
10-
query = use_search_params()
11-
params = use_params()
11+
search_params = use_search_params()
12+
url_params = use_params()
1213

1314
return html._(
1415
html.div({"id": "router-string"}, string),
15-
html.div(f"Params: {params}"),
1616
html.div(
1717
{"id": "router-path", "data-path": location.pathname},
18-
f"Path Name: {location.pathname}",
18+
f"path: {location.pathname}",
1919
),
20-
html.div(f"Query String: {location.search}"),
21-
html.div(f"Query: {query}"),
20+
html.div(f"url_params: {url_params}"),
21+
html.div(f"location.search: {location.search}"),
22+
html.div(f"search_params: {search_params}"),
2223
)
2324

2425

26+
def show_route(path: str, *children: Route) -> Route:
27+
return route(path, display_params(path), *children)
28+
29+
2530
@component
2631
def main():
2732
return django_router(
28-
route("/router/", display_params("Path 1")),
29-
route("/router/any/<value>/", display_params("Path 2")),
30-
route("/router/integer/<int:value>/", display_params("Path 3")),
31-
route("/router/path/<path:value>/", display_params("Path 4")),
32-
route("/router/slug/<slug:value>/", display_params("Path 5")),
33-
route("/router/string/<str:value>/", display_params("Path 6")),
34-
route("/router/uuid/<uuid:value>/", display_params("Path 7")),
35-
route("/router/", None, route("abc/", display_params("Path 8"))),
36-
route(
37-
"/router/two/<int:value>/<str:value2>/",
38-
display_params("Path 9"),
39-
),
40-
route(
41-
"/router/<any:name1>/",
42-
None,
43-
route("one/", display_params("Path 11")),
44-
route("<any:name2>", display_params("Path 12")),
45-
),
33+
show_route("/router/", show_route("subroute/")),
34+
show_route("/router/unspecified/<value>/"),
35+
show_route("/router/integer/<int:value>/"),
36+
show_route("/router/path/<path:value>/"),
37+
show_route("/router/slug/<slug:value>/"),
38+
show_route("/router/string/<str:value>/"),
39+
show_route("/router/uuid/<uuid:value>/"),
40+
show_route("/router/any/<any:name>"),
41+
show_route("/router/two/<int:value>/<str:value2>/"),
4642
)

tests/test_app/tests/test_components.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ComponentTests(ChannelsLiveServerTestCase):
2929

3030
@classmethod
3131
def setUpClass(cls):
32+
super().setUpClass()
33+
3234
# Repurposed from ChannelsLiveServerTestCase._pre_setup
3335
for connection in connections.all():
3436
if cls._is_in_memory_db(cls, connection):
@@ -77,14 +79,14 @@ def tearDownClass(cls):
7779
cls.playwright.stop()
7880

7981
# Close the other server processes
82+
cls._server_process.terminate()
83+
cls._server_process.join()
8084
cls._server_process2.terminate()
8185
cls._server_process2.join()
8286
cls._server_process3.terminate()
8387
cls._server_process3.join()
8488

8589
# Repurposed from ChannelsLiveServerTestCase._post_teardown
86-
cls._server_process.terminate()
87-
cls._server_process.join()
8890
cls._live_server_modified_settings.disable()
8991
for db_name in {"default", config.REACTPY_DATABASE}:
9092
call_command(
@@ -95,15 +97,15 @@ def tearDownClass(cls):
9597
reset_sequences=False,
9698
)
9799

100+
super().tearDownClass()
101+
98102
def _pre_setup(self):
99103
"""Handled manually in `setUpClass` to speed things up."""
100-
pass
101104

102105
def _post_teardown(self):
103106
"""Handled manually in `tearDownClass` to prevent TransactionTestCase from doing
104107
database flushing. This is needed to prevent a `SynchronousOnlyOperation` from
105108
occuring due to a bug within `ChannelsLiveServerTestCase`."""
106-
pass
107109

108110
def setUp(self):
109111
if self.page.url == "about:blank":
@@ -544,26 +546,44 @@ def test_url_router(self):
544546
new_page.goto(f"{self.live_server_url}/router/")
545547
path = new_page.wait_for_selector("#router-path")
546548
self.assertIn("/router/", path.get_attribute("data-path"))
549+
string = new_page.query_selector("#router-string")
550+
self.assertEqual("/router/", string.text_content())
551+
552+
new_page.goto(f"{self.live_server_url}/router/subroute/")
553+
path = new_page.wait_for_selector("#router-path")
554+
self.assertIn("/router/subroute/", path.get_attribute("data-path"))
555+
string = new_page.query_selector("#router-string")
556+
self.assertEqual("subroute/", string.text_content())
547557

548-
new_page.goto(f"{self.live_server_url}/router/any/123/")
558+
new_page.goto(f"{self.live_server_url}/router/unspecified/123/")
549559
path = new_page.wait_for_selector("#router-path")
550-
self.assertIn("/router/any/123/", path.get_attribute("data-path"))
560+
self.assertIn("/router/unspecified/123/", path.get_attribute("data-path"))
561+
string = new_page.query_selector("#router-string")
562+
self.assertEqual("/router/unspecified/<value>/", string.text_content())
551563

552564
new_page.goto(f"{self.live_server_url}/router/integer/123/")
553565
path = new_page.wait_for_selector("#router-path")
554566
self.assertIn("/router/integer/123/", path.get_attribute("data-path"))
567+
string = new_page.query_selector("#router-string")
568+
self.assertEqual("/router/integer/<int:value>/", string.text_content())
555569

556570
new_page.goto(f"{self.live_server_url}/router/path/abc/123/")
557571
path = new_page.wait_for_selector("#router-path")
558572
self.assertIn("/router/path/abc/123/", path.get_attribute("data-path"))
573+
string = new_page.query_selector("#router-string")
574+
self.assertEqual("/router/path/<path:value>/", string.text_content())
559575

560576
new_page.goto(f"{self.live_server_url}/router/slug/abc-123/")
561577
path = new_page.wait_for_selector("#router-path")
562578
self.assertIn("/router/slug/abc-123/", path.get_attribute("data-path"))
579+
string = new_page.query_selector("#router-string")
580+
self.assertEqual("/router/slug/<slug:value>/", string.text_content())
563581

564582
new_page.goto(f"{self.live_server_url}/router/string/abc/")
565583
path = new_page.wait_for_selector("#router-path")
566584
self.assertIn("/router/string/abc/", path.get_attribute("data-path"))
585+
string = new_page.query_selector("#router-string")
586+
self.assertEqual("/router/string/<str:value>/", string.text_content())
567587

568588
new_page.goto(
569589
f"{self.live_server_url}/router/uuid/123e4567-e89b-12d3-a456-426614174000/"
@@ -573,18 +593,8 @@ def test_url_router(self):
573593
"/router/uuid/123e4567-e89b-12d3-a456-426614174000/",
574594
path.get_attribute("data-path"),
575595
)
576-
577-
new_page.goto(f"{self.live_server_url}/router/abc/")
578-
path = new_page.wait_for_selector("#router-path")
579-
self.assertIn("/router/abc/", path.get_attribute("data-path"))
580-
581-
new_page.goto(f"{self.live_server_url}/router/two/123/abc/")
582-
path = new_page.wait_for_selector("#router-path")
583-
self.assertIn("/router/two/123/abc/", path.get_attribute("data-path"))
584-
585-
new_page.goto(f"{self.live_server_url}/router/any/one/")
586-
path = new_page.wait_for_selector("#router-path")
587-
self.assertIn("/router/any/one/", path.get_attribute("data-path"))
596+
string = new_page.query_selector("#router-string")
597+
self.assertEqual("/router/uuid/<uuid:value>/", string.text_content())
588598

589599
new_page.goto(
590600
f"{self.live_server_url}/router/any/adslkjgklasdjhfah/6789543256/"
@@ -595,7 +605,15 @@ def test_url_router(self):
595605
path.get_attribute("data-path"),
596606
)
597607
string = new_page.query_selector("#router-string")
598-
self.assertEqual("Path 12", string.text_content())
608+
self.assertEqual("/router/any/<any:name>", string.text_content())
609+
610+
new_page.goto(f"{self.live_server_url}/router/two/123/abc/")
611+
path = new_page.wait_for_selector("#router-path")
612+
self.assertIn("/router/two/123/abc/", path.get_attribute("data-path"))
613+
string = new_page.query_selector("#router-string")
614+
self.assertEqual(
615+
"/router/two/<int:value>/<str:value2>/", string.text_content()
616+
)
599617

600618
finally:
601619
new_page.close()

0 commit comments

Comments
 (0)