Skip to content

Commit

Permalink
tests: boxes: Add tests for saving drafts.
Browse files Browse the repository at this point in the history
Co-authored-by: Manu_K_Paul <[email protected]>
  • Loading branch information
Niloth-p and mkp6781 committed May 18, 2024
1 parent e32ae3e commit 96bdbba
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
TYPING_STARTED_EXPIRY_PERIOD,
TYPING_STARTED_WAIT_PERIOD,
TYPING_STOPPED_WAIT_PERIOD,
Composition,
PrivateComposition,
StreamComposition,
)
from zulipterminal.config.keys import (
keys_for_command,
Expand Down Expand Up @@ -103,6 +106,144 @@ def test_not_calling_typing_method_without_recipients(

assert not write_box.model.send_typing_status_by_user_ids.called

mocked_saved_drafts = [
case(None, id="no_saved_draft_exists"),
case(
StreamComposition(
type="stream",
to="Random Stream",
content="Random stream draft",
subject="Topic name",
read_by_sender=True,
),
id="saved_stream_draft_exists",
),
case(
PrivateComposition(
type="private",
to=[5140],
content="Random private draft",
read_by_sender=True,
),
id="saved_private_draft_exists",
),
]

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
"draft_saved_in_current_session",
mocked_saved_drafts,
)
def test_keypress_SAVE_AS_DRAFT_stream(
self,
key: str,
mocker: MockerFixture,
draft_saved_in_current_session: Optional[Composition],
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.model.session_draft_message.return_value = (
draft_saved_in_current_session
)

draft_composition = StreamComposition(
type="stream",
to="Current Stream",
content="Random message",
subject="Topic",
read_by_sender=True,
)
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.stream_write_box = mocker.Mock(edit_text=draft_composition["to"])
write_box.title_write_box = mocker.Mock(edit_text=draft_composition["subject"])

write_box.compose_box_status = "open_with_stream"
write_box.stream_id = 1

size = widget_size(write_box)
write_box.keypress(size, key)

assert write_box.model.session_draft_message.called
if draft_saved_in_current_session is not None:
write_box.view.controller.save_draft_confirmation_popup.assert_called_once_with(
draft_composition
)
else:
write_box.model.save_draft.assert_called_once_with(draft_composition)

@pytest.mark.parametrize("key", keys_for_command("SAVE_AS_DRAFT"))
@pytest.mark.parametrize(
"draft_composition",
[
case(
{
"type": "private",
"to": [5140],
"content": "Random message",
"read_by_sender": True,
},
id="dm",
),
case(
{
"type": "private",
"to": [5140, 5179],
"content": "Random DM group message",
"read_by_sender": True,
},
id="dm_group",
),
],
)
@pytest.mark.parametrize(
"is_every_recipient_valid",
[True, False],
ids=["valid_recipients", "invalid_recipients"],
)
@pytest.mark.parametrize(
"draft_saved_in_current_session",
mocked_saved_drafts,
)
def test_keypress_SAVE_AS_DRAFT_private(
self,
key: str,
mocker: MockerFixture,
draft_composition: PrivateComposition,
is_every_recipient_valid: bool,
draft_saved_in_current_session: Optional[Composition],
write_box: WriteBox,
widget_size: Callable[[Widget], urwid_Size],
) -> None:
mocker.patch(MODULE + ".WriteBox.update_recipients")
write_box.msg_write_box = mocker.Mock(edit_text=draft_composition["content"])
write_box.to_write_box = mocker.Mock()
mocker.patch(
MODULE + ".WriteBox._tidy_valid_recipients_and_notify_invalid_ones",
return_value=is_every_recipient_valid,
)

write_box.model.session_draft_message.return_value = (
draft_saved_in_current_session
)
write_box.compose_box_status = "open_with_private"
write_box.recipient_user_ids = draft_composition["to"]

size = widget_size(write_box)
write_box.keypress(size, key)

if not is_every_recipient_valid:
assert not write_box.model.save_draft.called
assert not write_box.view.controller.save_draft_confirmation_popup.called
else:
assert write_box.model.session_draft_message.called
if draft_saved_in_current_session is not None:
write_box.view.controller.save_draft_confirmation_popup.assert_called_once_with(
draft_composition
)
else:
write_box.model.save_draft.assert_called_once_with(draft_composition)

@pytest.mark.parametrize(
"text, state, is_valid_stream, required_typeahead",
[
Expand Down

0 comments on commit 96bdbba

Please sign in to comment.