mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Because some changes are going to be made to the way that removal of widgets happens, and because this will affect the linkage between parents and children, and because we don't want the current way of working to change... extend to the tests to be sure that when an unmount happens a widget has no children any more, but still knows about its parent.
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from textual.app import App, ComposeResult
|
|
from textual import events
|
|
from textual.containers import Container
|
|
from textual.screen import Screen
|
|
|
|
|
|
async def test_unmount():
|
|
"""Test unmount events are received in reverse DOM order."""
|
|
unmount_ids: list[str] = []
|
|
|
|
class UnmountWidget(Container):
|
|
def on_unmount(self, event: events.Unmount):
|
|
unmount_ids.append(f"{self.__class__.__name__}#{self.id}-{self.parent is not None}-{len(self.children)}")
|
|
|
|
class MyScreen(Screen):
|
|
def compose(self) -> ComposeResult:
|
|
yield UnmountWidget(
|
|
UnmountWidget(
|
|
UnmountWidget(id="bar1"), UnmountWidget(id="bar2"), id="bar"
|
|
),
|
|
UnmountWidget(
|
|
UnmountWidget(id="baz1"), UnmountWidget(id="baz2"), id="baz"
|
|
),
|
|
id="top",
|
|
)
|
|
|
|
def on_unmount(self, event: events.Unmount):
|
|
unmount_ids.append(f"{self.__class__.__name__}#{self.id}")
|
|
|
|
class UnmountApp(App):
|
|
async def on_mount(self) -> None:
|
|
await self.push_screen(MyScreen(id="main"))
|
|
|
|
app = UnmountApp()
|
|
async with app.run_test() as pilot:
|
|
await pilot.exit(None)
|
|
|
|
expected = [
|
|
"UnmountWidget#bar1-True-0",
|
|
"UnmountWidget#bar2-True-0",
|
|
"UnmountWidget#baz1-True-0",
|
|
"UnmountWidget#baz2-True-0",
|
|
"UnmountWidget#bar-True-0",
|
|
"UnmountWidget#baz-True-0",
|
|
"UnmountWidget#top-True-0",
|
|
"MyScreen#main",
|
|
]
|
|
|
|
assert unmount_ids == expected
|