test_radio.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_radio_click(screen: Screen):
  4. radio = ui.radio(['A', 'B', 'C'])
  5. ui.label().bind_text_from(radio, 'value', lambda x: f'Value: {x}')
  6. screen.open('/')
  7. screen.click('A')
  8. screen.should_contain('Value: A')
  9. screen.click('B')
  10. screen.should_contain('Value: B')
  11. screen.click('B') # already selected, should not change
  12. screen.wait(0.5)
  13. screen.should_contain('Value: B')
  14. def test_radio_set_value(screen: Screen):
  15. radio = ui.radio(['A', 'B', 'C'])
  16. ui.label().bind_text_from(radio, 'value', lambda x: f'Value: {x}')
  17. screen.open('/')
  18. radio.set_value('B')
  19. screen.should_contain('Value: B')
  20. def test_radio_set_options(screen: Screen):
  21. radio = ui.radio(['A', 'B', 'C'], value='C', on_change=lambda e: ui.notify(f'Event: {e.value}'))
  22. ui.label().bind_text_from(radio, 'value', lambda x: f'Value: {x}')
  23. ui.button('reverse', on_click=lambda: (radio.options.reverse(), radio.update())) # type: ignore
  24. ui.button('clear', on_click=lambda: (radio.options.clear(), radio.update())) # type: ignore
  25. screen.open('/')
  26. radio.set_options(['C', 'D', 'E'])
  27. screen.should_contain('D')
  28. screen.should_contain('E')
  29. screen.should_contain('Value: C')
  30. radio.set_options(['X', 'Y', 'Z'])
  31. screen.should_contain('X')
  32. screen.should_contain('Y')
  33. screen.should_contain('Z')
  34. screen.should_contain('Value: None')
  35. screen.should_contain('Event: None')
  36. radio.set_options(['1', '2', '3'], value='3')
  37. screen.should_contain('Value: 3')
  38. screen.should_contain('Event: 3')
  39. screen.click('reverse')
  40. screen.should_contain('Value: 3')
  41. screen.should_contain('Event: 3')
  42. screen.click('clear')
  43. screen.should_contain('Value: None')