Skip to content

Commit 76246df

Browse files
committed
fix up the tests
1 parent 03d6386 commit 76246df

16 files changed

+182
-67
lines changed

docs/source/examples/material_ui_button_no_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import idom
22

33
material_ui = idom.install("@material-ui/core")
4-
MaterialButton = material_ui.use("Button", fallback="loading...")
4+
MaterialButton = material_ui.define("Button", fallback="loading...")
55

66
idom.run(
77
idom.element(

docs/source/examples/material_ui_button_on_click.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
material_ui = idom.install("@material-ui/core")
7-
MaterialButton = material_ui.use("Button", fallback="loading...")
7+
MaterialButton = material_ui.define("Button", fallback="loading...")
88

99

1010
@idom.element

docs/source/examples/material_ui_slider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
material_ui = idom.install("@material-ui/core")
7-
MaterialSlider = material_ui.use("Slider", fallback="loading...")
7+
MaterialSlider = material_ui.define("Slider", fallback="loading...")
88

99

1010
@idom.element

docs/source/examples/simple_dashboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from idom.widgets.html import Input
77

88

9-
VictoryLine = idom.install("victory").use("VictoryLine", fallback="loading...")
9+
VictoryLine = idom.install("victory").define("VictoryLine", fallback="loading...")
1010

1111

1212
@idom.element

docs/source/examples/super_simple_chart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
path_to_source_file = Path(__file__).parent / "super_simple_chart.js"
77
super_simple_chart = idom.Module("super-simple-chart", source_file=path_to_source_file)
8-
SuperSimpleChart = super_simple_chart.use("SuperSimpleChart")
8+
SuperSimpleChart = super_simple_chart.define("SuperSimpleChart")
99

1010

1111
idom.run(

docs/source/examples/victory_chart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import idom
22

33
victory = idom.install("victory")
4-
VictoryBar = victory.use("VictoryBar", fallback="loading...")
4+
VictoryBar = victory.define("VictoryBar", fallback="loading...")
55

66
idom.run(
77
idom.element(

idom/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# pragma: no cover
21
import sys
32

43
from loguru import logger

idom/client/manage.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
from pathlib import Path
55
from tempfile import TemporaryDirectory
6-
from typing import Optional, List, Set, Union, Sequence
6+
from typing import List, Set, Union, Sequence
77

88
from loguru import logger
99

@@ -24,7 +24,7 @@ def web_module_exports(package_name: str) -> List[str]:
2424
return find_js_module_exports_in_source(web_module_path(package_name).read_text())
2525

2626

27-
def web_module_url(package_name: str) -> Optional[str]:
27+
def web_module_url(package_name: str) -> str:
2828
web_module_path(package_name, must_exist=True)
2929
return f"../web_modules/{package_name}.js"
3030

@@ -49,8 +49,6 @@ def add_web_module(package_name: str, source: Union[Path, str]) -> str:
4949
raise FileNotFoundError(f"Package source file does not exist: {str(source)!r}")
5050
target = web_module_path(package_name)
5151
target.parent.mkdir(parents=True, exist_ok=True)
52-
if target.exists():
53-
target.unlink()
5452
target.symlink_to(source.absolute())
5553
return web_module_url(package_name)
5654

idom/client/module.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import Any, Optional, Union, List, Sequence, overload
2+
from typing import Any, Optional, Union, List, Tuple, Dict, overload
33
from urllib.parse import urlparse
44

55
from idom.core.vdom import VdomDict, ImportSourceDict, make_vdom_constructor
@@ -15,20 +15,22 @@ def install(packages: str) -> "Module":
1515

1616

1717
@overload
18-
def install(packages: Sequence[str]) -> List["Module"]:
18+
def install(packages: Union[List[str], Tuple[str]]) -> List["Module"]:
1919
...
2020

2121

2222
def install(
23-
packages: Sequence[str], ignore_installed: bool = False
23+
packages: Union[str, List[str], Tuple[str]], ignore_installed: bool = False
2424
) -> Union["Module", List["Module"]]:
25-
return_one = isinstance(packages, str)
26-
if return_one:
25+
if isinstance(packages, str):
2726
packages = [packages]
27+
return_one = True
28+
else:
29+
return_one = False
2830

2931
pkg_names = {get_package_name(pkg) for pkg in packages}
3032

31-
if pkg_names.difference(client.current.web_module_names()):
33+
if ignore_installed or pkg_names.difference(client.current.web_module_names()):
3234
builtin_client.build(packages)
3335
not_discovered = pkg_names.difference(client.current.web_module_names())
3436
if not_discovered:
@@ -37,10 +39,7 @@ def install(
3739
f"{client.current} failed to discover {list(not_discovered)}."
3840
)
3941

40-
if return_one:
41-
return Module(pkg_names.pop())
42-
else:
43-
return list(map(Module, pkg_names))
42+
return Module(pkg_names.pop()) if return_one else [Module(pkg) for pkg in pkg_names]
4443

4544

4645
class Module:
@@ -77,7 +76,11 @@ def __init__(
7776
self.fallback = fallback
7877
self.exports: Optional[List[str]] = None
7978
if source_file is not None:
80-
self.url = client.current.add_web_module(url_or_name, source_file)
79+
self.url = (
80+
client.current.web_module_url(url_or_name)
81+
if client.current.web_module_exists(url_or_name)
82+
else client.current.add_web_module(url_or_name, source_file)
83+
)
8184
if check_exports:
8285
self.exports = client.current.web_module_exports(url_or_name)
8386
elif client.current.web_module_exists(url_or_name):
@@ -86,14 +89,10 @@ def __init__(
8689
self.exports = client.current.web_module_exports(url_or_name)
8790
elif _is_url(url_or_name):
8891
self.url = url_or_name
89-
self.installed = False
9092
else:
91-
raise ValueError(
92-
f"{url_or_name!r} is not installed or is not a URL - "
93-
"only installed modules can omit a file extension."
94-
)
93+
raise ValueError(f"{url_or_name!r} is not installed or is not a URL")
9594

96-
def use(
95+
def define(
9796
self,
9897
name: str,
9998
has_children: bool = True,
@@ -161,7 +160,7 @@ def __call__(
161160
return self._constructor(import_source=self._import_source, *args, **kwargs)
162161

163162
def __repr__(self) -> str:
164-
info = {"name": self._name, **self._import_source}
163+
info: Dict[str, Any] = {"name": self._name, **self._import_source}
165164
strings = ", ".join(f"{k}={v!r}" for k, v in info.items())
166165
return f"{type(self).__name__}({strings})"
167166

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ def last_server_error():
261261
return idom.Ref(default_error)
262262

263263

264+
@pytest.fixture
265+
def client_implementation():
266+
original = idom.client.current
267+
try:
268+
yield idom.client
269+
finally:
270+
idom.client.current = original
271+
272+
264273
@pytest.fixture(autouse=True)
265274
def _clean_last_server_error(last_server_error) -> Iterator[None]:
266275
last_server_error.current = default_error

tests/test_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typer.testing import CliRunner
2+
3+
import idom
4+
from idom.cli import main
5+
from idom.client.manage import web_module_exists
6+
7+
8+
cli_runner = CliRunner()
9+
10+
11+
def test_install():
12+
result = cli_runner.invoke(main, ["install", "jquery"])
13+
assert result.exit_code == 0
14+
assert web_module_exists("jquery")
15+
16+
17+
def test_restore():
18+
result = cli_runner.invoke(main, ["restore"])
19+
assert result.exit_code == 0
20+
21+
22+
def test_show_version():
23+
result = cli_runner.invoke(main, ["show", "version"])
24+
assert idom.__version__ in result.stdout

tests/test_client/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55

66
@pytest.fixture
7-
def victory_js(install):
7+
def victory():
88
return idom.install("victory@35.4.0")

tests/test_client/test_js_module.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "./react.js";
2+
import htm from "./htm.js";
3+
4+
const html = htm.bind(React.createElement);
5+
6+
export function TestButton(props) {
7+
return html`<button
8+
id=${props.id}
9+
onClick=${(event) => props.onClick({ data: props.eventResponseData })}
10+
>
11+
test button
12+
</button>`;
13+
}

tests/test_client/test_manage.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
import pytest
22

3-
from idom.client.manage import web_module_url, web_module_exports, web_module_exists
3+
from idom.client.manage import (
4+
web_module_url,
5+
web_module_exports,
6+
web_module_exists,
7+
web_module_path,
8+
add_web_module,
9+
)
410

511
from tests.general_utils import assert_same_items
612

713

8-
def test_web_module_url(victory_js):
9-
assert (
10-
web_module_url("tests", "victory") == "../web_modules/victory-tests-24fa38b.js"
11-
)
12-
14+
def test_add_web_module_source_must_exist(tmp_path):
15+
with pytest.raises(FileNotFoundError, match="Package source file does not exist"):
16+
add_web_module("test", tmp_path / "file-does-not-exist.js")
1317

14-
def test_bad_install(temp_build_config):
15-
temp_build_config.update_entries(
16-
[{"source_name": "tests", "js_dependencies": ["some-dep"]}]
17-
)
18-
with pytest.raises(WebModuleError, match="not installed"):
19-
web_module_url("tests", "some-dep")
2018

19+
def test_web_module_path_must_exist():
20+
with pytest.raises(ValueError, match="does not exist at path"):
21+
web_module_path("this-does-not-exist", must_exist=True)
22+
assert not web_module_path("this-does-not-exist", must_exist=False).exists()
2123

22-
def test_web_module_exists(temp_build_config, victory_js):
23-
assert not web_module_exists("tests", "does/not/exist")
24-
assert web_module_exists("tests", "victory")
2524

26-
with temp_build_config.change_entry("tests") as entry:
27-
entry["js_dependencies"].append("module_not_installed_yet")
28-
assert not web_module_exists("tests", "module_not_installed_yet")
25+
def test_web_module_url(victory):
26+
assert web_module_exists("victory")
27+
assert web_module_url("victory") == "../web_modules/victory.js"
2928

3029

31-
def test_web_module_exports(victory_js):
30+
def test_web_module_exports(victory):
3231
assert_same_items(
33-
web_module_exports("tests", "victory"),
32+
web_module_exports("victory"),
3433
[
3534
"Area",
3635
"Axis",

0 commit comments

Comments
 (0)