Skip to content
96 changes: 96 additions & 0 deletions tests/ui_tools/test_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MessageLinkButton,
ParsedNarrowLink,
PMButton,
SpoilerButton,
StarredButton,
StreamButton,
TopButton,
Expand Down Expand Up @@ -308,6 +309,101 @@ def test_keypress_USER_INFO(
pop_up.assert_called_once_with(user_button.user_id)


class TestSpoilerButton:
@pytest.fixture(autouse=True)
def mock_external_classes(self, mocker: MockerFixture) -> None:
self.controller = mocker.Mock()
self.super_init = mocker.patch(MODULE + ".urwid.Button.__init__")
self.connect_signal = mocker.patch(MODULE + ".urwid.connect_signal")

def spoiler_button(
self,
header_len: int = 0,
header: List[Any] = [""],
content: List[Any] = [""],
message: Message = {},
topic_links: Dict[str, Tuple[str, int, bool, bool]] = {},
message_links: Dict[str, Tuple[str, int, bool, bool]] = {},
time_mentions: List[Tuple[str, str]] = [],
spoilers: List[Tuple[int, List[Any], List[Any]]] = [],
display_attr: Optional[str] = None,
) -> SpoilerButton:
self.content = content
self.header_len = header_len
self.header = header
self.message = message
self.topic_links = topic_links
self.message_links = message_links
self.time_mentions = time_mentions
self.spoilers = spoilers
self.display_attr = display_attr
return SpoilerButton(
self.controller,
header_len,
header,
content,
message,
topic_links,
message_links,
time_mentions,
spoilers,
display_attr,
)

def test_init(self, mocker: MockerFixture) -> None:
self.update_widget = mocker.patch(MODULE + ".SpoilerButton.update_widget")

mocked_button = self.spoiler_button()

assert mocked_button.controller == self.controller
assert mocked_button.content == self.content
self.super_init.assert_called_once_with("")
self.update_widget.assert_called_once_with(
self.header_len, self.header, self.display_attr
)
assert self.connect_signal.called

@pytest.mark.parametrize(
"header, header_len, expected_cursor_position",
[
(["Test"], 4, 5),
(["Check"], 5, 6),
],
)
def test_update_widget(
self,
mocker: MockerFixture,
header: List[Any],
header_len: int,
expected_cursor_position: int,
display_attr: Optional[str] = None,
) -> None:
self.selectable_icon = mocker.patch(MODULE + ".urwid.SelectableIcon")

# The method update_widget() is called in SpoilerButton's init.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this suggestion.
But, can we mix these 2 test functions since they're both having the same line of code tested? Just different assertions.
Removing the mocking of update_widget in the test_init, and adding a parameter set with None should work?

mocked_button = self.spoiler_button(
header=header, header_len=header_len, display_attr=display_attr
)
self.selectable_icon.assert_called_once_with(
header, cursor_position=expected_cursor_position
)
assert isinstance(mocked_button._w, AttrMap)

def test_show_spoiler(self) -> None:
mocked_button = self.spoiler_button()

mocked_button.show_spoiler()

mocked_button.controller.show_spoiler.assert_called_once_with(
mocked_button.content,
mocked_button.message,
mocked_button.topic_links,
mocked_button.message_links,
mocked_button.time_mentions,
mocked_button.spoilers,
)


class TestEmojiButton:
@pytest.mark.parametrize(
"emoji_unit, to_vary_in_message, count",
Expand Down
72 changes: 61 additions & 11 deletions tests/ui_tools/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,52 @@ def test_private_message_to_self(self, mocker):
[("msg_emoji", ":github:")],
id="custom_emoji",
),
case(
'<div class="spoiler-block"><div class="spoiler-header"></div>'
'<div aria-hidden="true" class="spoiler-content">'
"<p>Test</p></div></div>",
[
"┌─",
"────────",
"─┬─",
"───────",
"─┐\n",
"│ ",
("msg_spoiler", "Spoiler:"),
" │ ",
"Spoiler",
" │\n",
"└─",
"────────",
"─┴─",
"───────",
"─┘",
],
id="spoiler_no_header",
),
case(
'<div class="spoiler-block"><div class="spoiler-header">'
'<p>Header</p></div><div aria-hidden="true" class="spoiler-content">'
"<p>Test</p></div></div>",
[
"┌─",
"────────",
"─┬─",
"──────",
"─┐\n",
"│ ",
("msg_spoiler", "Spoiler:"),
" │ ",
"Header",
" │\n",
"└─",
"────────",
"─┴─",
"──────",
"─┘",
],
id="spoiler_with_header",
),
],
)
def test_soup2markup(self, content, expected_markup, mocker):
Expand All @@ -680,6 +726,7 @@ def test_soup2markup(self, content, expected_markup, mocker):
server_url=SERVER_URL,
message_links=OrderedDict(),
time_mentions=list(),
spoilers=list(),
bq_len=0,
)

Expand Down Expand Up @@ -1834,7 +1881,7 @@ def test_reactions_view(
[
(
"https://github.com/zulip/zulip-terminal/pull/1",
("#T1", 1, True),
("#T1", 1, True, False),
),
]
),
Expand All @@ -1846,8 +1893,8 @@ def test_reactions_view(
case(
OrderedDict(
[
("https://foo.com", ("Foo!", 1, True)),
("https://bar.com", ("Bar!", 2, True)),
("https://foo.com", ("Foo!", 1, True, False)),
("https://bar.com", ("Bar!", 2, True, False)),
]
),
"1: https://foo.com\n2: https://bar.com",
Expand All @@ -1866,8 +1913,11 @@ def test_reactions_view(
case(
OrderedDict(
[
("https://example.com", ("https://example.com", 1, False)),
("http://example.com", ("http://example.com", 2, False)),
(
"https://example.com",
("https://example.com", 1, False, False),
),
("http://example.com", ("http://example.com", 2, False, False)),
]
),
None,
Expand All @@ -1878,8 +1928,8 @@ def test_reactions_view(
case(
OrderedDict(
[
("https://foo.com", ("https://foo.com, Text", 1, True)),
("https://bar.com", ("Text, https://bar.com", 2, True)),
("https://foo.com", ("https://foo.com, Text", 1, True, False)),
("https://bar.com", ("Text, https://bar.com", 2, True, False)),
]
),
"1: https://foo.com\n2: https://bar.com",
Expand All @@ -1898,9 +1948,9 @@ def test_reactions_view(
case(
OrderedDict(
[
("https://foo.com", ("Foo!", 1, True)),
("http://example.com", ("example.com", 2, False)),
("https://bar.com", ("Bar!", 3, True)),
("https://foo.com", ("Foo!", 1, True, False)),
("http://example.com", ("example.com", 2, False, False)),
("https://bar.com", ("Bar!", 3, True, False)),
]
),
"1: https://foo.com\n3: https://bar.com",
Expand Down Expand Up @@ -1947,7 +1997,7 @@ def test_footlinks_view(
def test_footlinks_limit(self, maximum_footlinks, expected_instance):
message_links = OrderedDict(
[
("https://github.com/zulip/zulip-terminal", ("ZT", 1, True)),
("https://github.com/zulip/zulip-terminal", ("ZT", 1, True, False)),
]
)

Expand Down
Loading