Skip to content

Commit 23213d1

Browse files
committed
100% coverage
1 parent 1f6fb2c commit 23213d1

File tree

8 files changed

+67
-13
lines changed

8 files changed

+67
-13
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ omit = [
233233
"src/reactpy/__init__.py",
234234
"src/reactpy/_console/*",
235235
"src/reactpy/__main__.py",
236+
"src/reactpy/pyscript/layout_handler.py",
237+
"src/reactpy/pyscript/component_template.py",
236238
]
237239

238240
[tool.coverage.report]

src/reactpy/templatetags/jinja.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def render(self, *args: str, **kwargs: str) -> str:
2121
if self.tag_name == "pyscript_setup":
2222
return pyscript_setup(*args, **kwargs)
2323

24-
raise ValueError(f"Unknown tag: {self.tag_name}")
24+
# This should never happen, but we validate it for safety.
25+
raise ValueError(f"Unknown tag: {self.tag_name}") # pragma: no cover
2526

2627

2728
def component(dotted_path: str, **kwargs: str) -> str:

src/reactpy/testing/display.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from playwright.async_api import (
88
Browser,
99
BrowserContext,
10-
ElementHandle,
1110
Page,
1211
async_playwright,
1312
)
@@ -41,18 +40,10 @@ async def show(
4140
) -> None:
4241
self.backend.mount(component)
4342
await self.goto("/")
44-
await self.root_element() # check that root element is attached
4543

4644
async def goto(self, path: str, query: Any | None = None) -> None:
4745
await self.page.goto(self.backend.url(path, query))
4846

49-
async def root_element(self) -> ElementHandle:
50-
element = await self.page.wait_for_selector("#app", state="attached")
51-
if element is None: # nocov
52-
msg = "Root element not attached"
53-
raise RuntimeError(msg)
54-
return element
55-
5647
async def __aenter__(self) -> DisplayFixture:
5748
es = self._exit_stack = AsyncExitStack()
5849

tests/templates/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<head></head>
55

66
<body>
7-
<div id="app"></div>
87
{% component "reactpy.testing.backend.root_hotswap_component" %}
98
</body>
109

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head></head>
5+
6+
<body>
7+
{% component "this.doesnt.matter", bad_kwarg='foo-bar' %}
8+
</body>
9+
10+
</html>

tests/templates/pyscript.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
{% pyscript_setup %}
6+
</head>
7+
8+
<body>
9+
{% pyscript_component "tests/test_asgi/pyscript_components/root.py", initial='<div id="loading">Loading...</div>' %}
10+
</body>
11+
12+
</html>

tests/test_asgi/test_middleware.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def Stub():
7878
async with DisplayFixture(backend=server) as new_display:
7979
await new_display.show(Stub)
8080

81-
# Wait for the log record to be popualted
81+
# Wait for the log record to be populated
8282
for _ in range(10):
8383
if len(server.log_records) > 0:
8484
break
@@ -117,3 +117,26 @@ async def app(scope, receive, send): ...
117117
request, "GET", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current
118118
)
119119
assert response.status_code == 404
120+
121+
122+
async def test_templatetag_bad_kwargs(page, caplog):
123+
"""Override for the display fixture that uses ReactPyMiddleware."""
124+
templates = Jinja2Templates(
125+
env=JinjaEnvironment(
126+
loader=JinjaFileSystemLoader("tests/templates"),
127+
extensions=["reactpy.templatetags.Jinja"],
128+
)
129+
)
130+
131+
async def homepage(request):
132+
return templates.TemplateResponse(request, "jinja_bad_kwargs.html")
133+
134+
app = Starlette(routes=[Route("/", homepage)])
135+
136+
async with BackendFixture(app) as server:
137+
async with DisplayFixture(backend=server, driver=page) as new_display:
138+
await new_display.goto("/")
139+
140+
# This test could be improved by actually checking if `bad kwargs` error message is shown in
141+
# `stderr`, but I was struggling to get that to work.
142+
assert "internal server error" in (await new_display.page.content()).lower()

tests/test_asgi/test_pyscript.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def jinja_display(page):
5151
)
5252

5353
async def homepage(request):
54-
return templates.TemplateResponse(request, "index.html")
54+
return templates.TemplateResponse(request, "pyscript.html")
5555

5656
app = Starlette(routes=[Route("/", homepage)])
5757

@@ -94,3 +94,19 @@ async def test_multi_file_components(multi_file_display: DisplayFixture):
9494
def test_bad_file_path():
9595
with pytest.raises(ValueError):
9696
ReactPyPyscript()
97+
98+
99+
async def test_jinja_template_tag(jinja_display: DisplayFixture):
100+
await jinja_display.goto("/")
101+
102+
await jinja_display.page.wait_for_selector("#loading")
103+
await jinja_display.page.wait_for_selector("#incr")
104+
105+
await jinja_display.page.click("#incr")
106+
await jinja_display.page.wait_for_selector("#incr[data-count='1']")
107+
108+
await jinja_display.page.click("#incr")
109+
await jinja_display.page.wait_for_selector("#incr[data-count='2']")
110+
111+
await jinja_display.page.click("#incr")
112+
await jinja_display.page.wait_for_selector("#incr[data-count='3']")

0 commit comments

Comments
 (0)