Skip to content

Commit 1f6fb2c

Browse files
committed
Some pyscript component tests
1 parent 5d2877b commit 1f6fb2c

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

src/reactpy/pyscript/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def extend_pyscript_config(
134134
if extra_js and isinstance(extra_js, str):
135135
pyscript_config["js_modules"]["main"].update(json.loads(extra_js))
136136
elif extra_js and isinstance(extra_js, dict):
137-
pyscript_config["js_modules"]["main"].update(extra_py)
137+
pyscript_config["js_modules"]["main"].update(extra_js)
138138

139139
# Update other config attributes
140140
if config and isinstance(config, str):

tests/test_asgi/test_pyscript.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ async def test_multi_file_components(multi_file_display: DisplayFixture):
8989

9090
await multi_file_display.page.click("#incr")
9191
await multi_file_display.page.wait_for_selector("#incr[data-count='3']")
92+
93+
94+
def test_bad_file_path():
95+
with pytest.raises(ValueError):
96+
ReactPyPyscript()

tests/test_pyscript/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from reactpy import component, hooks, html
2+
3+
4+
@component
5+
def custom():
6+
count, set_count = hooks.use_state(0)
7+
8+
def increment(event):
9+
set_count(count + 1)
10+
11+
return html.div(
12+
html.button(
13+
{"onClick": increment, "id": "incr", "data-count": count}, "Increment"
14+
),
15+
html.p(f"PyScript Count: {count}"),
16+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from reactpy import component, hooks, html
2+
3+
4+
@component
5+
def root():
6+
count, set_count = hooks.use_state(0)
7+
8+
def increment(event):
9+
set_count(count + 1)
10+
11+
return html.div(
12+
html.button(
13+
{"onClick": increment, "id": "incr", "data-count": count}, "Increment"
14+
),
15+
html.p(f"PyScript Count: {count}"),
16+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
import reactpy
6+
from reactpy import html, pyscript_component
7+
from reactpy.executors.asgi import ReactPy
8+
from reactpy.testing import BackendFixture, DisplayFixture
9+
from reactpy.testing.backend import root_hotswap_component
10+
11+
12+
@pytest.fixture()
13+
async def display(page):
14+
"""Override for the display fixture that uses ReactPyMiddleware."""
15+
app = ReactPy(root_hotswap_component, pyscript_setup=True)
16+
17+
async with BackendFixture(app) as server:
18+
async with DisplayFixture(backend=server, driver=page) as new_display:
19+
yield new_display
20+
21+
22+
async def test_pyscript_component(display: DisplayFixture):
23+
@reactpy.component
24+
def Counter():
25+
return pyscript_component(
26+
Path(__file__).parent / "pyscript_components" / "root.py",
27+
initial=html.div({"id": "loading"}, "Loading..."),
28+
)
29+
30+
await display.show(Counter)
31+
32+
await display.page.wait_for_selector("#loading")
33+
await display.page.wait_for_selector("#incr")
34+
35+
await display.page.click("#incr")
36+
await display.page.wait_for_selector("#incr[data-count='1']")
37+
38+
await display.page.click("#incr")
39+
await display.page.wait_for_selector("#incr[data-count='2']")
40+
41+
await display.page.click("#incr")
42+
await display.page.wait_for_selector("#incr[data-count='3']")
43+
44+
45+
async def test_custom_root_name(display: DisplayFixture):
46+
@reactpy.component
47+
def CustomRootName():
48+
return pyscript_component(
49+
Path(__file__).parent / "pyscript_components" / "custom_root_name.py",
50+
initial=html.div({"id": "loading"}, "Loading..."),
51+
root="custom",
52+
)
53+
54+
await display.show(CustomRootName)
55+
56+
await display.page.wait_for_selector("#loading")
57+
await display.page.wait_for_selector("#incr")
58+
59+
await display.page.click("#incr")
60+
await display.page.wait_for_selector("#incr[data-count='1']")
61+
62+
await display.page.click("#incr")
63+
await display.page.wait_for_selector("#incr[data-count='2']")
64+
65+
await display.page.click("#incr")
66+
await display.page.wait_for_selector("#incr[data-count='3']")
67+
68+
69+
def test_bad_file_path():
70+
with pytest.raises(ValueError):
71+
pyscript_component(initial=html.div({"id": "loading"}, "Loading...")).render()

tests/test_pyscript/test_utils.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pathlib import Path
2+
from uuid import uuid4
3+
4+
import orjson
5+
import pytest
6+
7+
from reactpy.pyscript import utils
8+
9+
10+
def test_bad_root_name():
11+
file_path = str(
12+
Path(__file__).parent / "pyscript_components" / "custom_root_name.py"
13+
)
14+
15+
with pytest.raises(ValueError):
16+
utils.pyscript_executor_html((file_path,), uuid4().hex, "bad")
17+
18+
19+
def test_extend_pyscript_config():
20+
extra_py = ["orjson", "tabulate"]
21+
extra_js = {"/static/foo.js": "bar"}
22+
config = {"packages_cache": "always"}
23+
24+
result = utils.extend_pyscript_config(extra_py, extra_js, config)
25+
result = orjson.loads(result)
26+
27+
# Check whether `packages` have been combined
28+
assert "orjson" in result["packages"]
29+
assert "tabulate" in result["packages"]
30+
assert any("reactpy" in package for package in result["packages"])
31+
32+
# Check whether `js_modules` have been combined
33+
assert "/static/foo.js" in result["js_modules"]["main"]
34+
assert any("morphdom" in module for module in result["js_modules"]["main"])
35+
36+
# Check whether `packages_cache` has been overridden
37+
assert result["packages_cache"] == "always"
38+
39+
40+
def test_extend_pyscript_config_string_values():
41+
extra_py = []
42+
extra_js = {"/static/foo.js": "bar"}
43+
config = {"packages_cache": "always"}
44+
45+
# Try using string based `extra_js` and `config`
46+
extra_js_string = orjson.dumps(extra_js).decode()
47+
config_string = orjson.dumps(config).decode()
48+
result = utils.extend_pyscript_config(extra_py, extra_js_string, config_string)
49+
result = orjson.loads(result)
50+
51+
# Make sure `packages` is unmangled
52+
assert any("reactpy" in package for package in result["packages"])
53+
54+
# Check whether `js_modules` have been combined
55+
assert "/static/foo.js" in result["js_modules"]["main"]
56+
assert any("morphdom" in module for module in result["js_modules"]["main"])
57+
58+
# Check whether `packages_cache` has been overridden
59+
assert result["packages_cache"] == "always"

0 commit comments

Comments
 (0)