|
| 1 | +from idom import component, html, use_state |
| 2 | +from idom.utils import Ref |
| 3 | + |
| 4 | + |
| 5 | +def test_script_mount_unmount(driver, driver_wait, display): |
| 6 | + toggle_is_mounted = Ref() |
| 7 | + |
| 8 | + def use_toggle(): |
| 9 | + state, set_state = use_state(True) |
| 10 | + return state, lambda: set_state(not state) |
| 11 | + |
| 12 | + @component |
| 13 | + def Root(): |
| 14 | + is_mounted, toggle_is_mounted.current = use_toggle() |
| 15 | + if is_mounted: |
| 16 | + el = HasScript() |
| 17 | + else: |
| 18 | + el = html.div() |
| 19 | + |
| 20 | + return html.div( |
| 21 | + html.div({"id": "mount-state", "data-value": False}), |
| 22 | + el, |
| 23 | + ) |
| 24 | + |
| 25 | + @component |
| 26 | + def HasScript(): |
| 27 | + return html.script( |
| 28 | + """() => { |
| 29 | + const mapping = {"false": false, "true": true}; |
| 30 | + const mountStateEl = document.getElementById("mount-state"); |
| 31 | + mountStateEl.setAttribute( |
| 32 | + "data-value", !mapping[mountStateEl.getAttribute("data-value")]); |
| 33 | + return () => mountStateEl.setAttribute( |
| 34 | + "data-value", !mapping[mountStateEl.getAttribute("data-value")]); |
| 35 | + }""" |
| 36 | + ) |
| 37 | + |
| 38 | + display(Root) |
| 39 | + |
| 40 | + mount_state = driver.find_element("id", "mount-state") |
| 41 | + |
| 42 | + driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "true") |
| 43 | + |
| 44 | + toggle_is_mounted.current() |
| 45 | + |
| 46 | + driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "false") |
| 47 | + |
| 48 | + toggle_is_mounted.current() |
| 49 | + |
| 50 | + driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "true") |
| 51 | + |
| 52 | + |
| 53 | +def test_script_re_run_on_content_change(driver, driver_wait, display): |
| 54 | + incr_count = Ref() |
| 55 | + |
| 56 | + def use_counter(): |
| 57 | + state, set_state = use_state(1) |
| 58 | + return state, lambda: set_state(state + 1) |
| 59 | + |
| 60 | + @component |
| 61 | + def HasScript(): |
| 62 | + count, incr_count.current = use_counter() |
| 63 | + return html.div( |
| 64 | + html.div({"id": "mount-count", "data-value": 0}), |
| 65 | + html.div({"id": "unmount-count", "data-value": 0}), |
| 66 | + html.script( |
| 67 | + f"""() => {{ |
| 68 | + const mountCountEl = document.getElementById("mount-count"); |
| 69 | + const unmountCountEl = document.getElementById("unmount-count"); |
| 70 | + mountCountEl.setAttribute("data-value", {count}); |
| 71 | + return () => unmountCountEl.setAttribute("data-value", {count});; |
| 72 | + }}""" |
| 73 | + ), |
| 74 | + ) |
| 75 | + |
| 76 | + display(HasScript) |
| 77 | + |
| 78 | + mount_count = driver.find_element("id", "mount-count") |
| 79 | + unmount_count = driver.find_element("id", "unmount-count") |
| 80 | + |
| 81 | + driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "1") |
| 82 | + driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "0") |
| 83 | + |
| 84 | + incr_count.current() |
| 85 | + |
| 86 | + driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "2") |
| 87 | + driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "1") |
| 88 | + |
| 89 | + incr_count.current() |
| 90 | + |
| 91 | + driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "3") |
| 92 | + driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "2") |
0 commit comments