forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_modules.py
More file actions
118 lines (95 loc) · 3.62 KB
/
test_modules.py
File metadata and controls
118 lines (95 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from pathlib import Path
import pytest
import reactpy
from reactpy import html
from reactpy.reactjs import component_from_string, import_reactjs
from reactpy.testing import BackendFixture, DisplayFixture
from .. import pytestmark # noqa: F401
JS_FIXTURES_DIR = Path(__file__).parent / "js_fixtures"
@pytest.fixture(scope="module")
async def display(browser):
"""Override for the display fixture that includes ReactJS."""
async with BackendFixture(html_head=html.head(import_reactjs())) as backend:
async with DisplayFixture(backend=backend, browser=browser) as new_display:
yield new_display
async def test_nested_client_side_components(display: DisplayFixture):
# Module A
ComponentA = component_from_string(
"""
import React from "react";
export function ComponentA({ children }) {
return React.createElement("div", { id: "component-a" }, children);
}
""",
"ComponentA",
name="module-a",
)
# Module B
ComponentB = component_from_string(
"""
import React from "react";
export function ComponentB({ children }) {
return React.createElement("div", { id: "component-b" }, children);
}
""",
"ComponentB",
name="module-b",
)
@reactpy.component
def App():
return ComponentA(
ComponentB(reactpy.html.div({"id": "server-side"}, "Server Side Content"))
)
await display.show(App)
# Check that all components are rendered
await display.page.wait_for_selector("#component-a")
await display.page.wait_for_selector("#component-b")
await display.page.wait_for_selector("#server-side")
async def test_interleaved_client_server_components(display: DisplayFixture):
# Module C
ComponentC = component_from_string(
"""
import React from "react";
export function ComponentC({ children }) {
return React.createElement("div", { id: "component-c", className: "component-c" }, children);
}
""",
"ComponentC",
name="module-c",
)
@reactpy.component
def App():
return reactpy.html.div(
{"id": "root-server"},
ComponentC(
reactpy.html.div(
{"id": "nested-server"},
ComponentC(
reactpy.html.span({"id": "deep-server"}, "Deep Content")
),
)
),
)
await display.show(App)
await display.page.wait_for_selector("#root-server")
await display.page.wait_for_selector(".component-c")
await display.page.wait_for_selector("#nested-server")
# We need to check that there are two component-c elements
elements = await display.page.query_selector_all(".component-c")
assert len(elements) == 2
await display.page.wait_for_selector("#deep-server")
async def test_nest_custom_component_under_web_component(display: DisplayFixture):
"""
Fix https://github.com/reactive-python/reactpy/discussions/1323
Custom components (i.e those wrapped in the component decorator) were not able to
be nested under web components.
"""
Container = reactpy.reactjs.component_from_file(
JS_FIXTURES_DIR / "nest-custom-under-web.js", "Container"
)
@reactpy.component
def CustomComponent():
return reactpy.html.div(reactpy.html.h1({"id": "my-header"}, "Header 1"))
await display.show(lambda: Container(CustomComponent()))
element = await display.page.wait_for_selector("#my-header", state="attached")
assert await element.inner_text() == "Header 1"