diff --git a/pynecone/components/media/icon.py b/pynecone/components/media/icon.py index 20208d2bd..0d9ccf4b8 100644 --- a/pynecone/components/media/icon.py +++ b/pynecone/components/media/icon.py @@ -27,6 +27,7 @@ class Icon(ChakraIconComponent): Raises: AttributeError: The errors tied to bad usage of the Icon component. + ValueError: If the icon tag is invalid. Returns: The created component. @@ -37,5 +38,71 @@ class Icon(ChakraIconComponent): ) if "tag" not in props.keys(): raise AttributeError("Missing 'tag' keyword-argument for Icon") + if props["tag"] not in ICON_LIST: + raise ValueError( + f"Invalid icon tag: {props['tag']}. Please use one of the following: {ICON_LIST}" + ) props["tag"] = utils.to_title_case(props["tag"]) + "Icon" return super().create(*children, **props) + + +# List of all icons. +ICON_LIST = [ + "add", + "arrow_back", + "arrow_down", + "arrow_forward", + "arrow_left", + "arrow_right", + "arrow_up", + "arrow_up_down", + "at_sign", + "attachment", + "bell", + "calendar", + "check_circle", + "check", + "chevron_down", + "chevron_left", + "chevron_right", + "chevron_up", + "close", + "copy", + "delete", + "download", + "drag_handle", + "edit", + "email", + "external_link", + "hamburger", + "info", + "info_outline", + "link", + "lock", + "minus", + "moon", + "not_allowed", + "phone", + "plus_square", + "question", + "question_outline", + "repeat", + "repeat_clock", + "search", + "search2", + "settings", + "small_add", + "small_close", + "spinner", + "star", + "sun", + "time", + "triangle_down", + "triangle_up", + "unlock", + "up_down", + "view", + "view_off", + "warning", + "warning_two", +] diff --git a/tests/components/graphing/__init__.py b/tests/components/graphing/__init__.py new file mode 100644 index 000000000..de2b4019d --- /dev/null +++ b/tests/components/graphing/__init__.py @@ -0,0 +1 @@ +"""Graphing component tests.""" diff --git a/tests/components/layout/__init__.py b/tests/components/layout/__init__.py new file mode 100644 index 000000000..c0f28ad5c --- /dev/null +++ b/tests/components/layout/__init__.py @@ -0,0 +1 @@ +"""Layout component tests.""" diff --git a/tests/components/media/__init__.py b/tests/components/media/__init__.py new file mode 100644 index 000000000..be65dfd7c --- /dev/null +++ b/tests/components/media/__init__.py @@ -0,0 +1 @@ +"""Media component tests.""" diff --git a/tests/components/media/test_icon.py b/tests/components/media/test_icon.py new file mode 100644 index 000000000..dc1480b0d --- /dev/null +++ b/tests/components/media/test_icon.py @@ -0,0 +1,41 @@ +import pytest + +from pynecone import utils +from pynecone.components.media.icon import ICON_LIST, Icon + + +def test_no_tag_errors(): + """Test that an icon without a tag raises an error.""" + with pytest.raises(AttributeError): + Icon.create() + + +def test_children_errors(): + """Test that an icon with children raises an error.""" + with pytest.raises(AttributeError): + Icon.create("child", tag="search") + + +@pytest.mark.parametrize( + "tag", + ICON_LIST, +) +def test_valid_icon(tag: str): + """Test that a valid icon does not raise an error. + + Args: + tag: The icon tag. + """ + icon = Icon.create(tag=tag) + assert icon.tag == utils.to_title_case(tag) + "Icon" + + +@pytest.mark.parametrize("tag", ["", " ", "invalid", 123]) +def test_invalid_icon(tag): + """Test that an invalid icon raises an error. + + Args: + tag: The icon tag. + """ + with pytest.raises(ValueError): + Icon.create(tag=tag)