From e87edd79cbb0312ff031c7b1c3d44b6875b4ca28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Tue, 11 Apr 2023 13:29:56 +0100 Subject: [PATCH] Add test for scroll_to_center method. --- .../snapshot_apps/scroll_to_center.py | 40 +++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 4 ++ 2 files changed, 44 insertions(+) create mode 100644 tests/snapshot_tests/snapshot_apps/scroll_to_center.py diff --git a/tests/snapshot_tests/snapshot_apps/scroll_to_center.py b/tests/snapshot_tests/snapshot_apps/scroll_to_center.py new file mode 100644 index 000000000..fdd616cb6 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/scroll_to_center.py @@ -0,0 +1,40 @@ +from textual.app import App, ComposeResult +from textual.containers import HorizontalScroll, VerticalScroll +from textual.widgets import Label + + +class MyApp(App[None]): + CSS = """ + VerticalScroll, HorizontalScroll { + border: round $primary; + } + #vertical { + height: 21; + } + HorizontalScroll { + height: auto; + } + """ + + def compose(self) -> ComposeResult: + with VerticalScroll(): + yield Label(("SPAM\n" * 25)[:-1]) + with VerticalScroll(): + yield Label(("SPAM\n" * 53)[:-1]) + with VerticalScroll(id="vertical"): + yield Label(("SPAM\n" * 78)[:-1]) + with HorizontalScroll(): + yield Label(("v\n" * 17)[:-1]) + yield Label("@" * 302) + yield Label("[red]>>bullseye<<[/red]", id="bullseye") + yield Label("@" * 99) + yield Label(("SPAM\n" * 49)[:-1]) + yield Label(("SPAM\n" * 51)[:-1]) + yield Label(("SPAM\n" * 59)[:-1]) + + def key_s(self) -> None: + self.query_one("#bullseye").scroll_to_center() + + +if __name__ == "__main__": + MyApp().run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 14226a191..6e8767885 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -406,3 +406,7 @@ def test_fr_margins(snap_compare): def test_scroll_visible(snap_compare): # https://github.com/Textualize/textual/issues/2181 assert snap_compare(SNAPSHOT_APPS_DIR / "scroll_visible.py", press=["t"]) + + +def test_scroll_to_center(snap_compare): + assert snap_compare(SNAPSHOT_APPS_DIR / "scroll_to_center.py", press=["s"])