Skip to content

Commit

Permalink
refactor: boxes/buttons/helper/model/ui: Hook semantic style helpers.
Browse files Browse the repository at this point in the history
Tests amended.
Fixes #782.
  • Loading branch information
Ezio-Sarthak committed Jun 16, 2021
1 parent 943ae89 commit 9a34ecd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 36 deletions.
14 changes: 7 additions & 7 deletions tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ def test_notify_quotes(monkeypatch, mocker, OS, cmd_length, title, text):
)
def test_display_error_if_present(mocker, response, footer_updated):
controller = mocker.Mock()
set_footer_text = controller.view.set_footer_text
report_error = controller.report_error

display_error_if_present(response, controller)

if footer_updated:
set_footer_text.assert_called_once_with(response["msg"], 3)
report_error.assert_called_once_with(response["msg"])
else:
set_footer_text.assert_not_called()
report_error.assert_not_called()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -424,17 +424,17 @@ def test_display_error_if_present(mocker, response, footer_updated):
)
def test_notify_if_message_sent_outside_narrow(mocker, req, narrow, footer_updated):
controller = mocker.Mock()
set_footer_text = controller.view.set_footer_text
report_success = controller.report_success
controller.model.narrow = narrow

notify_if_message_sent_outside_narrow(req, controller)

