Skip to content

Commit

Permalink
Fix for ChoiceElement duplicate self.update() calls and basic Choic…
Browse files Browse the repository at this point in the history
…eElement unit testing (#3736)

* Fix duplicate `self.update()` call

* Add basic unit testing for choice_element

* Add test for thing I was complaining about in the first place (lol oops)

* Add requested changes
  • Loading branch information
sSasha-uwu committed Sep 17, 2024
1 parent fe3cbc2 commit d6d4ecd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
3 changes: 1 addition & 2 deletions nicegui/elements/choice_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def set_options(self, options: Union[List, Dict], *, value: Any = ...) -> None:
:param value: The new value. If not given, the current value is kept.
"""
self.options = options
self.update()
if value is not ...:
self.value = value
self.update()
self.update()
68 changes: 68 additions & 0 deletions tests/test_radio_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from nicegui import events, ui
from nicegui.testing import Screen

def test_radio_click(screen: Screen):
r = ui.radio(['A', 'B', 'C'])

screen.open('/')
screen.click('A')
assert r.value == 'A'
screen.click('B')
assert r.value == 'B'

def test_radio_click_already_selected(screen: Screen):
r = ui.radio(['A', 'B', 'C'], value='B')

screen.open('/')
screen.click('B')
assert r.value == 'B'

def test_radio_set_value(screen: Screen):
r = ui.radio(['A', 'B', 'C'])

screen.open('/')
r.set_value('B')
assert r.value == 'B'

def test_radio_set_options(screen: Screen):
r = ui.radio(['A', 'B', 'C'])

screen.open('/')
r.set_options(['D', 'E', 'F'])
assert r.options == ['D', 'E', 'F']

def test_radio_set_options_value_still_valid(screen: Screen):
r = ui.radio(['A', 'B', 'C'], value='C')

screen.open('/')
r.set_options(['C', 'D', 'E'])
assert r.value == 'C'

def test_radio_set_options_value_none(screen: Screen):
r = ui.radio(['A', 'B', 'C'], value='C')

screen.open('/')
r.set_options(['D', 'E', 'F'])
assert r.value is None

def test_radio_set_options_value(screen: Screen):
r = ui.radio(['A', 'B', 'C'])

screen.open('/')
r.set_options(['D', 'E', 'F'], value='E')
assert r.value == 'E'

def test_radio_set_options_value_callback(screen: Screen):
"""Fix for https://github.com/zauberzeug/nicegui/issues/3733.
When using `set_options` with the value argument set and the `on_change` callback active on the element,
`on_change` should never pass `None` through, even if the old value is not within the new list of element options.
"""

def check_change_is_not_none(e: events.ValueChangeEventArguments):
assert e.value is not None

r = ui.radio(['A', 'B', 'C'], on_change=check_change_is_not_none)

screen.open('/')
r.set_options(['D', 'E', 'F'], value='F')

0 comments on commit d6d4ecd

Please sign in to comment.