File: test_unmount.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (54 lines) | stat: -rw-r--r-- 1,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import annotations

from textual import events
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.screen import Screen


async def test_unmount() -> None:
    """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._nodes)}"
            )

    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