Skip to content

Commit ad8fc87

Browse files
preetmishraneiljp
authored andcommitted
tests: conftest/ui/ui_tools: refactor: Extract key params as fixture.
Added a fixture, navigation_key_expected_key_pair, in `conftest.py` to generate test cases for keypress tests related to navigation. This helps in reducing code duplication through the sharing of the common fixture among different navigation tests. Tests amended for navigation keypresses in View and HelpView to leverage the fixture.
1 parent 8d00a90 commit ad8fc87

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from zulipterminal.config.keys import keys_for_command
78
from zulipterminal.helper import initial_index as helper_initial_index
89
from zulipterminal.ui_tools.boxes import MessageBox
910
from zulipterminal.ui_tools.buttons import (
@@ -756,3 +757,27 @@ def classified_unread_counts():
756757
99: 1
757758
}
758759
}
760+
761+
# --------------- UI Fixtures -----------------------------------------
762+
763+
764+
@pytest.fixture(params=[
765+
(key, expected_key)
766+
for keys, expected_key in [
767+
(keys_for_command('GO_UP'), 'up'),
768+
(keys_for_command('GO_DOWN'), 'down'),
769+
(keys_for_command('SCROLL_UP'), 'page up'),
770+
(keys_for_command('SCROLL_DOWN'), 'page down'),
771+
(keys_for_command('GO_TO_BOTTOM'), 'end'),
772+
]
773+
for key in keys
774+
],
775+
ids=lambda param: 'key:{}-expected_key:{}'.format(*param)
776+
)
777+
def navigation_key_expected_key_pair(request):
778+
"""
779+
Fixture to generate pairs of navigation keys with their respective
780+
expected key.
781+
The expected key is the one which is passed to the super `keypress` calls.
782+
"""
783+
return request.param

tests/ui/test_ui.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,9 @@ def test_show_right_panel(self, mocker, view,
181181
else:
182182
view.body.options.assert_not_called()
183183

184-
@pytest.mark.parametrize('key, expected_key', [
185-
(key, expected_key)
186-
for keys, expected_key in [
187-
(keys_for_command('GO_UP'), 'up'),
188-
(keys_for_command('GO_DOWN'), 'down'),
189-
(keys_for_command('SCROLL_UP'), 'page up'),
190-
(keys_for_command('SCROLL_DOWN'), 'page down'),
191-
(keys_for_command('GO_TO_BOTTOM'), 'end'),
192-
]
193-
for key in keys
194-
])
195184
def test_keypress_normal_mode_navigation(self, view, mocker,
196-
key, expected_key):
185+
navigation_key_expected_key_pair):
186+
key, expected_key = navigation_key_expected_key_pair
197187
view.users_view = mocker.Mock()
198188
view.body = mocker.Mock()
199189
view.user_search = mocker.Mock()

tests/ui/test_ui_tools.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,9 @@ def test_keypress_GO_BACK(self, key):
11121112
self.help_view.keypress(size, key)
11131113
assert self.controller.exit_popup.called
11141114

1115-
@pytest.mark.parametrize('key, expected_key', [
1116-
(key, expected_key)
1117-
for keys, expected_key in [
1118-
(keys_for_command('GO_UP'), 'up'),
1119-
(keys_for_command('GO_DOWN'), 'down'),
1120-
(keys_for_command('SCROLL_UP'), 'page up'),
1121-
(keys_for_command('SCROLL_DOWN'), 'page down'),
1122-
(keys_for_command('GO_TO_BOTTOM'), 'end'),
1123-
]
1124-
for key in keys
1125-
])
1126-
def test_keypress_navigation(self, mocker, key, expected_key):
1115+
def test_keypress_navigation(self, mocker,
1116+
navigation_key_expected_key_pair):
1117+
key, expected_key = navigation_key_expected_key_pair
11271118
size = (200, 20)
11281119
super_keypress = mocker.patch(VIEWS + '.urwid.ListBox.keypress')
11291120
self.help_view.keypress(size, key)

0 commit comments

Comments
 (0)