Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 12, 2024
1 parent 71f7277 commit 08c4aad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- `TextArea.line_number_start` reactive attribute https://github.com/Textualize/textual/pull/4471
- Added `DOMNode.mutate_reactive`

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion src/textual/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _set(self, obj: Reactable, value: ReactiveType, always: bool = False) -> Non
if callable(public_validate_function):
value = public_validate_function(value)
# If the value has changed, or this is the first time setting the value
if always or current_value != value or self._always_update:
if always or self._always_update or current_value != value:
# Store the internal value
setattr(obj, self.internal_name, value)

Expand Down
41 changes: 40 additions & 1 deletion tests/test_reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,6 @@ def on_test_widget_test_message(self, event: TestWidget.TestMessage) -> None:
message_senders.append(event._sender)

class TestApp(App[None]):

def compose(self) -> ComposeResult:
yield TestContainer()

Expand All @@ -744,3 +743,43 @@ def compose(self) -> ComposeResult:
pilot.app.query_one(TestWidget).make_reaction()
await pilot.pause()
assert message_senders == [pilot.app.query_one(TestWidget)]


async def test_mutate_reactive() -> None:
"""Test explicitly mutating reactives"""

watched_names: list[list[str]] = []

class TestWidget(Widget):
names: reactive[list[str]] = reactive(list)

def watch_names(self, names: list[str]) -> None:
watched_names.append(names.copy())

class TestApp(App):
def compose(self) -> ComposeResult:
yield TestWidget()

app = TestApp()
async with app.run_test():
widget = app.query_one(TestWidget)
# watch method called on startup
assert watched_names == [[]]

# Mutate the list
widget.names.append("Paul")
# No changes expected
assert watched_names == [[]]
# Explicitly mutate the reactive
widget.mutate_reactive(TestWidget.names)
# Watcher will be invoked
assert watched_names == [[], ["Paul"]]
# Make further modifications
widget.names.append("Jessica")
widget.names.remove("Paul")
# No change expected
assert watched_names == [[], ["Paul"]]
# Explicit mutation
widget.mutate_reactive(TestWidget.names)
# Watcher should be invoked
assert watched_names == [[], ["Paul"], ["Jessica"]]

0 comments on commit 08c4aad

Please sign in to comment.