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 55 56 57 58 59 60 61 62 63 64
|
from rich.style import Style
from rich.text import Text
from textual.app import App, ComposeResult
from textual.widgets import Tree
from textual.widgets.tree import TreeNode
class HistoryTree(Tree):
def __init__(self) -> None:
super().__init__("Root")
self.counter = 0
self.render_hits: set[tuple[int, int]] = set()
def on_mount(self) -> None:
self.root.add("Child").add_leaf("Grandchild")
def render_label(self, node: TreeNode, base_style: Style, style: Style) -> Text:
self.render_hits.add((node.id, self.counter))
return super().render_label(node, base_style, style)
class RefreshApp(App[None]):
def compose(self) -> ComposeResult:
yield HistoryTree()
def on_mount(self) -> None:
self.query_one(HistoryTree).root.expand_all()
async def test_initial_state() -> None:
"""Initially all the visible nodes should have had a render call."""
app = RefreshApp()
async with app.run_test():
assert app.query_one(HistoryTree).render_hits == {(0,0), (1,0), (2,0)}
async def test_root_refresh() -> None:
"""A refresh of the root node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (0, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.refresh()
await pilot.pause()
assert (0, 1) in pilot.app.query_one(HistoryTree).render_hits
async def test_child_refresh() -> None:
"""A refresh of the child node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (1, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.children[0].refresh()
await pilot.pause()
assert (1, 1) in pilot.app.query_one(HistoryTree).render_hits
async def test_grandchild_refresh() -> None:
"""A refresh of the grandchild node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (2, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.children[0].children[0].refresh()
await pilot.pause()
assert (2, 1) in pilot.app.query_one(HistoryTree).render_hits
|