Test that a binding may be skipped by an action raising SkipAction
()
| 606 | |
| 607 | |
| 608 | async def test_skip_action() -> None: |
| 609 | """Test that a binding may be skipped by an action raising SkipAction""" |
| 610 | |
| 611 | class Handle(Widget, can_focus=True): |
| 612 | BINDINGS = [("t", "test('foo')", "Test")] |
| 613 | |
| 614 | def action_test(self, text: str) -> None: |
| 615 | self.app.exit(text) |
| 616 | |
| 617 | no_handle_invoked = False |
| 618 | |
| 619 | class NoHandle(Widget, can_focus=True): |
| 620 | BINDINGS = [("t", "test('bar')", "Test")] |
| 621 | |
| 622 | def action_test(self, text: str) -> bool: |
| 623 | nonlocal no_handle_invoked |
| 624 | no_handle_invoked = True |
| 625 | raise SkipAction() |
| 626 | |
| 627 | class SkipApp(App): |
| 628 | def compose(self) -> ComposeResult: |
| 629 | yield Handle(NoHandle()) |
| 630 | |
| 631 | def on_mount(self) -> None: |
| 632 | self.query_one(NoHandle).focus() |
| 633 | |
| 634 | async with SkipApp().run_test() as pilot: |
| 635 | # Check the NoHandle widget has focus |
| 636 | assert pilot.app.query_one(NoHandle).has_focus |
| 637 | # Press the "t" key |
| 638 | await pilot.press("t") |
| 639 | # Check the action on the no handle widget was called |
| 640 | assert no_handle_invoked |
| 641 | # Check the return value, confirming that the action on Handle was called |
| 642 | assert pilot.app.return_value == "foo" |