|
1 | 1 | from pathlib import Path
|
2 | 2 |
|
| 3 | +import pytest |
| 4 | +from sanic import Sanic |
| 5 | +from selenium.webdriver.common.by import By |
| 6 | +from selenium.webdriver.support import expected_conditions |
| 7 | +from selenium.webdriver.support.ui import WebDriverWait |
| 8 | + |
3 | 9 | import idom
|
| 10 | +from idom.server.sanic import PerClientStateServer |
| 11 | +from idom.testing import ServerMountPoint |
| 12 | +from idom.web.module import NAME_SOURCE, WebModule |
4 | 13 |
|
5 | 14 |
|
6 | 15 | JS_FIXTURES_DIR = Path(__file__).parent / "js_fixtures"
|
7 | 16 |
|
8 | 17 |
|
9 |
| -def test_that_js_module_unmount_is_called(driver, driver_wait, display): |
| 18 | +def test_that_js_module_unmount_is_called(driver, display): |
10 | 19 | SomeComponent = idom.web.export(
|
11 | 20 | idom.web.module_from_file(
|
12 | 21 | "set-flag-when-unmount-is-called",
|
@@ -37,3 +46,112 @@ def ShowCurrentComponent():
|
37 | 46 |
|
38 | 47 | # the unmount callback for the old component was called
|
39 | 48 | driver.find_element_by_id("unmount-flag")
|
| 49 | + |
| 50 | + |
| 51 | +def test_module_from_url(driver): |
| 52 | + app = Sanic() |
| 53 | + |
| 54 | + # instead of directing the URL to a CDN, we just point it to this static file |
| 55 | + app.static( |
| 56 | + "/simple-button.js", |
| 57 | + str(JS_FIXTURES_DIR / "simple-button.js"), |
| 58 | + content_type="text/javascript", |
| 59 | + ) |
| 60 | + |
| 61 | + SimpleButton = idom.web.export( |
| 62 | + idom.web.module_from_url("/simple-button.js", resolve_exports=False), |
| 63 | + "SimpleButton", |
| 64 | + ) |
| 65 | + |
| 66 | + @idom.component |
| 67 | + def ShowSimpleButton(): |
| 68 | + return SimpleButton({"id": "my-button"}) |
| 69 | + |
| 70 | + with ServerMountPoint(PerClientStateServer, app=app) as mount_point: |
| 71 | + mount_point.mount(ShowSimpleButton) |
| 72 | + driver.get(mount_point.url()) |
| 73 | + driver.find_element_by_id("my-button") |
| 74 | + |
| 75 | + |
| 76 | +def test_module_from_template_where_template_does_not_exist(): |
| 77 | + with pytest.raises(ValueError, match="No template for 'does-not-exist.js'"): |
| 78 | + idom.web.module_from_template("does-not-exist", "something.js") |
| 79 | + |
| 80 | + |
| 81 | +def test_module_from_template(driver, display): |
| 82 | + victory = idom.web.module_from_template("react", "victory@35.4.0") |
| 83 | + VictoryBar = idom.web.export(victory, "VictoryBar") |
| 84 | + display(VictoryBar) |
| 85 | + wait = WebDriverWait(driver, 10) |
| 86 | + wait.until( |
| 87 | + expected_conditions.visibility_of_element_located( |
| 88 | + (By.CLASS_NAME, "VictoryContainer") |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_module_from_file(driver, driver_wait, display): |
| 94 | + SimpleButton = idom.web.export( |
| 95 | + idom.web.module_from_file( |
| 96 | + "simple-button", JS_FIXTURES_DIR / "simple-button.js" |
| 97 | + ), |
| 98 | + "SimpleButton", |
| 99 | + ) |
| 100 | + |
| 101 | + is_clicked = idom.Ref(False) |
| 102 | + |
| 103 | + @idom.component |
| 104 | + def ShowSimpleButton(): |
| 105 | + return SimpleButton( |
| 106 | + {"id": "my-button", "onClick": lambda event: is_clicked.set_current(True)} |
| 107 | + ) |
| 108 | + |
| 109 | + display(ShowSimpleButton) |
| 110 | + |
| 111 | + button = driver.find_element_by_id("my-button") |
| 112 | + button.click() |
| 113 | + driver_wait.until(lambda d: is_clicked.current) |
| 114 | + |
| 115 | + |
| 116 | +def test_module_from_file_source_conflict(tmp_path): |
| 117 | + first_file = tmp_path / "first.js" |
| 118 | + |
| 119 | + with pytest.raises(FileNotFoundError, match="does not exist"): |
| 120 | + idom.web.module_from_file("temp", first_file) |
| 121 | + |
| 122 | + first_file.touch() |
| 123 | + |
| 124 | + idom.web.module_from_file("temp", first_file) |
| 125 | + |
| 126 | + second_file = tmp_path / "second.js" |
| 127 | + second_file.touch() |
| 128 | + |
| 129 | + with pytest.raises(FileExistsError, match="already exists"): |
| 130 | + idom.web.module_from_file("temp", second_file) |
| 131 | + |
| 132 | + |
| 133 | +def test_module_missing_exports(): |
| 134 | + module = WebModule("test", NAME_SOURCE, None, {"a", "b", "c"}, None) |
| 135 | + |
| 136 | + with pytest.raises(ValueError, match="does not export 'x'"): |
| 137 | + idom.web.export(module, "x") |
| 138 | + |
| 139 | + with pytest.raises(ValueError, match=r"does not export \['x', 'y'\]"): |
| 140 | + idom.web.export(module, ["x", "y"]) |
| 141 | + |
| 142 | + |
| 143 | +def test_module_exports_multiple_components(driver, display): |
| 144 | + Header1, Header2 = idom.web.export( |
| 145 | + idom.web.module_from_file( |
| 146 | + "exports-two-components", JS_FIXTURES_DIR / "exports-two-components.js" |
| 147 | + ), |
| 148 | + ["Header1", "Header2"], |
| 149 | + ) |
| 150 | + |
| 151 | + display(lambda: Header1({"id": "my-h1"}, "My Header 1")) |
| 152 | + |
| 153 | + driver.find_element_by_id("my-h1") |
| 154 | + |
| 155 | + display(lambda: Header2({"id": "my-h2"}, "My Header 2")) |
| 156 | + |
| 157 | + driver.find_element_by_id("my-h2") |
0 commit comments