From 26e81c99e316b0b599c87dc2984c13414cc518b9 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, 29 Aug 2023 12:06:59 +0100 Subject: [PATCH] Test screen (sub-)title. --- CHANGELOG.md | 5 ++ tests/test_header.py | 151 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 tests/test_header.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c463bd7c..31ef2bbd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - TCSS styles `layer` and `layers` can be strings https://github.com/Textualize/textual/pull/3169 +- Screen-specific (sub-)title attributes https://github.com/Textualize/textual/pull/3199: + - `Screen.TITLE` + - `Screen.SUB_TITLE` + - `Screen.title` + - `Screen.sub_title` ### Changed diff --git a/tests/test_header.py b/tests/test_header.py new file mode 100644 index 000000000..45df30fa2 --- /dev/null +++ b/tests/test_header.py @@ -0,0 +1,151 @@ +from textual.app import App +from textual.screen import Screen +from textual.widgets import Header + + +async def test_screen_title_none_is_ignored(): + class MyScreen(Screen): + def compose(self): + yield Header() + + class MyApp(App): + TITLE = "app title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test(): + assert app.query_one("HeaderTitle").text == "app title" + + +async def test_screen_title_overrides_app_title(): + class MyScreen(Screen): + TITLE = "screen title" + + def compose(self): + yield Header() + + class MyApp(App): + TITLE = "app title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test(): + assert app.query_one("HeaderTitle").text == "screen title" + + +async def test_screen_title_reactive_updates_title(): + class MyScreen(Screen): + TITLE = "screen title" + + def compose(self): + yield Header() + + class MyApp(App): + TITLE = "app title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test() as pilot: + app.screen.title = "new screen title" + await pilot.pause() + assert app.query_one("HeaderTitle").text == "new screen title" + + +async def test_app_title_reactive_does_not_update_title_when_screen_title_is_set(): + class MyScreen(Screen): + TITLE = "screen title" + + def compose(self): + yield Header() + + class MyApp(App): + TITLE = "app title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test() as pilot: + app.title = "new app title" + await pilot.pause() + assert app.query_one("HeaderTitle").text == "screen title" + + +async def test_screen_sub_title_none_is_ignored(): + class MyScreen(Screen): + def compose(self): + yield Header() + + class MyApp(App): + SUB_TITLE = "app sub-title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test(): + assert app.query_one("HeaderTitle").sub_text == "app sub-title" + + +async def test_screen_sub_title_overrides_app_sub_title(): + class MyScreen(Screen): + SUB_TITLE = "screen sub-title" + + def compose(self): + yield Header() + + class MyApp(App): + SUB_TITLE = "app sub-title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test(): + assert app.query_one("HeaderTitle").sub_text == "screen sub-title" + + +async def test_screen_sub_title_reactive_updates_sub_title(): + class MyScreen(Screen): + SUB_TITLE = "screen sub-title" + + def compose(self): + yield Header() + + class MyApp(App): + SUB_TITLE = "app sub-title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test() as pilot: + app.screen.sub_title = "new screen sub-title" + await pilot.pause() + assert app.query_one("HeaderTitle").sub_text == "new screen sub-title" + + +async def test_app_sub_title_reactive_does_not_update_sub_title_when_screen_sub_title_is_set(): + class MyScreen(Screen): + SUB_TITLE = "screen sub-title" + + def compose(self): + yield Header() + + class MyApp(App): + SUB_TITLE = "app sub-title" + + def on_mount(self): + self.push_screen(MyScreen()) + + app = MyApp() + async with app.run_test() as pilot: + app.sub_title = "new app sub-title" + await pilot.pause() + assert app.query_one("HeaderTitle").sub_text == "screen sub-title"