if footer_updated:
set_footer_text.assert_called_once_with(
"Message is sent outside of current narrow.", 3
report_success.assert_called_once_with(
"Message is sent outside of current narrow."
)
else:
set_footer_text.assert_not_called()
report_success.assert_not_called()


@pytest.mark.parametrize(
Expand Down
6 changes: 3 additions & 3 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,11 @@ def test_update_stream_message(
self.client.update_message.assert_called_once_with(req)
assert result == return_value
self.display_error_if_present.assert_called_once_with(response, self.controller)
set_footer_text = model.controller.view.set_footer_text
report_success = model.controller.report_success
if result and footer_updated:
set_footer_text.assert_called_once_with("You changed a message's topic.", 3)
report_success.assert_called_once_with("You changed a message's topic.")
else:
set_footer_text.assert_not_called()
report_success.assert_not_called()

# NOTE: This tests only getting next-unread, not a fixed anchor
def test_success_get_messages(
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def test_keypress_autohide_streams(self, view, mocker, autohide, key, widget_siz
def test_keypress_OPEN_DRAFT(self, view, mocker, draft, key, widget_size):
view.body = mocker.Mock()
view.middle_column = mocker.Mock()
view.set_footer_text = mocker.Mock()
view.controller.report_error = mocker.Mock()
view.controller.is_in_editor_mode = lambda: False
view.model.stream_id_from_name.return_value = 10
view.model.session_draft_message.return_value = draft
Expand All @@ -383,8 +383,8 @@ def test_keypress_OPEN_DRAFT(self, view, mocker, draft, key, widget_size):
assert view.write_box.msg_write_box.edit_pos == len(draft["content"])
view.middle_column.set_focus.assert_called_once_with("footer")
else:
view.set_footer_text.assert_called_once_with(
"No draft message was saved in this session.", 3
view.controller.report_error.assert_called_once_with(
"No draft message was saved in this session."
)

@pytest.mark.parametrize("key", keys_for_command("SEARCH_PEOPLE"))
Expand Down
6 changes: 3 additions & 3 deletions tests/ui_tools/test_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ def test__switch_narrow_to(
assert mocked_button.controller.narrow_to_topic.called == narrow_to_topic_called

@pytest.mark.parametrize(
"error, set_footer_text_called, _switch_narrow_to_called, exit_popup_called",
"error, report_error_called, _switch_narrow_to_called, exit_popup_called",
[
("Some Validation Error", True, False, False),
("", False, True, True),
Expand All @@ -867,7 +867,7 @@ def test_handle_narrow_link(
self,
mocker,
error,
set_footer_text_called,
report_error_called,
_switch_narrow_to_called,
exit_popup_called,
):
Expand All @@ -883,6 +883,6 @@ def test_handle_narrow_link(

assert mocked_button._parse_narrow_link.called
assert mocked_button._validate_narrow_link.called
assert mocked_button.view.set_footer_text.called == set_footer_text_called
assert mocked_button.controller.report_error.called == report_error_called
assert mocked_button._switch_narrow_to.called == _switch_narrow_to_called
assert mocked_button.controller.exit_popup.called == exit_popup_called
4 changes: 2 additions & 2 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def notify(title: str, html_text: str) -> str:

def display_error_if_present(response: Dict[str, Any], controller: Any) -> None:
if response["result"] == "error" and hasattr(controller, "view"):
controller.view.set_footer_text(response["msg"], 3)
controller.report_error(response["msg"])


def check_narrow_and_notify(
Expand All @@ -674,7 +674,7 @@ def check_narrow_and_notify(
and current_narrow != outer_narrow
and current_narrow != inner_narrow
):
controller.view.set_footer_text("Message is sent outside of current narrow.", 3)
controller.report_success("Message is sent outside of current narrow.")


def notify_if_message_sent_outside_narrow(
Expand Down
5 changes: 2 additions & 3 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def session_draft_message(self) -> Optional[Composition]:

def save_draft(self, draft: Composition) -> None:
self._draft = deepcopy(draft)
self.controller.view.set_footer_text("Saved message as draft", 3)
self.controller.report_success("Saved message as draft")

@asynch
def toggle_message_star_status(self, message: Message) -> None:
Expand Down Expand Up @@ -471,9 +471,8 @@ def update_stream_message(
if response["result"] == "success":
old_topic = self.index["messages"][message_id].get("subject", None)
new_topic = request["topic"]
view = self.controller.view
if old_topic != new_topic:
view.set_footer_text("You changed a message's topic.", 3)
self.controller.report_success("You changed a message's topic.")

return response["result"] == "success"

Expand Down
4 changes: 3 additions & 1 deletion zulipterminal/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def keypress(self, size: urwid_Box, key: str) -> Optional[str]:
self.body.focus_col = 1
self.middle_column.set_focus("footer")
else:
self.set_footer_text("No draft message was saved in this session.", 3)
self.controller.report_error(
"No draft message was saved in this session."
)
return key
elif is_command_key("ABOUT", key):
self.controller.show_about()
Expand Down
23 changes: 10 additions & 13 deletions zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
content=self.msg_write_box.edit_text,
)
else:
self.view.set_footer_text(
"Cannot send message without specifying recipients.", 3
self.view.controller.report_error(
"Cannot send message without specifying recipients."
)
success = None
if success:
Expand Down Expand Up @@ -624,7 +624,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
primary_key_for_command("AUTOCOMPLETE_REVERSE"),
)
)
self.view.set_footer_text(invalid_stream_error, 3)
self.view.controller.report_error(invalid_stream_error)
return key
user_ids = self.model.get_other_subscribers_in_stream(
stream_name=stream_name
Expand Down Expand Up @@ -658,7 +658,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
invalid_emails_error = (
f"Invalid recipient(s) - {', '.join(invalid_emails)}"
)
self.view.set_footer_text(invalid_emails_error, 3)
self.view.controller.report_error(invalid_emails_error)
return key
users = self.model.user_dict
self.recipient_user_ids = [
Expand Down Expand Up @@ -1569,15 +1569,13 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
self.model.controller.view.middle_column.set_focus("footer")
elif is_command_key("EDIT_MESSAGE", key):
if self.message["sender_id"] != self.model.user_id:
self.model.controller.view.set_footer_text(
" You can't edit messages sent by other users.", 3
self.model.controller.report_error(
" You can't edit messages sent by other users."
)
return key
# Check if editing is allowed in the realm
elif not self.model.initial_data["realm_allow_message_editing"]:
self.model.controller.view.set_footer_text(
" Editing sent message is disabled.", 3
)
self.model.controller.report_error(" Editing sent message is disabled.")
return key
# Check if message is still editable, i.e. within
# the time limit. A limit of 0 signifies no limit
Expand All @@ -1590,16 +1588,15 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
]
if time_since_msg_sent >= edit_time_limit:
if self.message["type"] == "private":
self.model.controller.view.set_footer_text(
" Time Limit for editing the message has been exceeded.", 3
self.model.controller.report_error(
" Time Limit for editing the message has been exceeded."
)
return key
elif self.message["type"] == "stream":
self.model.controller.view.set_footer_text(
self.model.controller.report_warning(
" Only topic editing allowed."
" Time Limit for editing the message body has"
" been exceeded.",
3,
)
msg_body_edit_enabled = False

Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def handle_narrow_link(self) -> None:
error = self._validate_narrow_link(parsed_link)

if error:
self.view.set_footer_text(f" {error}", duration=3)
self.controller.report_error(f" {error}")
else:
self._switch_narrow_to(parsed_link)

Expand Down

0 comments on commit 9a34ecd

Please sign in to comment.