Add a test for a focused child widget with its own bindings

This commit is contained in:
Dave Pearson
2022-12-13 09:46:27 +00:00
parent d92c252ea6
commit c52ea0bdf2

View File

@@ -138,3 +138,27 @@ async def test_pressing_movement_keys_app() -> None:
await pilot.press("x", *MOVEMENT_KEYS, "x") await pilot.press("x", *MOVEMENT_KEYS, "x")
assert pilot.app.pressed_keys == ["x", *MOVEMENT_KEYS, "x"] assert pilot.app.pressed_keys == ["x", *MOVEMENT_KEYS, "x"]
##############################################################################
class WidgetWithBindings(Static,can_focus=True):
"""A widget that has its own bindings for the movement keys."""
BINDINGS=[Binding(key,f"local_record('{key}')",key) for key in MOVEMENT_KEYS]
async def action_local_record(self, key:str) -> None:
# Sneaky forward reference. Just for the purposes of testing.
await self.app.action_record(f"locally_{key}")
class AppWithWidgetWithBindings(AppKeyRecorder):
"""A test app that composes with a widget that has movement bindings."""
def compose(self) -> ComposeResult:
yield WidgetWithBindings()
def on_mount(self) -> None:
self.query_one(WidgetWithBindings).focus()
async def test_focused_child_widget_with_movement_bindings() -> None:
"""A focused child widget with movement bindings should handle its own actions."""
async with AppWithWidgetWithBindings().run_test() as pilot:
await pilot.press(*MOVEMENT_KEYS)
assert pilot.app.pressed_keys == [f"locally_{key}" for key in MOVEMENT_KEYS]