Skip to content

Commit

Permalink
Add RichTextList element and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parente committed Jan 9, 2024
1 parent 6be026b commit 698453b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
22 changes: 17 additions & 5 deletions blockkit/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic.networks import HttpUrl

from blockkit.components import Component
from blockkit.enums import Style
from blockkit.enums import ListStyle, Style
from blockkit.objects import (
Confirm,
DispatchActionConfig,
Expand Down Expand Up @@ -679,10 +679,6 @@ def __init__(
RichTextObject = Union[Text, Emoji]


class RichTextList(Component):
type: str = "rich_text_list"


class RichTextPreformatted(Component):
type: str = "rich_text_preformatted"
elements: List[RichTextObject] = Field(..., min_length=1)
Expand All @@ -705,3 +701,19 @@ class RichTextSection(Component):

def __init__(self, *, elements: List[RichTextObject]):
super().__init__(elements=elements)


class RichTextList(Component):
type: str = "rich_text_list"
elements: List[RichTextSection] = Field(..., min_length=1)
style: Optional[ListStyle] = None
indent: Optional[int] = Field(..., ge=0, le=8)

def __init__(
self,
*,
elements: List[RichTextSection],
style: Optional[ListStyle] = None,
indent: Optional[int] = None,
):
super().__init__(elements=elements, style=style, indent=indent)
7 changes: 6 additions & 1 deletion blockkit/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from enum import Enum

__all__ = ["Style", "TriggerActionsOn", "Include"]
__all__ = ["ListStyle", "Style", "TriggerActionsOn", "Include"]


class ListStyle(str, Enum):
ordered = "ordered"
bullet = "bullet"


class Style(str, Enum):
Expand Down
64 changes: 64 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from blockkit.elements import (
Button,
Image,
RichTextList,
RichTextPreformatted,
RichTextQuote,
RichTextSection,
Expand Down Expand Up @@ -291,6 +292,26 @@ def test_builds_rich_text():
Text(text="a section"),
]
),
RichTextList(
style="ordered",
elements=[
RichTextSection(elements=[Text(text="a bullet")]),
RichTextSection(elements=[Text(text="a another bullet")]),
],
),
RichTextList(
style="ordered",
indent=1,
elements=[
RichTextSection(elements=[Text(text="a nested bullet")]),
],
),
RichTextList(
style="ordered",
elements=[
RichTextSection(elements=[Text(text="an un-nested bullet")]),
],
),
]
).build() == {
"type": "rich_text",
Expand All @@ -313,6 +334,49 @@ def test_builds_rich_text():
{"type": "text", "text": "a section"},
],
},
{
"type": "rich_text_list",
"style": "ordered",
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "a bullet"},
],
},
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "a another bullet"},
],
},
],
},
{
"type": "rich_text_list",
"style": "ordered",
"indent": 1,
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "a nested bullet"},
],
},
],
},
{
"type": "rich_text_list",
"style": "ordered",
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "an un-nested bullet"},
],
},
],
},
],
}

Expand Down
50 changes: 50 additions & 0 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Overflow,
PlainTextInput,
RadioButtons,
RichTextList,
RichTextPreformatted,
RichTextQuote,
RichTextSection,
Expand Down Expand Up @@ -1467,6 +1468,55 @@ def test_empty_rich_text_section_raises_exception():
RichTextSection(elements=[])


def test_builds_rich_text_list():
assert RichTextList(
style="ordered",
indent=1,
elements=[
RichTextSection(elements=[Text(text="My first bullet point")]),
RichTextSection(elements=[Text(text="My second bullet point")]),
],
).build() == {
"type": "rich_text_list",
"style": "ordered",
"indent": 1,
"elements": [
{
"type": "rich_text_section",
"elements": [{"type": "text", "text": "My first bullet point"}],
},
{
"type": "rich_text_section",
"elements": [{"type": "text", "text": "My second bullet point"}],
},
],
}


def test_empty_rich_text_list_raises_exception():
with pytest.raises(ValidationError):
RichTextList(elements=[])


@pytest.fixture(scope="module")
def minimal_rich_text_list_elements():
return [RichTextSection(elements=[Text(text="a")])]


def test_invalid_rich_text_list_style_raises_exception(minimal_rich_text_list_elements):
with pytest.raises(ValidationError):
RichTextList(elements=minimal_rich_text_list_elements, style="invalid")


def test_invalid_rich_text_list_indent_raises_exception(
minimal_rich_text_list_elements,
):
with pytest.raises(ValidationError):
RichTextList(elements=minimal_rich_text_list_elements, indent=-1)
with pytest.raises(ValidationError):
RichTextList(elements=minimal_rich_text_list_elements, indent=9)


def test_builds_timepicker():
assert TimePicker(
action_id="action_id",
Expand Down

0 comments on commit 698453b

Please sign in to comment.