Files
textual/tests/test_unmount.py
darrenburns df37a9b90a Add get_child_by_id and get_widget_by_id (#1146)
* Add get_child_by_id and get_widget_by_id

* Remove redundant code

* Add unit tests for app-level get_child_by_id and get_widget_by_id

* Remove redundant test fixture injection

* Update CHANGELOG

* Enforce uniqueness of ID amongst widget children

* Enforce unique widget IDs amongst widgets mounted together

* Update CHANGELOG.md

* Ensuring unique IDs in a more logical place

* Add docstring to NodeList._get_by_id

* Dont use duplicate IDs in tests, dont mount 2000 widgets

* Mounting less widgets in a unit test

* Reword error message

* Use lower-level depth first search in get_widget_by_id to break out early
2022-11-16 15:29:59 +00:00

52 lines
1.6 KiB
Python

from __future__ import annotations
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