Skip to content

Commit 18fd2e9

Browse files
committed
test render imported child components
1 parent aef557d commit 18fd2e9

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { h, render } from "https://unpkg.com/preact?module";
2+
import htm from "https://unpkg.com/htm?module";
3+
4+
const html = htm.bind(h);
5+
6+
export function bind(node, config) {
7+
return {
8+
create: (type, props, children) => h(type, props, ...children),
9+
render: (element) => render(element, node),
10+
unmount: () => render(null, node),
11+
}
12+
}
13+
14+
// The intention here is that Child components are passed in here so we check that the
15+
// children of "the-parent" are "child-1" through "child-N"
16+
export function Parent(props) {
17+
return html`
18+
<div>
19+
<p>the parent</p>
20+
<ul id="the-parent">${props.children}</div>
21+
</div>
22+
`
23+
}
24+
25+
export function Child({ index }) {
26+
return html`<li id="child-${index}">child ${index}</li>`
27+
}

tests/test_web/test_module.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,26 @@ def test_module_exports_multiple_components(driver, display):
168168
display(lambda: Header2({"id": "my-h2"}, "My Header 2"))
169169

170170
driver.find_element_by_id("my-h2")
171+
172+
173+
def test_imported_components_can_render_children(driver, display):
174+
module = idom.web.module_from_file(
175+
"component-can-have-child", JS_FIXTURES_DIR / "component-can-have-child.js"
176+
)
177+
Parent, Child = idom.web.export(module, ["Parent", "Child"])
178+
179+
display(
180+
lambda: Parent(
181+
Child({"index": 1}),
182+
Child({"index": 2}),
183+
Child({"index": 3}),
184+
)
185+
)
186+
187+
parent = driver.find_element_by_id("the-parent")
188+
children = parent.find_elements_by_tag_name("li")
189+
190+
assert len(children) == 3
191+
192+
for index, child in enumerate(children):
193+
assert child.get_attribute("id") == f"child-{index + 1}"

0 commit comments

Comments
 (0)