From 6af2b61fc78e87881b2dd0bf0b13caeab76b37c8 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 13 Jan 2023 20:56:33 +0000 Subject: [PATCH] watching computed --- tests/test_reactive.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_reactive.py b/tests/test_reactive.py index eef5a9423..d908fa025 100644 --- a/tests/test_reactive.py +++ b/tests/test_reactive.py @@ -337,3 +337,35 @@ async def test_reactive_inheritance(): secondary.baz assert tertiary.baz == "baz" + + +async def test_watch_compute(): + """Check that watching a computed attribute works.""" + + watch_called: list[bool] = [] + + class Calculator(App): + + numbers = var("0") + show_ac = var(True) + value = var("") + + def compute_show_ac(self) -> bool: + return self.value in ("", "0") and self.numbers == "0" + + def watch_show_ac(self, show_ac: bool) -> None: + """Called when show_ac changes.""" + watch_called.append(show_ac) + + app = Calculator() + + async with app.run_test() as pilot: + assert app.show_ac is True + app.value = "1" + assert app.show_ac is False + app.value = "0" + assert app.show_ac is True + app.numbers = "123" + assert app.show_ac is False + + assert watch_called == [True, False, True, False]