Skip to content

Commit e622858

Browse files
committed
add test for new unmount callback
1 parent 699cc66 commit e622858

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

tests/test_client/js/component-without-mount.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function render(element, component, props) {
2+
if (element.firstChild) {
3+
element.removeChild(element.firstChild);
4+
}
5+
element.appendChild(component(props));
6+
}
7+
8+
export function unmount(element) {
9+
// We add an element to the document.body to indicate that this function was called.
10+
// Thus allowing Selenium to see communicate to server-side code that this effect
11+
// did indeed occur.
12+
const unmountFlag = document.createElement("h1");
13+
unmountFlag.setAttribute("id", "unmount-flag");
14+
document.body.appendChild(unmountFlag);
15+
element.innerHTML = "";
16+
}
17+
18+
export function SomeComponent(props) {
19+
const element = document.createElement("h1");
20+
element.appendChild(document.createTextNode(props.text));
21+
element.setAttribute("id", props.id);
22+
return element;
23+
}

tests/test_client/test_app.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from idom.testing import ServerMountPoint
55

66

7-
HERE = Path(__file__).parent
7+
JS_DIR = Path(__file__).parent / "js"
88

99

1010
def test_automatic_reconnect(create_driver):
@@ -43,3 +43,35 @@ def NewComponent():
4343
# check that we can resume normal operation
4444
set_state.current(1)
4545
driver.find_element_by_id("new-component-1")
46+
47+
48+
def test_that_js_module_unmount_is_called(driver, driver_wait, display):
49+
module = idom.Module(
50+
"set-flag-when-unmount-is-called",
51+
source_file=JS_DIR / "set-flag-when-unmount-is-called.js",
52+
)
53+
54+
set_current_component = idom.Ref(None)
55+
56+
@idom.component
57+
def ShowCurrentComponent():
58+
current_component, set_current_component.current = idom.hooks.use_state(
59+
lambda: module.SomeComponent(
60+
{"id": "some-component", "text": "initial component"}
61+
)
62+
)
63+
return current_component
64+
65+
display(ShowCurrentComponent)
66+
67+
driver.find_element_by_id("some-component")
68+
69+
set_current_component.current(
70+
idom.html.h1({"id": "some-other-component"}, "some other component")
71+
)
72+
73+
# the new component has been displayed
74+
driver.find_element_by_id("some-other-component")
75+
76+
# the unmount callback for the old component was called
77+
driver.find_element_by_id("unmount-flag")

0 commit comments

Comments
 (0)