From 2b8b7c15e600217f185b48a32db5e7ea0ade6679 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 14 Dec 2022 12:09:00 +0000 Subject: [PATCH] Add extra test for validator called before dom ready, update changelog --- CHANGELOG.md | 5 +++++ tests/test_reactive.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42111addd..4b7e918e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Fixed + +- Fixed validator not running on first reactive set https://github.com/Textualize/textual/pull/1359 ## [0.6.0] - 2022-12-11 diff --git a/tests/test_reactive.py b/tests/test_reactive.py index 52476ae1a..6ccfa0656 100644 --- a/tests/test_reactive.py +++ b/tests/test_reactive.py @@ -194,6 +194,27 @@ async def test_validate_init_true(): assert validator_call_count == 1 +async def test_validate_init_true_set_before_dom_ready(): + """When init is True for a reactive attribute, Textual should call the validator + AND the watch method when the app starts.""" + validator_call_count = 0 + + class ValidatorInitTrue(App): + count = var(5, init=True) + + def validate_count(self, value: int) -> int: + nonlocal validator_call_count + validator_call_count += 1 + return value + 1 + + app = ValidatorInitTrue() + app.count = 5 + async with app.run_test(): + assert app.count == 6 # Validator should run, so value should be 5+1=6 + assert validator_call_count == 1 + + + @pytest.mark.xfail(reason="Compute methods not called when init=True [issue#1227]") async def test_reactive_compute_first_time_set(): class ReactiveComputeFirstTimeSet(App):