test for is_attached

This commit is contained in:
Will McGugan
2023-02-09 11:17:41 +00:00
parent 6b91501ade
commit 93acc27482
2 changed files with 16 additions and 3 deletions

View File

@@ -125,8 +125,17 @@ class MessagePump(metaclass=MessagePumpMeta):
@property @property
def is_attached(self) -> bool: def is_attached(self) -> bool:
"""Check the node is attached to the DOM""" """Check the node is attached to the app via the DOM."""
return self._parent is not None
from .app import App
node = self
while not isinstance(node, App):
if node._parent is None:
return False
node = node._parent
return True
def _attach(self, parent: MessagePump) -> None: def _attach(self, parent: MessagePump) -> None:
"""Set the parent, and therefore attach this node to the tree. """Set the parent, and therefore attach this node to the tree.

View File

@@ -8,9 +8,13 @@ from textual.containers import Container
async def test_remove_single_widget(): async def test_remove_single_widget():
"""It should be possible to the only widget on a screen.""" """It should be possible to the only widget on a screen."""
async with App().run_test() as pilot: async with App().run_test() as pilot:
await pilot.app.mount(Static()) widget = Static()
assert not widget.is_attached
await pilot.app.mount(widget)
assert widget.is_attached
assert len(pilot.app.screen.children) == 1 assert len(pilot.app.screen.children) == 1
await pilot.app.query_one(Static).remove() await pilot.app.query_one(Static).remove()
assert not widget.is_attached
assert len(pilot.app.screen.children) == 0 assert len(pilot.app.screen.children) == 0