Add tests for pc.icon (#562)

This commit is contained in:
Nikhil Rao 2023-02-17 12:01:30 -08:00 committed by GitHub
parent 12faa76279
commit f88d4ce4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 0 deletions

View File

@ -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",
]

View File

@ -0,0 +1 @@
"""Graphing component tests."""

View File

@ -0,0 +1 @@
"""Layout component tests."""

View File

@ -0,0 +1 @@
"""Media component tests."""

View File

@ -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)